Skip to content

Commit

Permalink
added project for GCP PubSub and MongoDB integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jhakp committed Jul 26, 2022
1 parent 5708ecf commit 406590f
Show file tree
Hide file tree
Showing 21 changed files with 594 additions and 30 deletions.
28 changes: 21 additions & 7 deletions CAP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.AzureServiceBus.InMe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.GcpPubSub.GoogleSpanner", "samples\Sample.GcpPubSub.GoogleSpanner\Sample.GcpPubSub.GoogleSpanner.csproj", "{5F857ABF-449B-46C7-B160-CAC1C02DB782}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.GooglePubSub", "src\DotNetCore.CAP.GooglePubSub\DotNetCore.CAP.GooglePubSub.csproj", "{AB823637-CA8C-4139-B9EC-DD29494E2276}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.GoogleSpanner", "src\DotNetCore.CAP.GoogleSpanner\DotNetCore.CAP.GoogleSpanner.csproj", "{79278DDF-D699-4E1F-ACB4-907F8903E350}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.GcpPubSubMongo", "src\DotNetCore.CAP.GcpPubSubMongo\DotNetCore.CAP.GcpPubSubMongo.csproj", "{1D1F4183-8E5F-454F-BCAB-B15748EFBF53}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCore.CAP.GcpPubSubSpanner", "src\DotNetCore.CAP.GcpPubSubSpanner\DotNetCore.CAP.GcpPubSubSpanner.csproj", "{AD9FB3A1-19B3-40B5-828B-3EC00B2E2B6C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.GcpPubSub.MongoDB", "samples\Sample.GcpPubSub.MongoDB\Sample.GcpPubSub.MongoDB.csproj", "{EEA758C3-2AFE-491C-B593-06143C73BAC7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -224,14 +228,22 @@ Global
{5F857ABF-449B-46C7-B160-CAC1C02DB782}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F857ABF-449B-46C7-B160-CAC1C02DB782}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F857ABF-449B-46C7-B160-CAC1C02DB782}.Release|Any CPU.Build.0 = Release|Any CPU
{AB823637-CA8C-4139-B9EC-DD29494E2276}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB823637-CA8C-4139-B9EC-DD29494E2276}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB823637-CA8C-4139-B9EC-DD29494E2276}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB823637-CA8C-4139-B9EC-DD29494E2276}.Release|Any CPU.Build.0 = Release|Any CPU
{79278DDF-D699-4E1F-ACB4-907F8903E350}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79278DDF-D699-4E1F-ACB4-907F8903E350}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79278DDF-D699-4E1F-ACB4-907F8903E350}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79278DDF-D699-4E1F-ACB4-907F8903E350}.Release|Any CPU.Build.0 = Release|Any CPU
{1D1F4183-8E5F-454F-BCAB-B15748EFBF53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D1F4183-8E5F-454F-BCAB-B15748EFBF53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D1F4183-8E5F-454F-BCAB-B15748EFBF53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D1F4183-8E5F-454F-BCAB-B15748EFBF53}.Release|Any CPU.Build.0 = Release|Any CPU
{AD9FB3A1-19B3-40B5-828B-3EC00B2E2B6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD9FB3A1-19B3-40B5-828B-3EC00B2E2B6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD9FB3A1-19B3-40B5-828B-3EC00B2E2B6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD9FB3A1-19B3-40B5-828B-3EC00B2E2B6C}.Release|Any CPU.Build.0 = Release|Any CPU
{EEA758C3-2AFE-491C-B593-06143C73BAC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEA758C3-2AFE-491C-B593-06143C73BAC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEA758C3-2AFE-491C-B593-06143C73BAC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEA758C3-2AFE-491C-B593-06143C73BAC7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -268,8 +280,10 @@ Global
{83DDB126-A00B-4064-86E7-568322CA67EC} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{0C734FB2-7D75-4FF3-B564-1E50E6280B14} = {3A6B6931-A123-477A-9469-8B468B5385AF}
{5F857ABF-449B-46C7-B160-CAC1C02DB782} = {3A6B6931-A123-477A-9469-8B468B5385AF}
{AB823637-CA8C-4139-B9EC-DD29494E2276} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{79278DDF-D699-4E1F-ACB4-907F8903E350} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{1D1F4183-8E5F-454F-BCAB-B15748EFBF53} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{AD9FB3A1-19B3-40B5-828B-3EC00B2E2B6C} = {9B2AE124-6636-4DE9-83A3-70360DABD0C4}
{EEA758C3-2AFE-491C-B593-06143C73BAC7} = {3A6B6931-A123-477A-9469-8B468B5385AF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2E70565D-94CF-40B4-BFE1-AC18D5F736AB}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\DotNetCore.CAP.Dashboard\DotNetCore.CAP.Dashboard.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.GooglePubSub\DotNetCore.CAP.GooglePubSub.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.GcpPubSubSpanner\DotNetCore.CAP.GcpPubSubSpanner.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.GoogleSpanner\DotNetCore.CAP.GoogleSpanner.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP\DotNetCore.CAP.csproj" />
</ItemGroup>
Expand Down
76 changes: 76 additions & 0 deletions samples/Sample.GcpPubSub.MongoDB/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Text.Json;
using System.Threading.Tasks;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Sample.GcpPubSub.GoogleSpanner
{
[Route("api/[controller]")]
public class ValuesController : Controller, ICapSubscribe
{
public class MyObj
{
public string SingerId { get; set; }
public string FirstName { get; set; }
}

private readonly IMongoClient _client;
private readonly ICapPublisher _capBus;

public ValuesController(IMongoClient client, ICapPublisher producer)
{
_capBus = producer;
_client = client;
}

[Route("~/without/transaction")]
public async Task<IActionResult> WithoutTransaction()
{
await _capBus.PublishAsync("sample.gcppubsub.googlespanner", DateTime.Now);

return Ok();
}

[Route("~/adonet/transaction")]
public IActionResult AdonetWithTransaction()
{
var random = new Random().Next(1, 10000).ToString();
try
{
//NOTE: before your test, create "test" database and "test.collection" collection first
//The MongoDB should have replication enabled.

using (var session = _client.StartTransaction(_capBus, autoCommit: false))
{
var collection = _client.GetDatabase("test").GetCollection<BsonDocument>("test.collection");
collection.InsertOne(session, new BsonDocument {
{ "SingerId", random },
{ "FirstName", "John"+random },
{ "LastName", "Doe"+random }
});

var msg = new MyObj { SingerId = random, FirstName = "John" + random };
_capBus.Publish("sample.gcppubsub.mongodb", msg);

session.CommitTransaction();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return Ok();
}


[CapSubscribe("sample.gcppubsub.mongodb")]
public void Test2(MyObj value)
{
Console.WriteLine("Subscriber output message: " + JsonSerializer.Serialize(value));
}
}
}
20 changes: 20 additions & 0 deletions samples/Sample.GcpPubSub.MongoDB/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Sample.GcpPubSub.GoogleSpanner
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
21 changes: 21 additions & 0 deletions samples/Sample.GcpPubSub.MongoDB/Sample.GcpPubSub.MongoDB.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<WarningsAsErrors>NU1701</WarningsAsErrors>
<NoWarn>NU1701</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.7" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\DotNetCore.CAP.Dashboard\DotNetCore.CAP.Dashboard.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.GcpPubSubMongo\DotNetCore.CAP.GcpPubSubMongo.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP.MongoDB\DotNetCore.CAP.MongoDB.csproj" />
<ProjectReference Include="..\..\src\DotNetCore.CAP\DotNetCore.CAP.csproj" />
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions samples/Sample.GcpPubSub.MongoDB/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;

namespace Sample.GcpPubSub.GoogleSpanner
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IMongoClient>(new MongoClient(Configuration.GetConnectionString("MongoDB")));

services.AddCap(x =>
{
x.UseMongoDB(Configuration.GetConnectionString("MongoDB"));
x.UseGooglePubSub(cfg =>
{
cfg.ProjectId = Configuration["Pubsub:ProjectId"];
cfg.SubscriptionId = Configuration["Pubsub:SubscriptionId"];
cfg.VerificationToken = Configuration["Pubsub:VerificationToken"];
cfg.TopicId = Configuration["Pubsub:TopicId"];
});
x.UseDashboard();
});
//services.AddSingleton<INodeDiscoveryProvider>();
services.AddControllers();
}

public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
17 changes: 17 additions & 0 deletions samples/Sample.GcpPubSub.MongoDB/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Pubsub": {
"ProjectId": "<project-id>",
"VerificationToken": "<gcp-token>",
"TopicId": "<gcp-topic-id>",
"SubscriptionId": "<gcp-subscription-id>"
},
"ConnectionStrings": {
"MongoDB": "mongodb://192.168.2.120:27017,192.168.2.120:27018,192.168.2.120:27019/?replicaSet=rs0"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DotNetCore.CAP
/// <summary>
/// Provides programmatic configuration for the CAP Google Cloud Platform Pub/Sub project.
/// </summary>
public class GooglePubSubOptions
public class GcpPubSubMongoOptions
{
/// <summary>
/// The GCP <c>Project</c> ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
// ReSharper disable once CheckNamespace
namespace DotNetCore.CAP
{
internal sealed class GooglePubSubOptionsExtension : ICapOptionsExtension
internal sealed class GcpPubSubMongoOptionsExtension : ICapOptionsExtension
{
private readonly Action<GooglePubSubOptions> _configure;
private readonly Action<GcpPubSubMongoOptions> _configure;

public GooglePubSubOptionsExtension(Action<GooglePubSubOptions> configure)
public GcpPubSubMongoOptionsExtension(Action<GcpPubSubMongoOptions> configure)
{
_configure = configure;
}
Expand All @@ -24,8 +24,8 @@ public void AddServices(IServiceCollection services)

services.Configure(_configure);

services.AddSingleton<IConsumerClientFactory, GooglePubSubConsumerClientFactory>();
services.AddSingleton<ITransport,GooglePubSubTransport>();
services.AddSingleton<IConsumerClientFactory, GcpPubSubMongoConsumerClientFactory>();
services.AddSingleton<ITransport, GcpPubSubMongoTransport>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public static CapOptions UseGooglePubSub(this CapOptions options,string projectI
/// </summary>
/// <param name="options">CAP configuration options</param>
/// <param name="configure">Provides programmatic configuration for the Google Cloud Pub/Sub.</param>
public static CapOptions UseGooglePubSub(this CapOptions options, Action<GooglePubSubOptions> configure)
public static CapOptions UseGooglePubSub(this CapOptions options, Action<GcpPubSubMongoOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

options.RegisterExtension(new GooglePubSubOptionsExtension(configure));
options.RegisterExtension(new GcpPubSubMongoOptionsExtension(configure));

return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>DotNetCore.CAP.GooglePubSub</AssemblyName>
<PackageTags>$(PackageTags);PubSub;GCP;Google Cloud PubSub</PackageTags>
<AssemblyName>DotNetCore.CAP.GcpPubSubMongo</AssemblyName>
<PackageTags>$(PackageTags);PubSub;GCP;Google Cloud PubSub;Mongo</PackageTags>
</PropertyGroup>

<PropertyGroup>
<WarningsAsErrors>NU1605;NU1701</WarningsAsErrors>
<NoWarn>NU1701;CS1591</NoWarn>
<DocumentationFile>bin\$(Configuration)\netstandard2.0\DotNetCore.CAP.GooglePubSub.xml</DocumentationFile>
<DocumentationFile>bin\$(Configuration)\netstandard2.0\DotNetCore.CAP.GcpPubSubMongo.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

namespace DotNetCore.CAP.GooglePubSub
{
internal sealed class GooglePubSubConsumerClient : IConsumerClient
internal sealed class GcpPubSubMongoConsumerClient : IConsumerClient
{
private readonly ProjectName _projectName;
private readonly SubscriptionName _subscriptionName;
private readonly TopicName _topicName;
private SubscriberServiceApiClient _subscriberClient;

public GooglePubSubConsumerClient(string subscriptionName, IOptions<GooglePubSubOptions> options)
public GcpPubSubMongoConsumerClient(string subscriptionName, IOptions<GcpPubSubMongoOptions> options)
{
_projectName = new ProjectName(options.Value.ProjectId);
_subscriptionName = new SubscriptionName(options.Value.ProjectId, subscriptionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace DotNetCore.CAP.GooglePubSub
{
internal sealed class GooglePubSubConsumerClientFactory : IConsumerClientFactory
internal sealed class GcpPubSubMongoConsumerClientFactory : IConsumerClientFactory
{
private readonly IOptions<GooglePubSubOptions> _options;
private GooglePubSubConsumerClient consumerClient;
private readonly IOptions<GcpPubSubMongoOptions> _options;
private GcpPubSubMongoConsumerClient consumerClient;

public GooglePubSubConsumerClientFactory(IOptions<GooglePubSubOptions> options)
public GcpPubSubMongoConsumerClientFactory(IOptions<GcpPubSubMongoOptions> options)
{
_options = options;
}
Expand All @@ -24,7 +24,7 @@ public IConsumerClient Create(string groupId)
{
if (consumerClient == null)
{
consumerClient = new GooglePubSubConsumerClient(groupId, _options);
consumerClient = new GcpPubSubMongoConsumerClient(groupId, _options);
consumerClient.Connect();
}
return consumerClient;
Expand Down
Loading

0 comments on commit 406590f

Please sign in to comment.