-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added project for GCP PubSub and MongoDB integration
- Loading branch information
Showing
21 changed files
with
594 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
samples/Sample.GcpPubSub.MongoDB/Controllers/ValuesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
samples/Sample.GcpPubSub.MongoDB/Sample.GcpPubSub.MongoDB.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.