Esempio n. 1
0
 public KafkaMessageProducerFactory(KafkaMessagingGatewayConfiguration globalConfiguration,
                                    KafkaMessagingProducerConfiguration producerConfiguration)
 {
     _globalConfiguration   = globalConfiguration;
     _producerConfiguration = producerConfiguration;
 }
Esempio n. 2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            var retryPolicy               = Policy.Handle <Exception>().WaitAndRetry(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicy      = Policy.Handle <Exception>().CircuitBreaker(1, TimeSpan.FromMilliseconds(500));
            var retryPolicyAsync          = Policy.Handle <Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(150) });
            var circuitBreakerPolicyAsync = Policy.Handle <Exception>().CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500));
            var policyRegistry            = new PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy },
                { CommandProcessor.RETRYPOLICYASYNC, retryPolicyAsync },
                { CommandProcessor.CIRCUITBREAKERASYNC, circuitBreakerPolicyAsync },
                { Catalog.DynamoDbAccess, retryPolicyAsync }
            };

            var messageStore = new MySqlOutbox(new MySqlOutboxConfiguration(
                                                   Configuration["Database:Accounts"],
                                                   Configuration["Database:MessageTableName"]
                                                   ));

            var gatewayConnection = new KafkaMessagingGatewayConfiguration
            {
                Name             = "paramore.brighter.accounttransfer",
                BootStrapServers = new[] { "localhost:9092" }
            };

            var _kafkaMessagingProducerConfiguration = new KafkaMessagingProducerConfiguration
            {
                Acks                  = Acks.All,
                MessageTimeout        = 10000,
                RequestTimeout        = 3000,
                MessageSendMaxRetries = 3,
                RetryBackoff          = 100,
                QueueBufferingMax     = 0
            };

            var producer = new KafkaMessageProducerFactory(gatewayConnection, _kafkaMessagingProducerConfiguration).Create();

            services.AddBrighter(options =>
            {
                options.PolicyRegistry           = policyRegistry;
                options.BrighterMessaging        = new BrighterMessaging(messageStore, producer);
                options.CommandProcessorLifetime = ServiceLifetime.Scoped;
            })
            .AsyncHandlersFromAssemblies(typeof(AddNewAccountHandlerAsync).Assembly)
            .MapperRegistryFromAssemblies(typeof(AccountEvent).Assembly);

            services.AddDarker(options => options.HandlerLifetime = ServiceLifetime.Scoped)
            .AddHandlersFromAssemblies(typeof(GetAccountByIdHandlerAsync).Assembly);

            services.AddOpenApiDocument(config =>
            {
                config.PostProcess = document =>
                {
                    document.Info.Version        = "v1";
                    document.Info.Title          = "Booking API";
                    document.Info.Description    = "book rooms in our hotel";
                    document.Info.TermsOfService = "None";
                    document.Info.Contact        = new NSwag.OpenApiContact
                    {
                        Name  = "Ian Cooper",
                        Email = string.Empty,
                        Url   = "https://twitter.com/icooper"
                    };
                };
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDbContext <AccountContext>(options =>
                                                   options.UseMySql(Configuration["Database:Accounts"]));
        }