From 610f963f1d7257b7b460d742c057904cf8e720ba Mon Sep 17 00:00:00 2001 From: 丁松杰 <377973147@qq.com> Date: Tue, 7 Jan 2020 17:00:17 +0800 Subject: [PATCH] 添加可靠消息demo --- Pole.sln | 45 ++++++++++++++++++++++++++++++--------------- samples/apis/Backet.Api/Backet.Api.csproj | 7 +++++++ samples/apis/Backet.Api/Program.cs | 26 ++++++++++++++++++++++++++ samples/apis/Backet.Api/Properties/launchSettings.json | 27 +++++++++++++++++++++++++++ samples/apis/Backet.Api/Startup.cs | 40 ++++++++++++++++++++++++++++++++++++++++ samples/apis/Backet.Api/appsettings.Development.json | 9 +++++++++ samples/apis/Backet.Api/appsettings.json | 10 ++++++++++ samples/apis/Order.Api/Apis/OrderController.cs | 42 ++++++++++++++++++++++++++++++++++++++++++ samples/apis/Order.Api/Entity/Order.cs | 13 +++++++++++++ samples/apis/Order.Api/Entity/bank.cs | 13 +++++++++++++ samples/apis/Order.Api/Events/CreateOrderReliableEvent.cs | 13 +++++++++++++ samples/apis/Order.Api/Events/EventCallback/CreateOrderReliableEventCallback.cs | 24 ++++++++++++++++++++++++ samples/apis/Order.Api/Events/EventHandler/ToUpdateBankMoneyWhenCreateOrderReliableEventHandler.cs | 30 ++++++++++++++++++++++++++++++ samples/apis/Order.Api/Order.Api.csproj | 13 +++++++++++++ samples/apis/Order.Api/Program.cs | 27 +++++++++++++++++++++++++++ samples/apis/Order.Api/Properties/launchSettings.json | 27 +++++++++++++++++++++++++++ samples/apis/Order.Api/Repository/IBankRepository.cs | 23 +++++++++++++++++++++++ samples/apis/Order.Api/Repository/IOrderRepository.cs | 44 ++++++++++++++++++++++++++++++++++++++++++++ samples/apis/Order.Api/Startup.cs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ samples/apis/Order.Api/appsettings.Development.json | 9 +++++++++ samples/apis/Order.Api/appsettings.json | 25 +++++++++++++++++++++++++ samples/apis/Product.Api/Product.Api.csproj | 8 -------- samples/apis/Product.Api/Program.cs | 5 ++--- samples/apis/Product.Api/Properties/launchSettings.json | 19 +++++++++++++++++-- samples/apis/Product.Api/Protos/greet.proto | 21 --------------------- samples/apis/Product.Api/Services/GreeterService.cs | 26 -------------------------- samples/apis/Product.Api/Startup.cs | 7 ++----- samples/apis/Product.Api/appsettings.Development.json | 7 +++---- samples/apis/Product.Api/appsettings.json | 7 +------ src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs | 4 ++-- src/Pole.ReliableMessage/ComteckReliableMessageServiceCollectionExtensions.cs | 2 ++ 31 files changed, 548 insertions(+), 92 deletions(-) create mode 100644 samples/apis/Backet.Api/Backet.Api.csproj create mode 100644 samples/apis/Backet.Api/Program.cs create mode 100644 samples/apis/Backet.Api/Properties/launchSettings.json create mode 100644 samples/apis/Backet.Api/Startup.cs create mode 100644 samples/apis/Backet.Api/appsettings.Development.json create mode 100644 samples/apis/Backet.Api/appsettings.json create mode 100644 samples/apis/Order.Api/Apis/OrderController.cs create mode 100644 samples/apis/Order.Api/Entity/Order.cs create mode 100644 samples/apis/Order.Api/Entity/bank.cs create mode 100644 samples/apis/Order.Api/Events/CreateOrderReliableEvent.cs create mode 100644 samples/apis/Order.Api/Events/EventCallback/CreateOrderReliableEventCallback.cs create mode 100644 samples/apis/Order.Api/Events/EventHandler/ToUpdateBankMoneyWhenCreateOrderReliableEventHandler.cs create mode 100644 samples/apis/Order.Api/Order.Api.csproj create mode 100644 samples/apis/Order.Api/Program.cs create mode 100644 samples/apis/Order.Api/Properties/launchSettings.json create mode 100644 samples/apis/Order.Api/Repository/IBankRepository.cs create mode 100644 samples/apis/Order.Api/Repository/IOrderRepository.cs create mode 100644 samples/apis/Order.Api/Startup.cs create mode 100644 samples/apis/Order.Api/appsettings.Development.json create mode 100644 samples/apis/Order.Api/appsettings.json delete mode 100644 samples/apis/Product.Api/Protos/greet.proto delete mode 100644 samples/apis/Product.Api/Services/GreeterService.cs diff --git a/Pole.sln b/Pole.sln index 1621bad..58e8af3 100644 --- a/Pole.sln +++ b/Pole.sln @@ -9,35 +9,37 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{655E719B-4 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.Core", "src\Pole.Core\Pole.Core.csproj", "{CA80F6EF-95A0-4BB7-BA8B-02E167E82865}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.Application", "src\Pole.Application\Pole.Application.csproj", "{C7825E5B-4FB0-4498-B8D1-E9EC0BC1AA5C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.Application", "src\Pole.Application\Pole.Application.csproj", "{C7825E5B-4FB0-4498-B8D1-E9EC0BC1AA5C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.Domain", "src\Pole.Domain\Pole.Domain.csproj", "{6F6DBA49-4274-4C62-BBE7-91FCC5B77989}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.Domain", "src\Pole.Domain\Pole.Domain.csproj", "{6F6DBA49-4274-4C62-BBE7-91FCC5B77989}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.Domain.EntityframeworkCore", "src\Pole.Domain.EntityframeworkCore\Pole.Domain.EntityframeworkCore.csproj", "{1C26BE3A-CBEA-47D1-97A0-6DB4F41DFF5A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.Domain.EntityframeworkCore", "src\Pole.Domain.EntityframeworkCore\Pole.Domain.EntityframeworkCore.csproj", "{1C26BE3A-CBEA-47D1-97A0-6DB4F41DFF5A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.Grpc", "src\Pole.Grpc\Pole.Grpc.csproj", "{F40FE25F-6081-4B29-A7BD-CB5C24F6FDA8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.Grpc", "src\Pole.Grpc\Pole.Grpc.csproj", "{F40FE25F-6081-4B29-A7BD-CB5C24F6FDA8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{4A0FB696-EC29-4A5F-B40B-A6FC56001ADB}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "apis", "apis", "{475116FC-DEEC-4255-94E4-AE7B8C85038D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.ReliableMessage", "src\Pole.ReliableMessage\Pole.ReliableMessage.csproj", "{699C75AB-4814-4E16-A3F3-9735C4C609FE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.ReliableMessage", "src\Pole.ReliableMessage\Pole.ReliableMessage.csproj", "{699C75AB-4814-4E16-A3F3-9735C4C609FE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.ReliableMessage.Masstransit", "src\Pole.ReliableMessage.Masstransit\Pole.ReliableMessage.Masstransit.csproj", "{051BECA5-5E65-4FCB-9B7F-C9E64809E218}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.ReliableMessage.Masstransit", "src\Pole.ReliableMessage.Masstransit\Pole.ReliableMessage.Masstransit.csproj", "{051BECA5-5E65-4FCB-9B7F-C9E64809E218}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.ReliableMessage.Dashboard", "src\Pole.ReliableMessage.Dashboard\Pole.ReliableMessage.Dashboard.csproj", "{9EF13D70-C3AF-471A-8AA6-603338B194B7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.ReliableMessage.Dashboard", "src\Pole.ReliableMessage.Dashboard\Pole.ReliableMessage.Dashboard.csproj", "{9EF13D70-C3AF-471A-8AA6-603338B194B7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.ReliableMessage.Storage.Abstraction", "src\Pole.ReliableMessage.Storage.Abstraction\Pole.ReliableMessage.Storage.Abstraction.csproj", "{3D92F460-350B-4614-A6F6-C00A2D0FA9E2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.ReliableMessage.Storage.Abstraction", "src\Pole.ReliableMessage.Storage.Abstraction\Pole.ReliableMessage.Storage.Abstraction.csproj", "{3D92F460-350B-4614-A6F6-C00A2D0FA9E2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.ReliableMessage.Storage.Mongodb", "src\Pole.ReliableMessage.Storage.Mongodb\Pole.ReliableMessage.Storage.Mongodb.csproj", "{793C73C6-93DE-4A56-B979-137914B247F2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.ReliableMessage.Storage.Mongodb", "src\Pole.ReliableMessage.Storage.Mongodb\Pole.ReliableMessage.Storage.Mongodb.csproj", "{793C73C6-93DE-4A56-B979-137914B247F2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pole.ReliableMessage.Storage.EntityframeworkCore", "src\Pole.ReliableMessage.Storage.EntityframeworkCore\Pole.ReliableMessage.Storage.EntityframeworkCore.csproj", "{805CF4F7-CCDC-4390-A92B-55E0FFA7F659}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pole.ReliableMessage.Storage.EntityframeworkCore", "src\Pole.ReliableMessage.Storage.EntityframeworkCore\Pole.ReliableMessage.Storage.EntityframeworkCore.csproj", "{805CF4F7-CCDC-4390-A92B-55E0FFA7F659}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Grpc", "Grpc", "{629045E7-B047-452A-AADA-ACB455B4FAFD}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "webs", "webs", "{452B9D9E-881E-4E0E-A90B-98F2253F20F1}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Rest", "Rest", "{BA122337-74D2-4439-A10E-B20E14881290}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Order.Api", "samples\apis\Order.Api\Order.Api.csproj", "{098DF771-6DC6-45D4-ABFA-FF84E8F7750B}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "webs", "webs", "{452B9D9E-881E-4E0E-A90B-98F2253F20F1}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Product.Api", "samples\apis\Product.Api\Product.Api.csproj", "{125B1E4B-B1C1-4F85-9C6A-38815960E654}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backet.Api", "samples\apis\Backet.Api\Backet.Api.csproj", "{C961F25C-1C11-4855-84E4-ADABE96451E7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -89,6 +91,18 @@ Global {805CF4F7-CCDC-4390-A92B-55E0FFA7F659}.Debug|Any CPU.Build.0 = Debug|Any CPU {805CF4F7-CCDC-4390-A92B-55E0FFA7F659}.Release|Any CPU.ActiveCfg = Release|Any CPU {805CF4F7-CCDC-4390-A92B-55E0FFA7F659}.Release|Any CPU.Build.0 = Release|Any CPU + {098DF771-6DC6-45D4-ABFA-FF84E8F7750B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {098DF771-6DC6-45D4-ABFA-FF84E8F7750B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {098DF771-6DC6-45D4-ABFA-FF84E8F7750B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {098DF771-6DC6-45D4-ABFA-FF84E8F7750B}.Release|Any CPU.Build.0 = Release|Any CPU + {125B1E4B-B1C1-4F85-9C6A-38815960E654}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {125B1E4B-B1C1-4F85-9C6A-38815960E654}.Debug|Any CPU.Build.0 = Debug|Any CPU + {125B1E4B-B1C1-4F85-9C6A-38815960E654}.Release|Any CPU.ActiveCfg = Release|Any CPU + {125B1E4B-B1C1-4F85-9C6A-38815960E654}.Release|Any CPU.Build.0 = Release|Any CPU + {C961F25C-1C11-4855-84E4-ADABE96451E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C961F25C-1C11-4855-84E4-ADABE96451E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C961F25C-1C11-4855-84E4-ADABE96451E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C961F25C-1C11-4855-84E4-ADABE96451E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -106,9 +120,10 @@ Global {3D92F460-350B-4614-A6F6-C00A2D0FA9E2} = {9932C965-8B38-4F70-9E43-86DC56860E2B} {793C73C6-93DE-4A56-B979-137914B247F2} = {9932C965-8B38-4F70-9E43-86DC56860E2B} {805CF4F7-CCDC-4390-A92B-55E0FFA7F659} = {9932C965-8B38-4F70-9E43-86DC56860E2B} - {629045E7-B047-452A-AADA-ACB455B4FAFD} = {475116FC-DEEC-4255-94E4-AE7B8C85038D} - {BA122337-74D2-4439-A10E-B20E14881290} = {475116FC-DEEC-4255-94E4-AE7B8C85038D} {452B9D9E-881E-4E0E-A90B-98F2253F20F1} = {4A0FB696-EC29-4A5F-B40B-A6FC56001ADB} + {098DF771-6DC6-45D4-ABFA-FF84E8F7750B} = {475116FC-DEEC-4255-94E4-AE7B8C85038D} + {125B1E4B-B1C1-4F85-9C6A-38815960E654} = {475116FC-DEEC-4255-94E4-AE7B8C85038D} + {C961F25C-1C11-4855-84E4-ADABE96451E7} = {475116FC-DEEC-4255-94E4-AE7B8C85038D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DB0775A3-F293-4043-ADB7-72BAC081E87E} diff --git a/samples/apis/Backet.Api/Backet.Api.csproj b/samples/apis/Backet.Api/Backet.Api.csproj new file mode 100644 index 0000000..92605c5 --- /dev/null +++ b/samples/apis/Backet.Api/Backet.Api.csproj @@ -0,0 +1,7 @@ + + + + netcoreapp3.1 + + + diff --git a/samples/apis/Backet.Api/Program.cs b/samples/apis/Backet.Api/Program.cs new file mode 100644 index 0000000..3bcea34 --- /dev/null +++ b/samples/apis/Backet.Api/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Backet.Api +{ + 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(); + }); + } +} diff --git a/samples/apis/Backet.Api/Properties/launchSettings.json b/samples/apis/Backet.Api/Properties/launchSettings.json new file mode 100644 index 0000000..937f6c5 --- /dev/null +++ b/samples/apis/Backet.Api/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:21703", + "sslPort": 44369 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Backet.Api": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/apis/Backet.Api/Startup.cs b/samples/apis/Backet.Api/Startup.cs new file mode 100644 index 0000000..7b166b5 --- /dev/null +++ b/samples/apis/Backet.Api/Startup.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Backet.Api +{ + public class Startup + { + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices(IServiceCollection services) + { + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapGet("/", async context => + { + await context.Response.WriteAsync("Hello World!"); + }); + }); + } + } +} diff --git a/samples/apis/Backet.Api/appsettings.Development.json b/samples/apis/Backet.Api/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/samples/apis/Backet.Api/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/apis/Backet.Api/appsettings.json b/samples/apis/Backet.Api/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/samples/apis/Backet.Api/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/samples/apis/Order.Api/Apis/OrderController.cs b/samples/apis/Order.Api/Apis/OrderController.cs new file mode 100644 index 0000000..68ffc92 --- /dev/null +++ b/samples/apis/Order.Api/Apis/OrderController.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Mvc; +using Order.Api.Events; +using Pole.ReliableMessage.Abstraction; +using ServiceA.Repository; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Order.Api.Apis +{ + [Route("api/[controller]")] + [ApiController] + public class OrderController : ControllerBase + { + private readonly IReliableBus _reliableBus; + private readonly IOrderRepository _orderRepository; + public OrderController(IReliableBus reliableBus, IOrderRepository orderRepository) + { + _reliableBus = reliableBus; + _orderRepository = orderRepository; + } + [HttpGet] + public async Task Index() + { + var order = new Order.Api.Entity.Order + { + Id = 1, + Price = 10 + }; + CreateOrderReliableEvent createOrderReliableEvent = new CreateOrderReliableEvent + { + OrderPrice = order.Price + }; + var prePublishId = await _reliableBus.PrePublish(createOrderReliableEvent, order.Id); + _orderRepository.Add(order); + + await _reliableBus.Publish(createOrderReliableEvent, prePublishId); + return "ok"; + } + } +} \ No newline at end of file diff --git a/samples/apis/Order.Api/Entity/Order.cs b/samples/apis/Order.Api/Entity/Order.cs new file mode 100644 index 0000000..6123f45 --- /dev/null +++ b/samples/apis/Order.Api/Entity/Order.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Order.Api.Entity +{ + public class Order + { + public int Id { get; set; } + public decimal Price { get; set; } + } +} diff --git a/samples/apis/Order.Api/Entity/bank.cs b/samples/apis/Order.Api/Entity/bank.cs new file mode 100644 index 0000000..90feff6 --- /dev/null +++ b/samples/apis/Order.Api/Entity/bank.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Order.Api.Entity +{ + public class Bank + { + public int Id { get; set; } + public decimal Money { get; set; } + } +} diff --git a/samples/apis/Order.Api/Events/CreateOrderReliableEvent.cs b/samples/apis/Order.Api/Events/CreateOrderReliableEvent.cs new file mode 100644 index 0000000..6a3f743 --- /dev/null +++ b/samples/apis/Order.Api/Events/CreateOrderReliableEvent.cs @@ -0,0 +1,13 @@ +using ServiceA.Repository; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Order.Api.Events +{ + public class CreateOrderReliableEvent + { + public decimal OrderPrice { get; set; } + } +} diff --git a/samples/apis/Order.Api/Events/EventCallback/CreateOrderReliableEventCallback.cs b/samples/apis/Order.Api/Events/EventCallback/CreateOrderReliableEventCallback.cs new file mode 100644 index 0000000..b82864c --- /dev/null +++ b/samples/apis/Order.Api/Events/EventCallback/CreateOrderReliableEventCallback.cs @@ -0,0 +1,24 @@ +using Pole.ReliableMessage.Abstraction; +using ServiceA.Repository; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Order.Api.Events.EventCallback +{ + public class CreateOrderReliableEventCallback : IReliableEventCallback + { + private readonly IOrderRepository _orderRepository; + public CreateOrderReliableEventCallback(IOrderRepository orderRepository) + { + _orderRepository = orderRepository; + } + public Task Callback(int callbackParemeter) + { + var order = _orderRepository.Get(callbackParemeter); + //return Task.FromResult(order != null); + return Task.FromResult(true); + } + } +} diff --git a/samples/apis/Order.Api/Events/EventHandler/ToUpdateBankMoneyWhenCreateOrderReliableEventHandler.cs b/samples/apis/Order.Api/Events/EventHandler/ToUpdateBankMoneyWhenCreateOrderReliableEventHandler.cs new file mode 100644 index 0000000..7d926c3 --- /dev/null +++ b/samples/apis/Order.Api/Events/EventHandler/ToUpdateBankMoneyWhenCreateOrderReliableEventHandler.cs @@ -0,0 +1,30 @@ +using Order.Api.Events; +using Pole.ReliableMessage.Abstraction; +using Pole.ReliableMessage.Masstransit; +using ServiceA.Repository; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ServiceA.Events.EventHandler +{ + [ReliableEventHandlerParemeter(PrefetchCount =16)] + public class ToUpdateBankMoneyWhenCreateOrderReliableEventHandler : ReliableEventHandler + { + private readonly IBankRepository _bankRepository; + public ToUpdateBankMoneyWhenCreateOrderReliableEventHandler(IServiceProvider serviceProvider, IBankRepository bankRepository) : base(serviceProvider) + { + _bankRepository = bankRepository; + } + + public override async Task Handle(IReliableEventHandlerContext context) + { + await Task.CompletedTask; + var @event = context.Event; + + //throw new Exception("test"); + _bankRepository.AddMoney(@event.OrderPrice); + } + } +} diff --git a/samples/apis/Order.Api/Order.Api.csproj b/samples/apis/Order.Api/Order.Api.csproj new file mode 100644 index 0000000..2a452c7 --- /dev/null +++ b/samples/apis/Order.Api/Order.Api.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp3.1 + + + + + + + + + diff --git a/samples/apis/Order.Api/Program.cs b/samples/apis/Order.Api/Program.cs new file mode 100644 index 0000000..ce04e34 --- /dev/null +++ b/samples/apis/Order.Api/Program.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using ServiceA; + +namespace Order.Api +{ + 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(); + }); + } +} diff --git a/samples/apis/Order.Api/Properties/launchSettings.json b/samples/apis/Order.Api/Properties/launchSettings.json new file mode 100644 index 0000000..ff39c8d --- /dev/null +++ b/samples/apis/Order.Api/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:21490", + "sslPort": 44390 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Order.Api": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/apis/Order.Api/Repository/IBankRepository.cs b/samples/apis/Order.Api/Repository/IBankRepository.cs new file mode 100644 index 0000000..0b6f932 --- /dev/null +++ b/samples/apis/Order.Api/Repository/IBankRepository.cs @@ -0,0 +1,23 @@ +using Order.Api.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ServiceA.Repository +{ + public interface IBankRepository + { + bool AddMoney(decimal money); + } + public class BankRepository : IBankRepository + { + private static Bank Bank = new Bank { Id = 1, Money = 0 }; + + public bool AddMoney(decimal money) + { + Bank.Money += money; + return true; + } + } +} diff --git a/samples/apis/Order.Api/Repository/IOrderRepository.cs b/samples/apis/Order.Api/Repository/IOrderRepository.cs new file mode 100644 index 0000000..2a37c06 --- /dev/null +++ b/samples/apis/Order.Api/Repository/IOrderRepository.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ServiceA.Repository +{ + public interface IOrderRepository + { + Order.Api.Entity.Order Get(int id); + bool Add(Order.Api.Entity.Order order); + bool Save(Order.Api.Entity.Order order); + } + class OrderRepository : IOrderRepository + { + private static ConcurrentBag Orders = new ConcurrentBag(); + public OrderRepository() + { + Orders.Add(new Order.Api.Entity.Order { Id = 1, Price = 12 }); + } + public Order.Api.Entity.Order Get(int id) + { + return Orders.FirstOrDefault(m => m.Id == id); + } + + public bool Add(Order.Api.Entity.Order order) + { + Orders.Add(order); + return true; + } + + public bool Save(Order.Api.Entity.Order order) + { + var currentOrder = Orders.FirstOrDefault(m => m.Id == order.Id); + if (currentOrder == null) + { + return false; + } + currentOrder.Price = order.Price; + return true; + } + } +} diff --git a/samples/apis/Order.Api/Startup.cs b/samples/apis/Order.Api/Startup.cs new file mode 100644 index 0000000..02aa01f --- /dev/null +++ b/samples/apis/Order.Api/Startup.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Pole.ReliableMessage.Storage.Mongodb; +using ServiceA.Repository; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace ServiceA +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + + services.AddComteckReliableMessage(option => + { + option.AddMasstransitRabbitmq(rabbitoption => + { + rabbitoption.RabbitMqHostAddress = Configuration["RabbitmqConfig:HostAddress"]; + rabbitoption.RabbitMqHostUserName = Configuration["RabbitmqConfig:HostUserName"]; + rabbitoption.RabbitMqHostPassword = Configuration["RabbitmqConfig:HostPassword"]; + rabbitoption.QueueNamePrefix = Configuration["ServiceName"]; + }); + option.AddMongodb(mongodbOption => + { + mongodbOption.ServiceCollectionName = Configuration["ServiceName"]; + mongodbOption.Servers = Configuration.GetSection("MongoConfig:Servers").Get(); + }); + option.AddEventAssemblies(typeof(Startup).Assembly) + .AddEventHandlerAssemblies(typeof(Startup).Assembly); + option.NetworkInterfaceGatewayAddress = Configuration["ReliableMessageOption:NetworkInterfaceGatewayAddress"]; + }); + + services.AddScoped(); + services.AddScoped(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/samples/apis/Order.Api/appsettings.Development.json b/samples/apis/Order.Api/appsettings.Development.json new file mode 100644 index 0000000..4f30a00 --- /dev/null +++ b/samples/apis/Order.Api/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/apis/Order.Api/appsettings.json b/samples/apis/Order.Api/appsettings.json new file mode 100644 index 0000000..3faf58f --- /dev/null +++ b/samples/apis/Order.Api/appsettings.json @@ -0,0 +1,25 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning", + "Pole.ReliableMessage.Processor.MessageCleanProcessor": "Information" + } + }, + "ServiceName": "ServiceA", + "RabbitmqConfig": { + "HostAddress": "rabbitmq://192.168.0.248/", + "HostUserName": "comteck", + "HostPassword": "comteck3030" + }, + "MongoConfig": { + "Servers": [ + { + "Host": "192.168.0.248", + "Port": "27017" + } + ] + }, + "ReliableMessageOption": { + "NetworkInterfaceGatewayAddress": "192.168.0.1" + } +} diff --git a/samples/apis/Product.Api/Product.Api.csproj b/samples/apis/Product.Api/Product.Api.csproj index 3d293a8..92605c5 100644 --- a/samples/apis/Product.Api/Product.Api.csproj +++ b/samples/apis/Product.Api/Product.Api.csproj @@ -4,12 +4,4 @@ netcoreapp3.1 - - - - - - - - diff --git a/samples/apis/Product.Api/Program.cs b/samples/apis/Product.Api/Program.cs index 24b5f80..77cdfbb 100644 --- a/samples/apis/Product.Api/Program.cs +++ b/samples/apis/Product.Api/Program.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; namespace Product.Api { @@ -15,8 +16,6 @@ namespace Product.Api CreateHostBuilder(args).Build().Run(); } - // Additional configuration is required to successfully run gRPC on macOS. - // For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => diff --git a/samples/apis/Product.Api/Properties/launchSettings.json b/samples/apis/Product.Api/Properties/launchSettings.json index 0e1df79..bf0fa92 100644 --- a/samples/apis/Product.Api/Properties/launchSettings.json +++ b/samples/apis/Product.Api/Properties/launchSettings.json @@ -1,9 +1,24 @@ { + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:21635", + "sslPort": 0 + } + }, "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, "Product.Api": { "commandName": "Project", - "launchBrowser": false, - "applicationUrl": "https://localhost:5001", + "launchBrowser": true, + "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/samples/apis/Product.Api/Protos/greet.proto b/samples/apis/Product.Api/Protos/greet.proto deleted file mode 100644 index 26b73fb..0000000 --- a/samples/apis/Product.Api/Protos/greet.proto +++ /dev/null @@ -1,21 +0,0 @@ -syntax = "proto3"; - -option csharp_namespace = "Product.Api"; - -package greet; - -// The greeting service definition. -service Greeter { - // Sends a greeting - rpc SayHello (HelloRequest) returns (HelloReply); -} - -// The request message containing the user's name. -message HelloRequest { - string name = 1; -} - -// The response message containing the greetings. -message HelloReply { - string message = 1; -} diff --git a/samples/apis/Product.Api/Services/GreeterService.cs b/samples/apis/Product.Api/Services/GreeterService.cs deleted file mode 100644 index 9c4361f..0000000 --- a/samples/apis/Product.Api/Services/GreeterService.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Grpc.Core; -using Microsoft.Extensions.Logging; - -namespace Product.Api -{ - public class GreeterService : Greeter.GreeterBase - { - private readonly ILogger _logger; - public GreeterService(ILogger logger) - { - _logger = logger; - } - - public override Task SayHello(HelloRequest request, ServerCallContext context) - { - return Task.FromResult(new HelloReply - { - Message = "Hello " + request.Name - }); - } - } -} diff --git a/samples/apis/Product.Api/Startup.cs b/samples/apis/Product.Api/Startup.cs index 35d902b..80cb1f7 100644 --- a/samples/apis/Product.Api/Startup.cs +++ b/samples/apis/Product.Api/Startup.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -16,7 +16,6 @@ namespace Product.Api // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { - services.AddGrpc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -31,11 +30,9 @@ namespace Product.Api app.UseEndpoints(endpoints => { - endpoints.MapGrpcService(); - endpoints.MapGet("/", async context => { - await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); + await context.Response.WriteAsync("Hello World!"); }); }); } diff --git a/samples/apis/Product.Api/appsettings.Development.json b/samples/apis/Product.Api/appsettings.Development.json index fe20c40..8983e0f 100644 --- a/samples/apis/Product.Api/appsettings.Development.json +++ b/samples/apis/Product.Api/appsettings.Development.json @@ -1,10 +1,9 @@ { "Logging": { "LogLevel": { - "Default": "Debug", - "System": "Information", - "Grpc": "Information", - "Microsoft": "Information" + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } } } diff --git a/samples/apis/Product.Api/appsettings.json b/samples/apis/Product.Api/appsettings.json index 1f29241..d9d9a9b 100644 --- a/samples/apis/Product.Api/appsettings.json +++ b/samples/apis/Product.Api/appsettings.json @@ -6,10 +6,5 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*", - "Kestrel": { - "EndpointDefaults": { - "Protocols": "Http2" - } - } + "AllowedHosts": "*" } diff --git a/src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs b/src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs index ab955d1..34e3678 100644 --- a/src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs +++ b/src/Pole.ReliableMessage.Masstransit/MasstransitRabbitmqOption.cs @@ -14,9 +14,9 @@ namespace Pole.ReliableMessage.Masstransit public string RabbitMqHostPassword { get; set; } public string QueueNamePrefix { get; set; } = string.Empty; /// - /// 4 个并发 + /// 2 个并发 /// - public ushort PrefetchCount { get; set; } = 4; + public ushort PrefetchCount { get; set; } = 2; public Action RetryConfigure { get; set; } = r => r.Intervals(TimeSpan.FromSeconds(0.1) diff --git a/src/Pole.ReliableMessage/ComteckReliableMessageServiceCollectionExtensions.cs b/src/Pole.ReliableMessage/ComteckReliableMessageServiceCollectionExtensions.cs index fa9bcee..a10f5d7 100644 --- a/src/Pole.ReliableMessage/ComteckReliableMessageServiceCollectionExtensions.cs +++ b/src/Pole.ReliableMessage/ComteckReliableMessageServiceCollectionExtensions.cs @@ -28,6 +28,8 @@ namespace Microsoft.Extensions.DependencyInjection services.Configure(optionConfig); services.AddSingleton(); + //services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); -- libgit2 0.25.0