示例#1
0
    static void Main(string[] args)
    {
        Initialize();
        IServiceSubscriberFactory subscriberFactory = _serviceProvider.GetRequiredService <IServiceSubscriberFactory>();
        // 创建ConsoleLogProvider并根据日志类目名称(CategoryName)生成Logger实例
        var logger = _serviceProvider.GetService <ILoggerFactory>().AddConsole().CreateLogger("App");

        var serviceSubscriber = subscriberFactory.CreateSubscriber("SampleService.Kestrel", ConsulSubscriberOptions.Default, new NanoFabric.Router.Throttle.ThrottleSubscriberOptions()
        {
            MaxUpdatesPeriod = TimeSpan.FromSeconds(30), MaxUpdatesPerPeriod = 20
        });

        serviceSubscriber.StartSubscription().ConfigureAwait(false).GetAwaiter().GetResult();
        serviceSubscriber.EndpointsChanged += async(sender, eventArgs) =>
        {
            // Reset connection pool, do something with this info, etc
            var endpoints = await serviceSubscriber.Endpoints();

            var servicesInfo = string.Join(",", endpoints);
            logger.LogInformation($"Received updated subscribers [{servicesInfo}]");
        };
        ILoadBalancer loadBalancer = new RoundRobinLoadBalancer(serviceSubscriber);
        var           endPoint     = loadBalancer.Endpoint().ConfigureAwait(false).GetAwaiter().GetResult();
        var           httpClient   = new HttpClient();
        var           traceid      = Guid.NewGuid().ToString();

        httpClient.DefaultRequestHeaders.Add("ot-traceid", traceid);
        var content = httpClient.GetStringAsync($"{endPoint.ToUri()}api/values").Result;

        Console.WriteLine($"{traceid} content: {content }");
        System.Console.ReadLine();
    }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                              IServiceSubscriberFactory subscriberFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            var log = loggerFactory.CreateLogger(nameof(Startup));
            var serviceSubscriber = subscriberFactory.CreateSubscriber("FooService");

            serviceSubscriber.StartSubscription().ConfigureAwait(false).GetAwaiter().GetResult();
            serviceSubscriber.EndpointsChanged += async(sender, eventArgs) =>
            {
                // Reset connection pool, do something with this info, etc

                var endpoints = await serviceSubscriber.Endpoints();

                var services = string.Join(",", endpoints);
                log.LogInformation($"Received updated subscribers [{services}]");
            };

            app.Run(async context =>
            {
                var endpoints = await serviceSubscriber.Endpoints();
                await context.Response.WriteAsync(string.Join(",", endpoints));
            });
        }
示例#3
0
    static void Main(string[] args)
    {
        string consulHost = "localhost";
        int    consulPort = 8500;

        var consul = new ConsulClient(config =>
        {
            config.Address = new Uri($"http://{consulHost}:{consulPort}");
        });

        ServiceCollection services = new ServiceCollection(); // 准备好我们的容器

        services.AddSingleton <IConsulClient>(consul);
        services.AddCacheServiceSubscriber();
        services.AddConsulServiceDiscovery();
        services.TryAddTransient <IServiceSubscriberFactory, ServiceSubscriberFactory>();
        IServiceSubscriberFactory subscriberFactory = services.BuildServiceProvider().GetRequiredService <IServiceSubscriberFactory>();
        // 创建ConsoleLogProvider并根据日志类目名称(CategoryName)生成Logger实例
        var logger = services.AddLogging().BuildServiceProvider().GetService <ILoggerFactory>().AddConsole().CreateLogger("App");

        var serviceSubscriber = subscriberFactory.CreateSubscriber("FooService");

        serviceSubscriber.StartSubscription().ConfigureAwait(false).GetAwaiter().GetResult();
        serviceSubscriber.EndpointsChanged += async(sender, eventArgs) =>
        {
            // Reset connection pool, do something with this info, etc
            var endpoints = await serviceSubscriber.Endpoints();

            var servicesInfo = string.Join(",", endpoints);
            logger.LogInformation($"Received updated subscribers [{servicesInfo}]");
        };

        System.Console.ReadLine();
    }
示例#4
0
 public ZooKeeperServiceSubscribeManager(ConfigInfo configInfo, ISerializer <byte[]> serializer,
                                         ISerializer <string> stringSerializer, IServiceSubscriberFactory serviceSubscriberFactory,
                                         ILogger <ZooKeeperServiceSubscribeManager> logger) : base(stringSerializer)
 {
     _configInfo = configInfo;
     _serviceSubscriberFactory = serviceSubscriberFactory;
     _serializer = serializer;
     _logger     = logger;
     CreateZooKeeper().Wait();
     EnterSubscribers().Wait();
 }
示例#5
0
 public ConsulServiceSubscribeManager(ConfigInfo configInfo, ISerializer <byte[]> serializer,
                                      ISerializer <string> stringSerializer, IClientWatchManager manager, IServiceSubscriberFactory serviceSubscriberFactory,
                                      ILogger <ConsulServiceSubscribeManager> logger, IConsulClientProvider consulClientFactory) : base(stringSerializer)
 {
     _configInfo               = configInfo;
     _serializer               = serializer;
     _stringSerializer         = stringSerializer;
     _serviceSubscriberFactory = serviceSubscriberFactory;
     _logger              = logger;
     _manager             = manager;
     _consulClientFactory = consulClientFactory;
     EnterSubscribers().Wait();
 }
 public ConsulServiceSubscribeManager(ConfigInfo configInfo, ISerializer <byte[]> serializer,
                                      ISerializer <string> stringSerializer, IClientWatchManager manager, IServiceSubscriberFactory serviceSubscriberFactory,
                                      ILogger <ConsulServiceSubscribeManager> logger) : base(stringSerializer)
 {
     _configInfo               = configInfo;
     _serializer               = serializer;
     _stringSerializer         = stringSerializer;
     _serviceSubscriberFactory = serviceSubscriberFactory;
     _logger  = logger;
     _manager = manager;
     _consul  = new ConsulClient(config =>
     {
         config.Address = new Uri($"http://{configInfo.Host}:{configInfo.Port}");
     });
     EnterSubscribers().Wait();
 }
 public ConsulServiceSubscribeManager(ConfigInfo configInfo, ISerializer <byte[]> serializer,
                                      ISerializer <string> stringSerializer, IClientWatchManager manager, IServiceSubscriberFactory serviceSubscriberFactory,
                                      ILogger <ConsulServiceSubscribeManager> logger) : base(stringSerializer)
 {
     _configInfo               = configInfo;
     _serializer               = serializer;
     _stringSerializer         = stringSerializer;
     _serviceSubscriberFactory = serviceSubscriberFactory;
     _logger  = logger;
     _manager = manager;
     _logger.LogInformation($"ConsulServiceSubscribeManager->ConsulClient Connect to http://{configInfo.Host}:{configInfo.Port}"); //20180719
     _consul = new ConsulClient(config =>
     {
         config.Address = new Uri($"http://{configInfo.Host}:{configInfo.Port}");
     }, null, h => { h.UseProxy = false; h.Proxy = null; });
     EnterSubscribers().Wait();
 }
示例#8
0
 public HomeController(IServiceSubscriberFactory subscriberFactory, HttpClient httpClient, IConfiguration configuration)
 {
     this.subscriberFactory = subscriberFactory;
     this.httpClient        = httpClient;
     this.configuration     = configuration;
 }
 public GRpcConnection(IServiceSubscriberFactory subscriberFactory, IGrpcChannelFactory grpcChannelFactory)
 {
     this._subscriberFactory  = subscriberFactory;
     this._grpcChannelFactory = grpcChannelFactory;
 }
示例#10
0
 public GRpcRemoteServiceFactory(IGRpcChannelFactory rpcChannelFactory, IServiceSubscriberFactory serviceSubscriberFactory)
 {
     this._rpcChannelFactory = rpcChannelFactory;
     this._subscriberFactory = serviceSubscriberFactory;
 }
示例#11
0
 public HomeController(IServiceSubscriberFactory subscriberFactory, HttpClient httpClient)
 {
     this.subscriberFactory = subscriberFactory;
     this.httpClient        = httpClient;
 }