Ejemplo n.º 1
0
        public void Configure(ISiloBuilder siloBuilder) =>
#pragma warning disable CA1062 // 验证公共方法的参数
        siloBuilder
#pragma warning restore CA1062 // 验证公共方法的参数
        .ConfigureServices((context, services) =>
        {
            services.RemoveAll <RestClient>();

            services.AddScoped <MsvClientCtrlSDK>();
            var client = new RestClient(null, "http://172.16.0.205:9025", "http://172.16.0.205:10023");
            services.AddSingleton <RestClient>(client);
            //services.AddSingleton<IScheduleService, ScheduleTaskService>();
            services.AddSingleton <IDeviceMonitorClient, DeviceMonitorClient>();

            services.AddTransient <ITaskHandlerFactory, TaskHandlerFactory>(sp => TaskHandlerFactory.Create(sp));
            services.AddAutoMapper(typeof(GlobalProfile));
            services.AddSingletonNamedService <PlacementStrategy, ScheduleTaskPlacementStrategy>(nameof(ScheduleTaskPlacementStrategy));
            services.AddSingletonKeyedService <Type, IPlacementDirector, ScheduleTaskPlacementSiloDirector>(typeof(ScheduleTaskPlacementStrategy));
        } /*services.AddSingleton<ILoggerFactory>(x => new SerilogLoggerFactory())*/)
        .AddMemoryGrainStorageAsDefault()
        .AddMemoryGrainStorage("PubSubStore")
        .AddGrainService <DeviceMonitorService>()
        .UseInMemoryReminderService()
        .AddSimpleMessageStreamProvider(StreamProviderName.Default);
Ejemplo n.º 2
0
        private static void ConfigureSiloBuilder(
            Microsoft.Extensions.Hosting.HostBuilderContext context,
            ISiloBuilder siloBuilder) =>
        siloBuilder
        .Configure <SerializationProviderOptions>(opt => opt.SerializationProviders.Add(typeof(ProtobufNetSerializer).GetTypeInfo()))
        .Configure <ClusterMembershipOptions>(opt => {
            opt.DefunctSiloExpiration    = TimeSpan.FromHours(1);
            opt.DefunctSiloCleanupPeriod = TimeSpan.FromHours(1);
        })
#if DEBUG
#else
        .Configure <EndpointOptions>(options =>
        {
            var lst = Dns.GetHostEntry("appnode").AddressList;
            foreach (var item in lst)
            {
                Console.WriteLine(item.ToString());
            }

            options.SiloPort                 = 11111;
            options.GatewayPort              = 30000;
            options.AdvertisedIPAddress      = Dns.GetHostAddresses("appnode").First();
            options.GatewayListeningEndpoint = new IPEndPoint(IPAddress.Any, 40000);
            options.SiloListeningEndpoint    = new IPEndPoint(IPAddress.Any, 50000);
        })
#endif
        .ConfigureServices(
            (context, services) =>
        {
            var CircuitBreakerOpenTriggerCount = context.Configuration.GetSection("PollySetting:CircuitBreakerOpenTriggerCount").Get <int>();
            //通用策略
            services.AddHttpClientPolly(PollyHttpClientServiceCollectionExtensions.HttpclientName, options =>
            {
                options.TimeoutTime = context.Configuration.GetSection("PollySetting:TimeoutTime").Get <int>();
                options.RetryCount  = context.Configuration.GetSection("PollySetting:RetryCount").Get <int>();
                options.CircuitBreakerOpenFallCount = context.Configuration.GetSection("PollySetting:CircuitBreakerOpenFallCount").Get <int>();
                options.CircuitBreakerDownTime      = context.Configuration.GetSection("PollySetting:CircuitBreakerDownTime").Get <int>();
                options.CircuitBreakerAction        = (p =>
                {
                    int rcount = (int)p;
                    if (rcount == CircuitBreakerOpenTriggerCount)
                    {
                        Environment.Exit(0);            //断路器触发超过CircuitBreakerOpenTriggerCount次-退出程序
                    }
                });
            });

            services.AddSingleton <MsvClientCtrlSDK>();
            services.AddSingleton <RestClient>(pd =>
            {
                return(new RestClient(pd.GetService <IHttpClientFactory>().CreateClient(PollyHttpClientServiceCollectionExtensions.HttpclientName),
                                      context.Configuration.GetSection("IngestDBSvr").Value, context.Configuration.GetSection("CMServer").Value));
            });

            //services.AddSingleton<IDeviceMonitorService, DeviceMonitorService>();
            services.AddSingleton <IDeviceMonitorClient, DeviceMonitorClient>();
            services.AddSingleton <IGrainServiceDataBack, GrainServiceDataBack>();
            services.AddTransient <ITaskHandlerFactory, TaskHandlerFactory>(sp => TaskHandlerFactory.Create(sp));

            services.Configure <ApplicationOptions>(context.Configuration);
            services.Configure <ClusterOptions>(opt => { opt.ClusterId = Cluster.ClusterId; opt.ServiceId = Cluster.ServiceId; });
            services.AddSingletonNamedService <PlacementStrategy, ScheduleTaskPlacementStrategy>(nameof(ScheduleTaskPlacementStrategy));
            services.AddSingletonKeyedService <Type, IPlacementDirector, ScheduleTaskPlacementSiloDirector>(typeof(ScheduleTaskPlacementStrategy));
            //services.BuildServiceProvider();
        })
        .UseSiloUnobservedExceptionsHandler()
        .UseAdoNetClustering(
            options =>
        {
            options.Invariant        = "MySql.Data.MySqlClient";
            options.ConnectionString = context.Configuration.GetSection("ConnectDB").Value;
        })
#if DEBUG
        .ConfigureEndpoints(
            EndpointOptions.DEFAULT_SILO_PORT,
            EndpointOptions.DEFAULT_GATEWAY_PORT
            )
#endif
        .AddAdoNetGrainStorageAsDefault(
            options =>
        {
            options.Invariant        = "MySql.Data.MySqlClient";
            options.ConnectionString = context.Configuration.GetSection("ConnectDB").Value;
            options.ConfigureJsonSerializerSettings = ConfigureJsonSerializerSettings;
            options.UseJsonFormat = true;
        }
            )

        .UseAdoNetReminderService(
            options =>
        {
            options.Invariant        = "MySql.Data.MySqlClient";
            options.ConnectionString = context.Configuration.GetSection("ConnectDB").Value;
        })

        .AddSimpleMessageStreamProvider(StreamProviderName.Default)

        .AddAdoNetGrainStorage(
            "PubSubStore",
            options =>
        {
            options.Invariant        = "MySql.Data.MySqlClient";
            options.ConnectionString = context.Configuration.GetSection("ConnectDB").Value;
            options.ConfigureJsonSerializerSettings = ConfigureJsonSerializerSettings;
            options.UseJsonFormat = true;
        })
        .AddGrainService <DeviceMonitorService>()
        .UseIf(
            RuntimeInformation.IsOSPlatform(OSPlatform.Linux),
            x => x.UseLinuxEnvironmentStatistics())
        .UseIf(
            RuntimeInformation.IsOSPlatform(OSPlatform.Windows),
            x => x.UsePerfCounterEnvironmentStatistics())
        .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(HelloGrain).Assembly).WithReferences())
        .ConfigureApplicationParts(parts => parts.AddFromApplicationBaseDirectory())
        .UseDashboard(config =>
        {
            config.Port = int.Parse(context.Configuration.GetSection("Port").Value, CultureInfo.CurrentCulture);
        });
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            var siloPort    = 11111;
            int gatewayPort = 30000;
            var siloAddress = IPAddress.Loopback;

            var silo =
                new SiloHostBuilder()

                .ConfigureServices((context, services) =>
            {
                services.RemoveAll <RestClient>();

                services.AddScoped <MsvClientCtrlSDK>();
                var client = new RestClient(null, "http://172.16.135.116:9024", "http://172.16.0.205:10023");
                services.AddSingleton <RestClient>(client);
                services.AddSingleton <IDeviceMonitorService, DeviceMonitorService>();
                services.AddSingleton <IDeviceMonitorClient, DeviceMonitorClient>();

                services.AddTransient <ITaskHandlerFactory, TaskHandlerFactory>(sp => TaskHandlerFactory.Create(sp));
                services.AddSingleton <IGrainServiceDataBack, GrainServiceDataBack>();
                services.AddAutoMapper(typeof(GlobalProfile));
                services.AddSingletonNamedService <PlacementStrategy, ScheduleTaskPlacementStrategy>(nameof(ScheduleTaskPlacementStrategy));
                services.AddSingletonKeyedService <Type, IPlacementDirector, ScheduleTaskPlacementSiloDirector>(typeof(ScheduleTaskPlacementStrategy));
            })
                .UseDevelopmentClustering(options => options.PrimarySiloEndpoint = new IPEndPoint(siloAddress, siloPort))
                .UseInMemoryReminderService()
                .ConfigureEndpoints(siloAddress, siloPort, gatewayPort)
                .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = "helloworldcluster";
                options.ServiceId = "1";
            })
                .ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(TestCalls).Assembly))
                .ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(IngestTestGrain.TestCalls).Assembly))
                .ConfigureApplicationParts(parts => parts.AddFromApplicationBaseDirectory())
                .ConfigureLogging(builder =>
            {
                builder.AddConsole();
            })
                .ConfigureAppConfiguration((config) =>
            {
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);

                var dic = new Dictionary <string, string>();

                string fileName = "publicsetting.xml";
                string path     = string.Empty;
                if ((Environment.OSVersion.Platform == PlatformID.Unix) || (Environment.OSVersion.Platform == PlatformID.MacOSX))
                {
                    //str = string.Format(@"{0}/{1}", System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, fileName);
                    path = '/' + fileName;
                }
                else
                {
                    path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "/" + fileName;
                }

                if (File.Exists(path))
                {
                    XDocument xd = new XDocument();
                    xd           = XDocument.Load(path);
                    XElement ps  = xd.Element("PublicSetting");
                    XElement sys = ps.Element("System");

                    string vip = sys.Element("Sys_VIP").Value;
                    dic.Add("VIP", vip);
                    dic.Add("IngestDBSvr", CreateConfigURI(sys.Element("IngestDBSvr").Value));
                    dic.Add("IngestDEVCTL", CreateConfigURI(sys.Element("IngestDEVCTL").Value));
                    dic.Add("CMWindows", CreateConfigURI(sys.Element("CMserver_windows").Value));
                    dic.Add("CMServer", CreateConfigURI(sys.Element("CMServer").Value));
                    config.AddInMemoryCollection(dic);
                }
            })
                .AddMemoryGrainStorageAsDefault()
                .AddMemoryGrainStorage("PubSubStore")
                .AddGrainService <DeviceMonitorService>()
                .AddSimpleMessageStreamProvider(StreamProviderName.Default)
                .UseInMemoryReminderService()
                .UseDashboard(options =>
            {
                options.HostSelf  = true;
                options.HideTrace = false;
            })
                .Build();

            silo.StartAsync().Wait();

            var client =
                new ClientBuilder()
                .UseStaticClustering(options => options.Gateways.Add((new IPEndPoint(siloAddress, gatewayPort)).ToGatewayUri()))
                .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = "helloworldcluster";
                options.ServiceId = "1";
            })
                .ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(TestCalls).Assembly))
                .ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(IngestTestGrain.TestCalls).Assembly))
                .ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(IngestTestGrain.TestCalls).Assembly))
                .ConfigureApplicationParts(parts => parts.AddFromApplicationBaseDirectory())
                .ConfigureLogging(builder =>
            {
                builder.AddConsole();
            })
                .Build();

            client.Connect().Wait();

            var cts = new CancellationTokenSource();

            //TestCalls.Make(client, cts);
            IngestTestGrain.TestCalls.Make(client, cts);

            Console.WriteLine("Press key to exit...");
            Console.ReadLine();

            cts.Cancel();

            silo.StopAsync().Wait();
        }