public async Task <bool> Publish(object @event, CancellationToken cancellationToken = default) { var eventType = @event.GetType(); var eventTypeCode = eventTypeFinder.GetCode(eventType); var eventId = snowflakeIdGenerator.NextId(); var eventContent = serializer.Serialize(@event, eventType); var eventEntity = new EventEntity { Added = DateTime.UtcNow, Content = eventContent, ExpiresAt = null, Id = eventId, Name = eventTypeCode, Retries = 0, StatusName = nameof(EventStatus.Pending) }; if (Transaction?.DbTransaction == null) { await eventStorage.StoreEvent(eventEntity); } else { await eventStorage.StoreEvent(eventEntity, Transaction.DbTransaction); } PrePublishEventBuffer.Add(eventEntity); return(true); }
internal Saga(ISnowflakeIdGenerator snowflakeIdGenerator, IServiceProvider serviceProvider, IEventSender eventSender, PoleSagasOption poleSagasOption, ISerializer serializer, IActivityFinder activityFinder) { this.snowflakeIdGenerator = snowflakeIdGenerator; this.serviceProvider = serviceProvider; this.eventSender = eventSender; this.poleSagasOption = poleSagasOption; this.serializer = serializer; this.activityFinder = activityFinder; Id = snowflakeIdGenerator.NextId(); }
public ApiGatewayApplicationAutoMapperProfile(ISnowflakeIdGenerator snowflakeIdGenerator) { //Configure your AutoMapper mapping configuration here... CreateMap <CacheOptions, CacheOptionsDto>(); CreateMap <QoSOptions, QosOptionsDto>(); CreateMap <LoadBalancerOptions, LoadBalancerOptionsDto>(); CreateMap <RateLimitOptions, RateLimitOptionsDto>(); CreateMap <RateLimitRule, RateLimitRuleDto>() .ForMember(dto => dto.ClientWhitelist, map => map.MapFrom(m => m.ClientWhitelist.Split(',').ToList())); CreateMap <AuthenticationOptions, AuthenticationOptionsDto>() .ForMember(dto => dto.AllowedScopes, map => map.MapFrom(m => !string.IsNullOrWhiteSpace(m.AllowedScopes) ? m.AllowedScopes.Split(',').ToList() : new List <string>())); CreateMap <HttpHandlerOptions, HttpHandlerOptionsDto>(); CreateMap <HostAndPort, HostAndPortDto>(); CreateMap <SecurityOptions, SecurityOptionsDto>() .ForMember(dto => dto.IPAllowedList, map => map.MapFrom(m => m.IPAllowedList.Split(',').ToList())) .ForMember(dto => dto.IPBlockedList, map => map.MapFrom(m => m.IPBlockedList.Split(',').ToList())); CreateMap <AggregateReRouteConfig, AggregateReRouteConfigDto>(); CreateMap <AggregateReRoute, AggregateReRouteDto>() .ForMember(dto => dto.ReRouteKeys, map => map.MapFrom(m => m.ReRouteKeys.Split(',').ToList())); CreateMap <GlobalConfiguration, GlobalConfigurationDto>(); CreateMap <DynamicReRoute, DynamicReRouteDto>(); CreateMap <ReRoute, ReRouteDto>(); CreateMap <ReRouteDto, ReRoute>() .ForCtorParam("rerouteId", x => x.MapFrom(m => snowflakeIdGenerator.NextId())) .ForCtorParam("routeName", x => x.MapFrom(m => m.ReRouteName)) .ForCtorParam("downPath", x => x.MapFrom(m => m.DownstreamPathTemplate)) .ForCtorParam("upPath", x => x.MapFrom(m => m.UpstreamPathTemplate)) .ForCtorParam("upMethod", x => x.MapFrom(m => m.UpstreamHttpMethod)) .ForCtorParam("downHost", x => x.MapFrom(m => m.DownstreamHostAndPorts)) .ForMember(map => map.QoSOptions, dto => dto.Ignore()) .ForMember(map => map.CacheOptions, dto => dto.Ignore()) .ForMember(map => map.LoadBalancerOptions, dto => dto.Ignore()) .ForMember(map => map.RateLimitOptions, dto => dto.Ignore()) .ForMember(map => map.AuthenticationOptions, dto => dto.Ignore()) .ForMember(map => map.HttpHandlerOptions, dto => dto.Ignore()) .ForMember(map => map.SecurityOptions, dto => dto.Ignore()); }
public async Task <GlobalConfigurationDto> CreateAsync(GlobalConfigurationDto configurationDto) { await CheckPolicyAsync(); var globalConfiguration = new GlobalConfiguration(_snowflakeIdGenerator.NextId(), configurationDto.BaseUrl); globalConfiguration.RequestIdKey = configurationDto.RequestIdKey; globalConfiguration.DownstreamScheme = configurationDto.DownstreamScheme; ApplyGlobalConfigurationOptions(globalConfiguration, configurationDto); globalConfiguration = await _globalConfigRepository.InsertAsync(globalConfiguration, true); await _eventPublisher.PublishAsync(ApiGatewayDomainConsts.Events_OcelotConfigChanged, Clock.Now); return(ObjectMapper.Map <GlobalConfiguration, GlobalConfigurationDto>(globalConfiguration)); }
public virtual async Task <GlobalConfigurationDto> CreateAsync(GlobalCreateDto input) { await _routeGroupChecker.CheckActiveAsync(input.AppId); var globalConfiguration = new GlobalConfiguration(_snowflakeIdGenerator.NextId(), input.BaseUrl, input.AppId); globalConfiguration.RequestIdKey = input.RequestIdKey; globalConfiguration.DownstreamScheme = input.DownstreamScheme; globalConfiguration.DownstreamHttpVersion = input.DownstreamHttpVersion; ApplyGlobalConfigurationOptions(globalConfiguration, input); globalConfiguration = await _globalConfigRepository.InsertAsync(globalConfiguration, true); await DistributedEventBus.PublishAsync(new ApigatewayConfigChangeEventData(globalConfiguration.AppId, "Global", "Create")); return(ObjectMapper.Map <GlobalConfiguration, GlobalConfigurationDto>(globalConfiguration)); }
public virtual async Task <GlobalConfigurationDto> CreateAsync(GlobalCreateDto globalCreateDto) { await _routeGroupChecker.CheckActiveAsync(globalCreateDto.AppId); var globalConfiguration = new GlobalConfiguration(_snowflakeIdGenerator.NextId(), globalCreateDto.BaseUrl, globalCreateDto.AppId); globalConfiguration.RequestIdKey = globalCreateDto.RequestIdKey; globalConfiguration.DownstreamScheme = globalCreateDto.DownstreamScheme; globalConfiguration.DownstreamHttpVersion = globalCreateDto.DownstreamHttpVersion; ApplyGlobalConfigurationOptions(globalConfiguration, globalCreateDto); globalConfiguration = await _globalConfigRepository.InsertAsync(globalConfiguration, true); await _eventPublisher.PublishAsync(ApigatewayConfigChangeCommand.EventName, new ApigatewayConfigChangeCommand("Global", "Create")); return(ObjectMapper.Map <GlobalConfiguration, GlobalConfigurationDto>(globalConfiguration)); }
private async Task <ActivityExecuteResult> RecursiveExecuteActivity(ActivityWapper activityWapper) { var activityId = snowflakeIdGenerator.NextId(); activityWapper.Id = activityId; activityWapper.CancellationTokenSource = new System.Threading.CancellationTokenSource(activityWapper.TimeOutSeconds * 1000); try { var content = serializer.Serialize(activityWapper.DataObj, activityWapper.ActivityDataType); activityWapper.ActivityStatus = ActivityStatus.Executing; await eventSender.ActivityExecuting(activityId, activityWapper.Name, Id, content, activityWapper.Order, DateTime.UtcNow); var result = await activityWapper.InvokeExecute(); if (!result.IsSuccess) { activityWapper.ActivityStatus = ActivityStatus.Revoked; await eventSender.ActivityRevoked(activityId); await CompensateActivity(result, currentExecuteOrder); var expiresAt = DateTime.UtcNow.AddSeconds(poleSagasOption.CompeletedSagaExpiredAfterSeconds); await eventSender.SagaEnded(Id, expiresAt); return(result); } activityWapper.ActivityStatus = ActivityStatus.Executed; var executeActivity = GetNextExecuteActivity(); if (executeActivity == null) { var expiresAt = DateTime.UtcNow.AddSeconds(poleSagasOption.CompeletedSagaExpiredAfterSeconds); await eventSender.SagaEnded(Id, expiresAt); return(result); } else { return(await RecursiveExecuteActivity(executeActivity)); } } catch (Exception exception) { if (activityWapper.CancellationTokenSource.Token.IsCancellationRequested) { var errors = exception.InnerException != null ? exception.InnerException.Message + exception.StackTrace : exception.Message + exception.StackTrace; var result = new ActivityExecuteResult { IsSuccess = false, Errors = errors }; var bytesContent = serializer.SerializeToUtf8Bytes(activityWapper.DataObj, activityWapper.ActivityDataType); activityWapper.ActivityStatus = ActivityStatus.ExecutingOvertime; await eventSender.ActivityExecuteOvertime(activityId); // 超时的时候 需要首先补偿这个超时的操作 return(await CompensateActivity(result, currentExecuteOrder + 1)); } else { var errors = exception.InnerException != null ? exception.InnerException.Message + exception.StackTrace : exception.Message + exception.StackTrace; var result = new ActivityExecuteResult { IsSuccess = false, Errors = errors }; activityWapper.ActivityStatus = ActivityStatus.ExecuteAborted; await eventSender.ActivityExecuteAborted(activityId); // 出错的时候 需要首先补偿这个出错的操作 var executeResult = await CompensateActivity(result, currentExecuteOrder + 1); var expiresAt = DateTime.UtcNow.AddSeconds(poleSagasOption.CompeletedSagaExpiredAfterSeconds); if (IsCompensated) { await eventSender.SagaEnded(Id, expiresAt); } return(executeResult); } } }
public ApiGatewayApplicationAutoMapperProfile(ISnowflakeIdGenerator snowflakeIdGenerator) { //Configure your AutoMapper mapping configuration here... CreateMap <CacheOptions, CacheOptionsDto>(); CreateMap <QoSOptions, QosOptionsDto>(); CreateMap <LoadBalancerOptions, LoadBalancerOptionsDto>(); CreateMap <RateLimitOptions, RateLimitOptionsDto>(); CreateMap <ServiceDiscoveryProvider, ServiceDiscoveryProviderDto>(); CreateMap <RateLimitRule, RateLimitRuleDto>() .ForMember(dto => dto.ClientWhitelist, map => map.MapFrom((m, n) => { if (!m.ClientWhitelist.IsNullOrWhiteSpace()) { return(m.ClientWhitelist.Split(',').ToList()); } return(new List <string>()); })); CreateMap <AuthenticationOptions, AuthenticationOptionsDto>() .ForMember(dto => dto.AllowedScopes, map => map.MapFrom(m => !string.IsNullOrWhiteSpace(m.AllowedScopes) ? m.AllowedScopes.Split(',').ToList() : new List <string>())); CreateMap <HttpHandlerOptions, HttpHandlerOptionsDto>(); CreateMap <HostAndPort, HostAndPortDto>(); CreateMap <SecurityOptions, SecurityOptionsDto>() .ForMember(dto => dto.IPAllowedList, map => map.MapFrom((m, n) => { if (!m.IPAllowedList.IsNullOrWhiteSpace()) { return(m.IPAllowedList.Split(',').ToList()); } return(new List <string>()); })) .ForMember(dto => dto.IPBlockedList, map => map.MapFrom((m, n) => { if (!m.IPBlockedList.IsNullOrWhiteSpace()) { return(m.IPBlockedList.Split(',').ToList()); } return(new List <string>()); })); CreateMap <AggregateReRouteConfig, AggregateReRouteConfigDto>(); CreateMap <AggregateReRoute, AggregateReRouteDto>() .ForMember(dto => dto.ReRouteKeys, map => map.MapFrom(m => MapperList(m.ReRouteKeys))) .ForMember(dto => dto.UpstreamHttpMethod, map => map.MapFrom(m => MapperList(m.UpstreamHttpMethod))); CreateMap <AggregateReRouteCreateDto, AggregateReRoute>() .ForCtorParam("name", x => x.MapFrom(m => m.Name)) .ForCtorParam("routeId", x => x.MapFrom(m => snowflakeIdGenerator.NextId())) .ForCtorParam("aggregator", x => x.MapFrom(m => m.Aggregator)) .ForCtorParam("appId", x => x.MapFrom(m => m.AppId)) .ForMember(src => src.ExtraProperties, x => x.Ignore()) .ForMember(src => src.ReRouteKeys, x => x.Ignore()) .ForMember(src => src.ReRouteKeysConfig, x => x.Ignore()) .ForMember(src => src.UpstreamHttpMethod, x => x.Ignore()); CreateMap <GlobalConfiguration, GlobalConfigurationDto>(); CreateMap <DynamicReRoute, DynamicReRouteDto>(); CreateMap <ReRoute, ReRouteDto>() .ForMember(frr => frr.AddClaimsToRequest, map => map.MapFrom(m => MapperDictionary(m.AddClaimsToRequest))) .ForMember(frr => frr.AddHeadersToRequest, map => map.MapFrom(m => MapperDictionary(m.AddHeadersToRequest))) .ForMember(frr => frr.AddQueriesToRequest, map => map.MapFrom(m => MapperDictionary(m.AddQueriesToRequest))) .ForMember(frr => frr.DelegatingHandlers, map => map.MapFrom(m => MapperList(m.DelegatingHandlers))) .ForMember(frr => frr.DownstreamHeaderTransform, map => map.MapFrom(m => MapperDictionary(m.DownstreamHeaderTransform))) .ForMember(frr => frr.RouteClaimsRequirement, map => map.MapFrom(m => MapperDictionary(m.RouteClaimsRequirement))) .ForMember(frr => frr.UpstreamHeaderTransform, map => map.MapFrom(m => MapperDictionary(m.UpstreamHeaderTransform))) .ForMember(frr => frr.ChangeDownstreamPathTemplate, map => map.MapFrom(m => MapperDictionary(m.ChangeDownstreamPathTemplate))) .ForMember(frr => frr.UpstreamHttpMethod, map => map.MapFrom(m => MapperList(m.UpstreamHttpMethod))) .ForMember(frr => frr.DownstreamHostAndPorts, map => map.MapFrom(m => MapperHostAndPortList(m.DownstreamHostAndPorts))); CreateMap <ReRouteCreateDto, ReRoute>() .ForCtorParam("rerouteId", x => x.MapFrom(m => snowflakeIdGenerator.NextId())) .ForCtorParam("routeName", x => x.MapFrom(m => m.ReRouteName)) .ForCtorParam("appId", x => x.MapFrom(m => m.AppId)) .ForMember(s => s.DelegatingHandlers, d => d.MapFrom((dto, src) => { src.SetDelegatingHandler(dto.DelegatingHandlers); return(src.DelegatingHandlers); })) .ForMember(s => s.DownstreamHeaderTransform, d => d.MapFrom((dto, src) => { src.SetDownstreamHeader(dto.DownstreamHeaderTransform); return(src.DownstreamHeaderTransform); })) .ForMember(s => s.AddQueriesToRequest, d => d.MapFrom((dto, src) => { src.SetQueriesParamter(dto.AddQueriesToRequest); return(src.AddQueriesToRequest); })) .ForMember(s => s.AddClaimsToRequest, d => d.MapFrom((dto, src) => { src.SetRequestClaims(dto.AddClaimsToRequest); return(src.AddClaimsToRequest); })) .ForMember(s => s.AddHeadersToRequest, d => d.MapFrom((dto, src) => { src.SetRequestHeader(dto.AddHeadersToRequest); return(src.AddHeadersToRequest); })) .ForMember(s => s.RouteClaimsRequirement, d => d.MapFrom((dto, src) => { src.SetRouteClaims(dto.RouteClaimsRequirement); return(src.RouteClaimsRequirement); })) .ForMember(s => s.UpstreamHeaderTransform, d => d.MapFrom((dto, src) => { src.SetUpstreamHeader(dto.UpstreamHeaderTransform); return(src.UpstreamHeaderTransform); })) .ForMember(s => s.ChangeDownstreamPathTemplate, d => d.MapFrom((dto, src) => { src.SetChangeDownstreamPath(dto.ChangeDownstreamPathTemplate); return(src.ChangeDownstreamPathTemplate); })) .ForMember(map => map.QoSOptions, dto => dto.Ignore()) .ForMember(map => map.CacheOptions, dto => dto.Ignore()) .ForMember(map => map.LoadBalancerOptions, dto => dto.Ignore()) .ForMember(map => map.RateLimitOptions, dto => dto.Ignore()) .ForMember(map => map.AuthenticationOptions, dto => dto.Ignore()) .ForMember(map => map.HttpHandlerOptions, dto => dto.Ignore()) .ForMember(map => map.SecurityOptions, dto => dto.Ignore()); CreateMap <RouteGroup, RouteGroupDto>(); }