Example #1
0
        public IActionResult About()
        {
            var serviceSubscriber = subscriberFactory.CreateSubscriber("NanoFabric_Ocelot", 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);
            };
            ILoadBalancer loadBalancer = new RoundRobinLoadBalancer(serviceSubscriber);
            var           endPoint     = loadBalancer.Endpoint().ConfigureAwait(false).GetAwaiter().GetResult();

            string content = "Your application description page";

            if (endPoint != null)
            {
                content             = httpClient.GetStringAsync($"{endPoint.ToUri()}api/values").Result;
                ViewData["Message"] = $"Get {endPoint.ToUri()}api/values: {content}.";
            }
            else
            {
                ViewData["Message"] = $"{content}.";
            }

            return(View());
        }
Example #2
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));
            });
        }
Example #4
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();
    }
Example #5
0
        public async Task <TService> GetRemoteService <TService>(string serviceName) where TService : IService <TService>
        {
            var serviceSubscriber = _subscriberFactory.CreateSubscriber(serviceName);
            await serviceSubscriber.StartSubscription();

            serviceSubscriber.EndpointsChanged += async(sender, eventArgs) =>
            {
                var endpoints = await serviceSubscriber.Endpoints();

                var servicesInfo = string.Join(",", endpoints);
            };
            ILoadBalancer balancer = new RoundRobinLoadBalancer(serviceSubscriber);
            var           endPoint = await balancer.Endpoint();

            var channel = _rpcChannelFactory.Get(endPoint.Address, endPoint.Port);

            return(MagicOnionClient.Create <TService>(channel));
        }
        public async Task <TService> GetRemoteService <TService>(string serviceName) where TService : IService <TService>
        {
            var serviceSubscriber = _subscriberFactory.CreateSubscriber(serviceName, ConsulSubscriberOptions.Default, new NanoFabric.Router.Throttle.ThrottleSubscriberOptions()
            {
                MaxUpdatesPeriod    = TimeSpan.FromSeconds(30),
                MaxUpdatesPerPeriod = 20
            });
            await serviceSubscriber.StartSubscription();

            serviceSubscriber.EndpointsChanged += async(sender, eventArgs) =>
            {
                var endpoints = await serviceSubscriber.Endpoints();

                var servicesInfo = string.Join(",", endpoints);
            };
            ILoadBalancer loadBalancer = new RoundRobinLoadBalancer(serviceSubscriber);
            var           endPoint     = await loadBalancer.Endpoint();

            var serviceChannel = _grpcChannelFactory.Get(endPoint.Address, endPoint.Port);

            return(MagicOnionClient.Create <TService>(serviceChannel));
        }