コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="hostBuilder"></param>
        /// <param name="grainAssembly"></param>
        /// <param name="autoMapperAssembly"></param>
        /// <returns></returns>
        public static IHostBuilder AddOrleans(this IHostBuilder hostBuilder, Assembly grainAssembly, Assembly autoMapperAssembly)
        {
            hostBuilder.ConfigureServices(serviceCollection =>
            {
                serviceCollection.AddHostedService <ConsulCleanupService>();
            });

            hostBuilder.UseOrleans((context, siloBuilder) =>
            {
                OrleansConfig = context.Configuration.Get <KanekoOptions>();

                siloBuilder
                .ConfigureApplicationParts(parts =>
                {
                    parts.AddApplicationPart(grainAssembly).WithReferences();
                    parts.AddKanekoParts();
                })
                .Configure <SiloOptions>(options => options.SiloName        = OrleansConfig.ServiceName)
                .Configure <HostOptions>(options => options.ShutdownTimeout = TimeSpan.FromSeconds(30))
                .UseLinuxEnvironmentStatistics()
                .UsePerfCounterEnvironmentStatistics();

                if (OrleansConfig.Orleans.Dashboard.Enable)
                {
                    siloBuilder.UseDashboard(o =>
                    {
                        o.Port = OrleansConfig.Orleans.Dashboard.SiloDashboardPort;
                        o.CounterUpdateIntervalMs = (int)TimeSpan.Parse(OrleansConfig.Orleans.Dashboard.WriteInterval).TotalMilliseconds;
                        o.HideTrace = OrleansConfig.Orleans.Dashboard.HideTrace;
                        o.Username  = OrleansConfig.Orleans.Dashboard.UserName;
                        o.Password  = OrleansConfig.Orleans.Dashboard.Password;
                        o.BasePath  = OrleansConfig.Orleans.Dashboard.BasePath;
                    });
                }

                SetGrainCollectionOptions(siloBuilder, grainAssembly);

                siloBuilder.Configure <ClusterMembershipOptions>(options =>
                {
                    options.IAmAliveTablePublishTimeout = TimeSpan.FromMinutes(1.0);//失活的节点默认忽略时间
                });

                //siloBuilder.Configure<PerformanceTuningOptions>(options =>
                //{
                //    options.DefaultConnectionLimit = ServicePointManager.DefaultConnectionLimit;
                //});
                siloBuilder.Configure <SchedulingOptions>(options =>
                {
                    options.PerformDeadlockDetection = true;
                    options.AllowCallChainReentrancy = true;
                    options.MaxActiveThreads         = Process.GetCurrentProcess().ProcessorAffinityList().Count();
                })
                .Configure <MessagingOptions>(options =>
                {
                    options.ResponseTimeout = TimeSpan.FromMinutes(5);
                })
                .Configure <SiloMessagingOptions>(options =>
                {
                    options.ResponseTimeout = TimeSpan.FromMinutes(5);
                });

                SetReminder(siloBuilder);
                SetStorage(siloBuilder);
                SetSiloSource(siloBuilder);
                SetConfigureServices(siloBuilder, grainAssembly, autoMapperAssembly);

                siloBuilder.Configure <StatisticsOptions>(o =>
                {
                    o.LogWriteInterval          = TimeSpan.FromMinutes(10);
                    o.PerfCountersWriteInterval = TimeSpan.Parse(OrleansConfig.Orleans.MetricsTableWriteInterval);
                });
            });

            return(hostBuilder);
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: zhrjin/Kaneko
        public void ConfigureServices(IServiceCollection services)
        {
            KanekoOptions kanekoConfig = Configuration.Get <KanekoOptions>();

            services.AddControllers(mvcConfig =>
            {
                mvcConfig.Filters.Add <GlobalExceptionFilter>();
            })
            .AddApplicationPart(typeof(Startup).Assembly)
            .AddApplicationPart(typeof(CapController).Assembly);

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title   = kanekoConfig.ServiceName,
                    Version = "v1"
                });
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description  = "在下框中输入请求头中需要添加Jwt授权Token:Bearer Token",
                    Name         = "Authorization",
                    In           = ParameterLocation.Header,
                    Type         = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme       = "Bearer"
                });
            });

            services.AddSingleton <IMongoClient>(new MongoClient(kanekoConfig.MongoDB.ConnectionString));
            services.AddCap(x =>
            {
                x.UseMongoDB(configure =>
                {
                    configure.DatabaseConnection = kanekoConfig.MongoDB.ConnectionString;
                    configure.DatabaseName       = kanekoConfig.MongoDB.DatabaseName + "_cap";
                });
                x.UseRabbitMQ(configure =>
                {
                    configure.HostName    = kanekoConfig.RabbitMQ.HostName;
                    configure.UserName    = kanekoConfig.RabbitMQ.UserName;
                    configure.Password    = kanekoConfig.RabbitMQ.Password;
                    configure.Port        = kanekoConfig.RabbitMQ.Port;
                    configure.VirtualHost = kanekoConfig.RabbitMQ.VirtualHost;
                });

                x.UseDashboard();

                if (kanekoConfig.Cap.ServiceDiscovery.Enable)
                {
                    x.UseDiscovery(d =>
                    {
                        d.DiscoveryServerHostName = kanekoConfig.Consul.HostName;
                        d.DiscoveryServerPort     = kanekoConfig.Consul.Port;
                        d.CurrentNodeHostName     = kanekoConfig.CurrentNodeHostName;
                        d.CurrentNodePort         = kanekoConfig.CurrentNodePort;
                        d.NodeId    = $"cap_{kanekoConfig.ServiceName}_{kanekoConfig.CurrentNodeHostName}:{kanekoConfig.CurrentNodePort}";
                        d.NodeName  = "cap_" + kanekoConfig.ClusterId;
                        d.MatchPath = kanekoConfig.Cap.ServiceDiscovery.HealthPath;
                    });
                }
            });

            //对象转换注入
            services.AddSingleton <Kaneko.Server.AutoMapper.IObjectMapper, AutoMapperObjectMapper>();

            //自动注入接口
            services.Scan(scan =>
            {
                scan.FromAssemblies(typeof(ITaskListSyncService).Assembly).AddClasses().UsingAttributes();
            });

            //AutoMapper 注入
            services.AddAutoMapper(typeof(ITaskListSyncService).Assembly);

            services.AddHttpClient();
            services.AddHttpClient(ConfigKeys.HttpClientName, configureClient =>
            {
                configureClient.BaseAddress = new System.Uri(Configuration[ConfigKeys.SYSDOMAIN_GATEWAYURL]);
            });

            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = $"{kanekoConfig.Redis.HostName}:{kanekoConfig.Redis.Port}";
                options.InstanceName  = kanekoConfig.Redis.InstanceName;
                if (!string.IsNullOrEmpty(kanekoConfig.Redis.Password))
                {
                    options.ConfigurationOptions.Password = kanekoConfig.Redis.Password;
                }
            });
        }