Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json")
                          .AddEnvironmentVariables();

            Configuration = builder.Build();

            var loggerFactory = new LoggerFactory()
                                .AddConsole()
                                .AddDebug();

            ILogger logger    = loggerFactory.CreateLogger <Program>();
            var     rpcLogger = loggerFactory.CreateLogger <InventoryManagementImpl>();

            var port = int.Parse(Configuration["service:port"]);

            var          brokerList    = Configuration["kafkaclient:brokerlist"];
            const string ReservedTopic = "inventoryreserved";
            const string ReleasedTopic = "inventoryreleased";

            var config = new Dictionary <string, object>
            {
                { "group.id", "inventory-server" },
                { "enable.auto.commit", false },
                { "bootstrap.servers", brokerList }
            };
            //var context = new InventoryContext(Configuration["postgres:connectionstring"]);
            var context = new InventoryContext();
            var repo    = new InventoryRepository(context);

            var reservedEventProcessor = new InventoryReservedEventProcessor(repo);
            var kafkaConsumer          = new KafkaReservedConsumer(ReservedTopic, config, reservedEventProcessor);

            kafkaConsumer.Consume();

            var releasedEventProcessor = new InventoryReleasedEventProcessor(repo);
            var releasedConsumer       = new KafkaReleasedConsumer(ReleasedTopic, config, releasedEventProcessor);

            releasedConsumer.Consume();

            var refImpl = new ReflectionServiceImpl(
                ServerReflection.Descriptor, InventoryManagement.Descriptor);
            var inventoryManagement = new InventoryManagementImpl(repo, rpcLogger);
            var server = new Server
            {
                Services = { InventoryManagement.BindService(inventoryManagement),
                             ServerReflection.BindService(refImpl) },
                Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) }
            };

            server.Start();
            logger.LogInformation("Inventory gRPC Service Listening on Port " + port);

            mre.WaitOne();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string       brokerList    = "localhost:9092";
            const string reservedTopic = "inventoryreserved";
            const string releasedTopic = "inventoryreleased";

            var config = new Dictionary <string, object>
            {
                { "group.id", "inventory-server" },
                { "enable.auto.commit", false },
                { "bootstrap.servers", brokerList }
            };
            var context = new InventoryContext();
            var repo    = new InventoryRepository(context);

            var reservedEventProcessor = new InventoryReservedEventProcessor(repo);
            var kafkaConsumer          = new KafkaReservedConsumer(reservedTopic, config, reservedEventProcessor);

            kafkaConsumer.Consume();

            var releasedEventProcessor = new InventoryReleasedEventProcessor(repo);
            var releasedConsumer       = new KafkaReleasedConsumer(releasedTopic, config, releasedEventProcessor);

            releasedConsumer.Consume();

            const int Port   = 3002;
            Server    server = new Server
            {
                Services = { InventoryManagement.BindService(new InventoryManagementImpl(repo)) },
                Ports    = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };

            server.Start();

            Console.WriteLine($"Inventory management listening on port {Port}. Press any key to stop");
            Console.ReadKey();
        }
 public KafkaReservedConsumer(string topic, Dictionary <string, object> config, InventoryReservedEventProcessor eventProcessor)
 {
     this.topic          = topic;
     this.config         = config;
     this.eventProcessor = eventProcessor;
 }