diff --git a/samples/apis/SagasServer/Program.cs b/samples/apis/SagasServer/Program.cs index 61c1b67..2bec419 100644 --- a/samples/apis/SagasServer/Program.cs +++ b/samples/apis/SagasServer/Program.cs @@ -19,6 +19,13 @@ namespace SagasServer public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) - .UseStartup(); + .UseStartup() + .UseKestrel(option => + { + option.ListenAnyIP(80, config => + { + config.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2; + }); + }); } } diff --git a/samples/apis/SagasServer/Properties/launchSettings.json b/samples/apis/SagasServer/Properties/launchSettings.json index 68ff505..895db6d 100644 --- a/samples/apis/SagasServer/Properties/launchSettings.json +++ b/samples/apis/SagasServer/Properties/launchSettings.json @@ -18,7 +18,7 @@ "SagasServer": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/samples/apis/SagasServer/SagasServer.csproj b/samples/apis/SagasServer/SagasServer.csproj index d0095d1..26da4d1 100644 --- a/samples/apis/SagasServer/SagasServer.csproj +++ b/samples/apis/SagasServer/SagasServer.csproj @@ -6,10 +6,8 @@ - - - + diff --git a/samples/apis/SagasServer/Startup.cs b/samples/apis/SagasServer/Startup.cs index f42a2e6..df281de 100644 --- a/samples/apis/SagasServer/Startup.cs +++ b/samples/apis/SagasServer/Startup.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -12,11 +13,25 @@ namespace SagasServer { public class Startup { + private IConfiguration Configuration { get; } + private IWebHostEnvironment Environment { get; } + public Startup(IConfiguration configuration, IWebHostEnvironment env) + { + Configuration = configuration; + Environment = env; + } // 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) { - services.AddPoleSagasServer(); + services.AddPole(config => + { + config.AddSagasServer(); + config.AddSagasServerPGStorage(option => + { + option.ConnectionString = Configuration["postgres:write"]; + }); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/samples/apis/SagasServer/appsettings.json b/samples/apis/SagasServer/appsettings.json index def9159..2f7154d 100644 --- a/samples/apis/SagasServer/appsettings.json +++ b/samples/apis/SagasServer/appsettings.json @@ -4,5 +4,8 @@ "Default": "Warning" } }, + "postgres": { + "write": "Server=192.168.0.248;Port=5432;Username=postgres;Password=comteck2020!@#;Database=Pole-Sagas;Enlist=True;Timeout=0;Command Timeout=600;MinPoolSize=20;MaxPoolSize=500;" + }, "AllowedHosts": "*" } diff --git a/samples/apis/SagasTest.Api/Domain/AggregatesModels/BacketAggregate/Backet.cs b/samples/apis/SagasTest.Api/Domain/AggregatesModels/BacketAggregate/Backet.cs deleted file mode 100644 index b6596a6..0000000 --- a/samples/apis/SagasTest.Api/Domain/AggregatesModels/BacketAggregate/Backet.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Pole.Core.Domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Backet.Api.Domain.AggregatesModel.BacketAggregate -{ - public class Backet : Entity, IAggregateRoot - { - public void AddBacketItem(string productId, string productName, long Price) - { - BacketItem backetItem = new BacketItem() - { - Id = Guid.NewGuid().ToString("N"), - Price = Price, - ProductId = productId, - ProductName = productName - }; - BacketItems.Add(backetItem); - SetBacketTotalPrice(); - } - public void ModifyItemProductId(string productId) - { - BacketItems.ForEach(m => m.ProductId = productId); - } - private void SetBacketTotalPrice() - { - foreach (var item in BacketItems) - { - TotalPrice = BacketItems.Sum(m=>m.Price); - } - } - public string UserId { get; set; } - public List BacketItems { get; private set; } = new List(); - public long TotalPrice { get; set; } - - internal void RemoveFirstItem() - { - var first = BacketItems.FirstOrDefault(); - if (first != null) - { - BacketItems.Remove(first); - SetBacketTotalPrice(); - } - } - } -} diff --git a/samples/apis/SagasTest.Api/Domain/AggregatesModels/BacketAggregate/BacketItem.cs b/samples/apis/SagasTest.Api/Domain/AggregatesModels/BacketAggregate/BacketItem.cs deleted file mode 100644 index d86a6fb..0000000 --- a/samples/apis/SagasTest.Api/Domain/AggregatesModels/BacketAggregate/BacketItem.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Pole.Core.Domain; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Backet.Api.Domain.AggregatesModel.BacketAggregate -{ - public class BacketItem : Entity - { - public string ProductId { get; set; } - public string ProductName { get; set; } - public long Price { get; set; } - public string BacketId { get; set; } - } -} diff --git a/samples/apis/SagasTest.Api/Infrastructure/BacketDbContext.cs b/samples/apis/SagasTest.Api/Infrastructure/BacketDbContext.cs deleted file mode 100644 index c492348..0000000 --- a/samples/apis/SagasTest.Api/Infrastructure/BacketDbContext.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Backet.Api.Domain.AggregatesModel.BacketAggregate; -using Backet.Api.Infrastructure.EntityConfigurations; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Backet.Api.Infrastructure -{ - public class BacketDbContext : DbContext - { - public BacketDbContext(DbContextOptions options) : base(options) - { - - } - public DbSet Backets { get; set; } - public DbSet BacketItems { get; set; } - protected override void OnModelCreating(ModelBuilder builder) - { - base.OnModelCreating(builder); - - builder.ApplyConfiguration(new BacketItemEntityTypeConfiguration()); - builder.ApplyConfiguration(new BacketEntityTypeConfiguration()); - } - } -} diff --git a/samples/apis/SagasTest.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs b/samples/apis/SagasTest.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs deleted file mode 100644 index 8db1f30..0000000 --- a/samples/apis/SagasTest.Api/Infrastructure/EntityConfigurations/BacketEntityTypeConfiguration.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Backet.Api.Infrastructure.EntityConfigurations -{ - public class BacketEntityTypeConfiguration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable(nameof(Backet.Api.Domain.AggregatesModel.BacketAggregate.Backet)); - - builder.Property(m => m.Id).HasMaxLength(32); - builder.Property(m => m.UserId).HasMaxLength(32).IsRequired(); - builder.HasMany(m => m.BacketItems).WithOne().IsRequired().OnDelete(DeleteBehavior.Cascade).HasForeignKey("BacketId"); - - builder.Ignore(m => m.DomainEvents); - builder.Ignore(m => m.IsPersisted); - - builder.HasKey(m => m.Id); - builder.HasIndex(m => m.UserId); - } - } -} diff --git a/samples/apis/SagasTest.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs b/samples/apis/SagasTest.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs deleted file mode 100644 index 0cce173..0000000 --- a/samples/apis/SagasTest.Api/Infrastructure/EntityConfigurations/BacketItemEntityTypeConfiguration.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Backet.Api.Domain.AggregatesModel.BacketAggregate; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Backet.Api.Infrastructure.EntityConfigurations -{ - public class BacketItemEntityTypeConfiguration : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable(nameof(BacketItem)); - - builder.Property(m => m.Id).HasMaxLength(32); - builder.Property(m => m.ProductId).HasMaxLength(32); - builder.Property(m => m.ProductName).HasMaxLength(256).IsRequired(); - builder.Property(m => m.BacketId).HasMaxLength(32).IsRequired(); - - builder.Ignore(m => m.DomainEvents); - builder.Ignore(m => m.IsPersisted); - - builder.HasKey(m => m.Id); - builder.HasIndex(m => m.ProductId); - } - } -} diff --git a/samples/apis/SagasTest.Api/SagasTest.Api.csproj b/samples/apis/SagasTest.Api/SagasTest.Api.csproj index 89d53e8..8610f91 100644 --- a/samples/apis/SagasTest.Api/SagasTest.Api.csproj +++ b/samples/apis/SagasTest.Api/SagasTest.Api.csproj @@ -8,8 +8,8 @@ - + diff --git a/samples/apis/SagasTest.Api/Startup.cs b/samples/apis/SagasTest.Api/Startup.cs index 088635c..531b433 100644 --- a/samples/apis/SagasTest.Api/Startup.cs +++ b/samples/apis/SagasTest.Api/Startup.cs @@ -2,12 +2,10 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Backet.Api.Infrastructure; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -29,19 +27,12 @@ namespace SagasTest.Api // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddDbContextPool(options => options.UseNpgsql(Configuration["postgres:write"])); services.AddControllers(); services.AddPole(config => { - //config.AddRabbitMQ(option => - //{ - // option.Hosts = new string[1] { Configuration["RabbitmqConfig:HostAddress"] }; - // option.Password = Configuration["RabbitmqConfig:HostPassword"]; - // option.UserName = Configuration["RabbitmqConfig:HostUserName"]; - //}); - //config.AddEntityFrameworkEventStorage(); config.AddSagas(option=> { option.ServiceName = "SagasTest"; + option.SagasServerHost = "http://localhost:80"; }); }); services.AddHttpClient(); diff --git a/src/Pole.Sagas.Client/NotEndedSagasCompensateRetryBackgroundService.cs b/src/Pole.Sagas.Client/NotEndedSagasCompensateRetryBackgroundService.cs index b1f2e9b..28bef1a 100644 --- a/src/Pole.Sagas.Client/NotEndedSagasCompensateRetryBackgroundService.cs +++ b/src/Pole.Sagas.Client/NotEndedSagasCompensateRetryBackgroundService.cs @@ -24,7 +24,7 @@ namespace Pole.Sagas.Client private readonly SagaRestorer sagaRestorer; private readonly IEventSender eventSender; private readonly ILogger logger; - public NotEndedSagasCompensateRetryBackgroundService(IOptions options, SagaClient sagaClient, IServiceProvider serviceProvider, IEventSender eventSender, ILogger logger) + public NotEndedSagasCompensateRetryBackgroundService(IOptions options, SagaClient sagaClient, IServiceProvider serviceProvider, IEventSender eventSender, ILogger logger) { this.options = options.Value; this.sagaClient = sagaClient; @@ -111,7 +111,7 @@ namespace Pole.Sagas.Client public Task StopAsync(CancellationToken cancellationToken) { - throw new NotImplementedException(); + return Task.CompletedTask; } } } diff --git a/src/Pole.Sagas.Client/PoleSagaServiceCollectionExtensions.cs b/src/Pole.Sagas.Client/PoleSagaServiceCollectionExtensions.cs index 61964d0..7a5d741 100644 --- a/src/Pole.Sagas.Client/PoleSagaServiceCollectionExtensions.cs +++ b/src/Pole.Sagas.Client/PoleSagaServiceCollectionExtensions.cs @@ -20,10 +20,13 @@ namespace Microsoft.Extensions.DependencyInjection { public static void AddSagas(this StartupConfig startupOption, Action configAction) { + // 让客户端支持 没有TLS 的 grpc call + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); startupOption.Services.Configure(configAction); startupOption.Services.AddSingleton(); startupOption.Services.AddSingleton(); startupOption.Services.AddSingleton(); + startupOption.Services.AddHostedService(); PoleSagasOption sagasOption = null; using (var provider = startupOption.Services.BuildServiceProvider()) { diff --git a/src/Pole.Sagas.Server/PoleSagasServerOption.cs b/src/Pole.Sagas.Server/PoleSagasServerOption.cs index 6c1efe6..84286a1 100644 --- a/src/Pole.Sagas.Server/PoleSagasServerOption.cs +++ b/src/Pole.Sagas.Server/PoleSagasServerOption.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Text; @@ -26,5 +27,6 @@ namespace Pole.Sagas.Server /// 批量删除时 实际过期的数量比预定数量要大时,会分多次删除,此值为其中每次分批删除的时间间隔 /// public int ExpiredDataPreBulkDeleteDelaySeconds { get; set; } = 3; + } } diff --git a/src/Pole.Sagas.Server/PoleSagasServerServiceCollectionExtensions.cs b/src/Pole.Sagas.Server/PoleSagasServerServiceCollectionExtensions.cs index 2bd53a0..8a2d321 100644 --- a/src/Pole.Sagas.Server/PoleSagasServerServiceCollectionExtensions.cs +++ b/src/Pole.Sagas.Server/PoleSagasServerServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Pole.Core; using Pole.Core.Processor; using Pole.Sagas.Server; using Pole.Sagas.Server.Processor; @@ -10,17 +11,17 @@ namespace Microsoft.Extensions.DependencyInjection { public static class PoleSagasServerServiceCollectionExtensions { - public static IServiceCollection AddPoleSagasServer(this IServiceCollection services, Action config = null) + public static StartupConfig AddSagasServer(this StartupConfig startupConfig, Action config = null) { Action defaultConfig = option => { }; var finalConfig = config ?? defaultConfig; - services.AddGrpc(); - services.Configure(config); + startupConfig.Services.AddGrpc(); + startupConfig.Services.Configure(finalConfig); - services.AddSingleton(); - services.AddSingleton(); - services.AddHostedService(); - return services; + startupConfig.Services.AddSingleton(); + startupConfig.Services.AddSingleton(); + startupConfig.Services.AddHostedService(); + return startupConfig; } } } diff --git a/src/Pole.Sagas.Storage.PostgreSql/PoleSagasPostgreSqlExtensions.cs b/src/Pole.Sagas.Storage.PostgreSql/PoleSagasPostgreSqlExtensions.cs index ee31358..8cfbc28 100644 --- a/src/Pole.Sagas.Storage.PostgreSql/PoleSagasPostgreSqlExtensions.cs +++ b/src/Pole.Sagas.Storage.PostgreSql/PoleSagasPostgreSqlExtensions.cs @@ -1,20 +1,22 @@ using Microsoft.Extensions.DependencyInjection; +using Pole.Core; using Pole.Sagas.Core; using Pole.Sagas.Core.Abstraction; +using Pole.Sagas.Storage.PostgreSql; using System; using System.Collections.Generic; using System.Text; -namespace Pole.Sagas.Storage.PostgreSql +namespace Microsoft.Extensions.DependencyInjection { public static class PoleSagasPostgreSqlExtensions { - public static IServiceCollection AddPostgreSqlStorage(IServiceCollection services,Action config) + public static StartupConfig AddSagasServerPGStorage(this StartupConfig startupConfig, Action config) { - services.Configure(config); - services.AddSingleton(); - services.AddSingleton(); - return services; + startupConfig.Services.Configure(config); + startupConfig.Services.AddSingleton(); + startupConfig.Services.AddSingleton(); + return startupConfig; } } } diff --git a/src/Pole.Sagas.Storage.PostgreSql/PostgreSqlSagaStorageInitializer.cs b/src/Pole.Sagas.Storage.PostgreSql/PostgreSqlSagaStorageInitializer.cs index 7ac014f..15a4cc9 100644 --- a/src/Pole.Sagas.Storage.PostgreSql/PostgreSqlSagaStorageInitializer.cs +++ b/src/Pole.Sagas.Storage.PostgreSql/PostgreSqlSagaStorageInitializer.cs @@ -56,7 +56,7 @@ CREATE TABLE IF NOT EXISTS {GetSagaTableName()}( ""ExpiresAt"" timestamp, ""AddTime"" timestamp NOT NULL ); -ALTER TABLE ""{GetSagaTableName()}"" ADD CONSTRAINT ""Sagas_pkey"" PRIMARY KEY (""Id""); +ALTER TABLE {GetSagaTableName()} ADD CONSTRAINT ""Sagas_pkey"" PRIMARY KEY (""Id""); CREATE TABLE IF NOT EXISTS {GetActivityTableName()}( ""Id"" varchar(20) COLLATE ""pg_catalog"".""default"" NOT NULL, @@ -71,14 +71,14 @@ CREATE TABLE IF NOT EXISTS {GetActivityTableName()}( ""AddTime"" timestamp NOT NULL ); -CREATE INDEX ""Activities_SagaId"" ON ""{GetActivityTableName()}"" USING btree ( +CREATE INDEX ""Activities_SagaId"" ON {GetActivityTableName()} USING btree ( ""SagaId"" COLLATE ""pg_catalog"".""default"" ""pg_catalog"".""text_ops"" ASC NULLS LAST ); -ALTER TABLE ""{GetActivityTableName()}"" ADD CONSTRAINT ""Activities_pkey"" PRIMARY KEY (""Id""); +ALTER TABLE {GetActivityTableName()} ADD CONSTRAINT ""Activities_pkey"" PRIMARY KEY (""Id""); -ALTER TABLE ""{GetActivityTableName()}"" ADD CONSTRAINT ""Activities_SagaId_fkey"" FOREIGN KEY (""SagaId"") REFERENCES {GetSagaTableName()} (""Id"") ON DELETE CASCADE ON UPDATE NO ACTION; +ALTER TABLE {GetActivityTableName()} ADD CONSTRAINT ""Activities_SagaId_fkey"" FOREIGN KEY (""SagaId"") REFERENCES {GetSagaTableName()} (""Id"") ON DELETE CASCADE ON UPDATE NO ACTION; "; return batchSql; }