Beispiel #1
0
        public static KafkaBuilder AddProducer <TProducerClient, TKey, TValue>(
            this KafkaBuilder kafkaBuilder,
            Action <IServiceProvider, ProducerConnectionBuilder <TKey, TValue> > optionsAction
            )
            where TProducerClient : class, IProducerClient <TKey, TValue>
        {
            var services = kafkaBuilder.Services;

            services.AddSingleton <IProducerClient <TKey, TValue>, TProducerClient>();

            services.AddSingleton <IProducerSender <TKey, TValue> >(s =>
            {
                var logger         = s.GetRequiredService <IServiceBusLogger>();
                var config         = s.GetService <IOptions <KafkaConfig> >();
                var builder        = new ProducerConnectionBuilder <TKey, TValue>(config.Value.CertificatePath);
                var producerClient = s.GetRequiredService <IProducerClient <TKey, TValue> >();

                optionsAction(s, builder);

                if (builder.AsyncProducer)
                {
                    return(new ProducerAsyncSender <TKey, TValue>(
                               builder.Build(),
                               producerClient,
                               logger
                               ));
                }
                else
                {
                    return(new ProducerSyncSender <TKey, TValue>(
                               builder.Build(),
                               producerClient,
                               logger
                               ));
                }
            });

            return(kafkaBuilder);
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IKafkaService, KafkaService>();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddAuthorization();
            services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority            = "http://localhost:64433";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api1";
            });

            services.AddKafka(KafkaBuilder =>
            {
                var kafkaConfig = Configuration.GetSection("KafkaService");
                KafkaBuilder.AddConfiguration(kafkaConfig);
            });

            //services.AddSingleton<IOrderService, OrderService>();
            // Add Steeltoe Discovery Client service
            services.AddDiscoveryClient(Configuration);

            // Add Steeltoe handler to container
            services.AddTransient <DiscoveryHttpMessageHandler>();

            // Configure a HttpClient
            services.AddHttpClient <OrderService>(c =>
            {
                c.BaseAddress = new Uri("http://orderservice");
            })
            .AddHttpMessageHandler <DiscoveryHttpMessageHandler>()
            .AddTypedClient <IOrderService, OrderService>();

            var log = LoggerFac.CreateLogger <Startup>();

            log.LogDebug("服务配置完成");
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            //这里获取程序及和工作线程配置信息
            Dictionary <string, Assembly> assmblyColl = new Dictionary <string, Assembly>();
            var host = new HostBuilder()
                       .UseEnvironment(EnvironmentName.Development)

                       .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                //这里netcore支持多数据源,所以可以扩展到数据库或者redis,集中进行配置。
                //
                configApp.SetBasePath(Directory.GetCurrentDirectory());
                configApp.AddJsonFile(
                    $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                    optional: true);
                configApp.AddEnvironmentVariables("PREFIX_");
                configApp.AddCommandLine(args);
            }).ConfigureLogging((hostContext, configBuild) =>
            {
                configBuild.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
                configBuild.AddConsole();
                configBuild.AddCustomizationLogger();
            })
                       .ConfigureServices((hostContext, service) =>
            {
                service.Configure <HostOptions>(option =>
                {
                    option.ShutdownTimeout = System.TimeSpan.FromSeconds(10);
                });

                service.AddKafka(KafkaBuilder =>
                {
                    KafkaBuilder.AddConfiguration(hostContext.Configuration.GetSection("KafkaService"));
                });
                service.AddElasticsearchClient(config => {
                    config.AddConfiguration(hostContext.Configuration.GetSection("ElasticsearchService"));
                });

                service.AddDbContext <ConsoleDbContext>(option =>
                                                        option.UseMySQL(hostContext.Configuration.GetConnectionString("ConsoleDatabase")), ServiceLifetime.Transient, ServiceLifetime.Transient);
                ///TODO 待实现从数据库中pull数据,再将任务添加进DI
                service.AddSingleton <IConsole, KafkaToElasticsearch>();
            })
                       .Build();
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       token  = source.Token;
            var task = Task.Run(async() => {
                IConsole console = host.Services.GetService <IConsole>();
                await console.AsyncExcute(source.Token);
            }, source.Token);
            Dictionary <string, Task> dictTask = new Dictionary <string, Task>();

            dictTask.Add("kafkatoelasticsearch", task);

            int recordRunCount = 0;
            var fact           = host.Services.GetService <ILoggerFactory>();
            var log            = fact.CreateLogger <Program>();
            var disp           = Task.Run(() =>
            {
                while (true)
                {
                    if (!token.IsCancellationRequested)
                    {
                        ++recordRunCount;
                        foreach (KeyValuePair <string, Task> item in dictTask)
                        {
                            if (item.Value.IsCanceled ||
                                item.Value.IsCompleted ||
                                item.Value.IsCompletedSuccessfully ||
                                item.Value.IsFaulted)
                            {
                                log.LogWarning("console任务:{0},参数:{1},执行异常,task状态:{2}", item.Key, "", item.Value.Status);
                                if (item.Value.Exception != null)
                                {
                                    log.LogError(item.Value.Exception, "task:{0},参数:{1},执行错误.", item.Key, "");
                                    //TODO 根据参数更新数据库状态,以便被监控到。
                                }
                                //更新数据库状态。
                            }
                        }
                    }
                    System.Threading.Thread.Sleep(2000);
                    log.LogInformation("循环:{0}次,接下来等待2秒。", recordRunCount);
                }
            }, source.Token);

            IApplicationLifetime appLiftTime = host.Services.GetService <IApplicationLifetime>();

            appLiftTime.ApplicationStopping.Register(() => {
                log.LogInformation("程序停止中。");
                source.Cancel();
                log.LogInformation("程序停止完成。");
            });
            host.RunAsync().GetAwaiter().GetResult();
        }