コード例 #1
0
 public KafkaMessageProducerFactory(
     KafkaMessagingGatewayConfiguration globalConfiguration,
     KafkaPublication publication)
 {
     _globalConfiguration = globalConfiguration;
     _publication         = publication;
 }
コード例 #2
0
 public KafkaMessageProducerFactory(
     KafkaMessagingGatewayConfiguration globalConfiguration
     ) : this(globalConfiguration, new KafkaPublication {
     MakeChannels = OnMissingChannel.Create
 })
 {
 }
コード例 #3
0
ファイル: Startup.cs プロジェクト: mallickhruday/JustRooms
        /// <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 gatewayConnection = new KafkaMessagingGatewayConfiguration
            {
                Name             = "paramore.brighter.accounttransfer",
                BootStrapServers = new[] { "localhost:9092" }
            };
            var producer = new KafkaMessageProducerFactory(gatewayConnection).Create();

            services.AddBrighter(options =>
            {
                options.PolicyRegistry           = policyRegistry;
                options.BrighterMessaging        = new BrighterMessaging(new InMemoryOutbox(), 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"]));
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Red-F/Paramore
        static void Main(string[] args)
        {
            var kafkaMessagingGatewayConfiguration
                = new KafkaMessagingGatewayConfiguration()
                {
                Name             = "paramore.brighter.greetingsender",
                BootStrapServers = new[] { "localhost:9092" }
                };

            for (var i = 0; i < args.Length; i += 2)
            {
                var key = args[i];
                var val = default(string);
                if (i + 1 < args.Length)
                {
                    val = args[i + 1];
                }

                switch (key)
                {
                case "--bootstrap-server":
                    kafkaMessagingGatewayConfiguration.BootStrapServers = new[] { val };
                    break;

                default:
                    break;
                }
            }

            var container             = new TinyIoCContainer();
            var messageMapperFactory  = new TinyIoCMessageMapperFactory(container);
            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory)
            {
                { typeof(GreetingEvent), typeof(GreetingEventMessageMapper) }
            };

            var messageStore = new InMemoryMessageStore();
            var producer     = new KafkaMessageProducerFactory(kafkaMessagingGatewayConfiguration)
                               .Create();

            var builder = CommandProcessorBuilder.With()
                          .Handlers(new HandlerConfiguration())
                          .DefaultPolicy()
                          .TaskQueues(new MessagingConfiguration(messageStore, producer, messageMapperRegistry))
                          .RequestContextFactory(new InMemoryRequestContextFactory());

            var commandProcessor = builder.Build();

            commandProcessor.Post(new GreetingEvent("Wayne"));
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: mcDev89/JustRooms
        private static IHost BuildHost()
        {
            return(new HostBuilder()
                   .ConfigureLogging(loggingBuilder => loggingBuilder.AddConsole())
                   .ConfigureHostConfiguration((configurationBuilder) =>
            {
                configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
                configurationBuilder.AddEnvironmentVariables(prefix: "ASR_");
            })
                   .ConfigureServices((hostContext, services) =>
            {
                var connections = new Connection[]
                {
                    new Connection <UpsertAccountEvent>(
                        new ConnectionName("credit.card.account.stream"),
                        new ChannelName("account.event"),
                        new RoutingKey("account.event"),
                        timeoutInMilliseconds: 200,
                        isDurable: true,
                        highAvailability: true)
                };

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

                var messageConsumerFactory = new KafkaMessageConsumerFactory(gatewayConfiguration);

                services.AddServiceActivator(options =>
                {
                    options.Connections = connections;
                    options.ChannelFactory = new ChannelFactory(messageConsumerFactory);
                })
                .HandlersFromAssemblies(typeof(UpsertAccountEventHandler).Assembly)
                .MapperRegistryFromAssemblies(typeof(AccountEventMessageMapper).Assembly);

                services.AddSingleton <ILoggerFactory>(x => new SerilogLoggerFactory());
                services.AddHostedService <ServiceActivatorHostedService>();

                services.AddDbContext <CardDetailsContext>(options =>
                                                           options.UseMySql(hostContext.Configuration["Database:CardDetails"]));
            })
                   .UseSerilog()
                   .UseConsoleLifetime()
                   .Build());
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: wangkai2014/Brighter
        static void Main(string[] args)
        {
            var kafkaMessagingGatewayConfiguration = new KafkaMessagingGatewayConfiguration()
            {
                Name             = "paramore.brighter.greetingsender",
                BootStrapServers = new[] { "localhost:9092" }
            };

            for (var i = 0; i < args.Length; i += 2)
            {
                var key = args[i];
                var val = default(string);
                if (i + 1 < args.Length)
                {
                    val = args[i + 1];
                }

                switch (key)
                {
                case "--bootstrap-server":
                    kafkaMessagingGatewayConfiguration.BootStrapServers = new[] { val };
                    break;

                default:
                    break;
                }
            }


            var serviceCollection = new ServiceCollection();

            var producer = new KafkaMessageProducerFactory(kafkaMessagingGatewayConfiguration).Create();

            serviceCollection.AddBrighter(options =>
            {
                var outBox = new InMemoryOutbox();
                options.BrighterMessaging = new BrighterMessaging(outBox, outBox, producer, null);
            }).AutoFromAssemblies();

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var commandProcessor = serviceProvider.GetService <IAmACommandProcessor>();

            commandProcessor.Post(new GreetingEvent("Wayne"));
        }
コード例 #7
0
        public static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Console()
                         .CreateLogger();

            var host = new HostBuilder()
                       .ConfigureServices((hostContext, services) =>
            {
                var connections = new Connection[]
                {
                    new Connection <GreetingEvent>(
                        new ConnectionName("paramore.example.greeting"),
                        new ChannelName("greeting.event"),
                        new RoutingKey("greeting.event"),
                        timeoutInMilliseconds: 200)
                };
                //create the gateway
                var messagingGatewayConfiguration = new KafkaMessagingGatewayConfiguration
                {
                    Name             = "paramore.brighter",
                    BootStrapServers = new[] { "localhost:9092" }
                };

                var consumerFactory = new KafkaMessageConsumerFactory(messagingGatewayConfiguration);

                services.AddServiceActivator(options =>
                {
                    options.Connections       = connections;
                    options.ChannelFactory    = new ChannelFactory(consumerFactory);
                    var outBox                = new InMemoryOutbox();
                    options.BrighterMessaging = new BrighterMessaging(outBox, outBox, new KafkaMessageProducerFactory(messagingGatewayConfiguration).Create(), null);
                }).AutoFromAssemblies();


                services.AddHostedService <ServiceActivatorHostedService>();
            })
                       .UseConsoleLifetime()
                       .UseSerilog()
                       .Build();

            await host.RunAsync();
        }
コード例 #8
0
 public KafkaMessageProducerFactory(KafkaMessagingGatewayConfiguration globalConfiguration) :
     this(globalConfiguration, new KafkaMessagingProducerConfiguration())
 {
 }
コード例 #9
0
 public KafkaMessageConsumerFactory(KafkaMessagingGatewayConfiguration globalConfiguration,
                                    KafkaMessagingConsumerConfiguration consumerConfiguration)
 {
     _globalConfiguration   = globalConfiguration;
     _consumerConfiguration = consumerConfiguration;
 }
コード例 #10
0
ファイル: Program.cs プロジェクト: zhangzihan/Brighter
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Console()
                         .CreateLogger();

            var container = new TinyIoCContainer();

            var handlerFactory       = new TinyIocHandlerFactory(container);
            var messageMapperFactory = new TinyIoCMessageMapperFactory(container);

            container.Register <IHandleRequests <GreetingEvent>, GreetingEventHandler>();

            var subscriberRegistry = new SubscriberRegistry();

            subscriberRegistry.Register <GreetingEvent, GreetingEventHandler>();

            //create policies
            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 policyRegistry = new PolicyRegistry
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            };

            //create message mappers
            var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory)
            {
                { typeof(GreetingEvent), typeof(GreetingEventMessageMapper) }
            };

            //create the gateway
            var gatewayConfiguration = new KafkaMessagingGatewayConfiguration
            {
                Name             = "paramore.brighter",
                BootStrapServers = new[] { "localhost:9092" }
            };

            var messageConsumerFactory = new KafkaMessageConsumerFactory(gatewayConfiguration);

            var dispatcher = DispatchBuilder.With()
                             .CommandProcessor(CommandProcessorBuilder.With()
                                               .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                                               .Policies(policyRegistry)
                                               .NoTaskQueues()
                                               .RequestContextFactory(new InMemoryRequestContextFactory())
                                               .Build())
                             .MessageMappers(messageMapperRegistry)
                             .DefaultChannelFactory(new KafkaInputChannelFactory(messageConsumerFactory))
                             .Connections(new Connection[]
            {
                new Connection <GreetingEvent>(
                    new ConnectionName("paramore.example.greeting"),
                    new ChannelName("greeting.event"),
                    new RoutingKey("greeting.event"),
                    timeoutInMilliseconds: 200)
            }).Build();

            dispatcher.Receive();

            Console.WriteLine("Press Enter to stop ...");
            Console.ReadLine();

            dispatcher.End().Wait();
        }
コード例 #11
0
        private static IHost BuildHost()
        {
            return(new HostBuilder()
                   .ConfigureLogging(loggingBuilder => loggingBuilder.AddConsole())
                   .ConfigureHostConfiguration((configurationBuilder) =>
            {
                configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
                configurationBuilder.AddEnvironmentVariables(prefix: "ASP_");
            })
                   .ConfigureServices((hostContext, services) =>
            {
                var gatewayConnection = new KafkaMessagingGatewayConfiguration
                {
                    Name = "paramore.brighter.accounttransfer",
                    BootStrapServers = new[] { "localhost:9092" }
                };

                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 }
                };

                services.AddSingleton <IReadOnlyPolicyRegistry <string> >(policyRegistry);
                var producer = new KafkaMessageProducerFactory(gatewayConnection).Create();
                services
                .AddBrighter(options =>
                {
                    options.PolicyRegistry = policyRegistry;
                    options.BrighterMessaging = new BrighterMessaging(new InMemoryOutbox(), producer);
                })
                .MapperRegistryFromAssemblies(typeof(AccountEventMessageMapper).Assembly);

                var useLocalAwsServices = hostContext.Configuration.GetValue <bool>("AWS:UseLocalServices");

                if (useLocalAwsServices)
                {
                    services.AddSingleton <IAmazonDynamoDB>(sp => CreateClient(hostContext.Configuration));
                    services.AddSingleton <IAmazonDynamoDBStreams>(sp => CreateStreamClient(hostContext.Configuration));
                }
                else
                {
                    services.AddAWSService <IAmazonDynamoDB>();
                    services.AddAWSService <IAmazonDynamoDBStreams>();
                }

                var translatorRegistry = new RecordTranslatorRegistry(new TranslatorFactory());
                translatorRegistry.Add(typeof(AccountEvent), typeof(AccountFromRecordTranslator));

                services.AddSingleton <RecordTranslatorRegistry>(translatorRegistry);
                services.AddSingleton <IRecordProcessor <StreamRecord>, DynamoDbRecordProcessor>();
                services.AddSingleton <IStreamReader, DynamoStreamReader>();
                services.AddHostedService <Pump>();
            })
                   .UseSerilog()
                   .UseConsoleLifetime()
                   .Build());
        }
コード例 #12
0
 /// <summary>
 /// Initializes a factory with the <see cref="KafkaMessagingGatewayConfiguration"/> used to connect to a Kafka Broker
 /// </summary>
 /// <param name="configuration">The <see cref="KafkaMessagingGatewayConfiguration"/> used to connect to the Broker</param>
 public KafkaMessageConsumerFactory(
     KafkaMessagingGatewayConfiguration configuration
     )
 {
     _configuration = configuration;
 }