Example #1
0
        private Server StartLoadBalancerService([NotNull] ServiceRegistry serviceRegistry,
                                                [NotNull] LoadBalancerConfig loadBalancerConfig)
        {
            ServerCredentials serverCredentials =
                GrpcServerUtils.GetServerCredentials(loadBalancerConfig.Certificate,
                                                     loadBalancerConfig.PrivateKeyFile,
                                                     loadBalancerConfig.EnforceMutualTls);

            var serviceDiscoveryGrpcImpl = new ServiceDiscoveryGrpcImpl(serviceRegistry)
            {
                RemoveUnhealthyServices           = !_keyValueStoreIsLocal,
                WorkerResponseTimeoutSeconds      = loadBalancerConfig.ServiceResponseTimeoutSeconds,
                RecentlyUsedServiceTimeoutSeconds = loadBalancerConfig.RecentlyUsedTimeoutSeconds
            };

            var health = new HealthServiceImpl();

            serviceDiscoveryGrpcImpl.Health = health;

            // General status:
            health.SetStatus(string.Empty, HealthCheckResponse.Types.ServingStatus.Serving);

            // Specifically the LB service:
            health.SetStatus(serviceDiscoveryGrpcImpl.ServiceName,
                             HealthCheckResponse.Types.ServingStatus.Serving);

            _logger.LogInformation("Starting load-balancer service at {host}:{port}",
                                   loadBalancerConfig.HostName, loadBalancerConfig.Port);

            var server =
                new Server
            {
                Services =
                {
                    ServiceDiscoveryGrpc.BindService(serviceDiscoveryGrpcImpl),
                    Health.BindService(health)
                },
                Ports =
                {
                    new ServerPort(loadBalancerConfig.HostName, loadBalancerConfig.Port,
                                   serverCredentials)
                }
            };

            server.Start();

            string protocol = serverCredentials == ServerCredentials.Insecure
                                ? "http"
                                : "https";

            _logger.LogInformation(
                "Load balancer service is serving at {protocol}://{host}:{port}",
                protocol, loadBalancerConfig.HostName, loadBalancerConfig.Port);

            return(server);
        }
        private static ServiceRegistry StartServiceDiscoveryService()
        {
            var serviceRegistry = new ServiceRegistry(new LocalKeyValueStore(), "test");

            var serviceDiscoveryGrpcImpl =
                new ServiceDiscoveryGrpcImpl(serviceRegistry);

            var server =
                new Server
            {
                Services =
                {
                    ServiceDiscoveryGrpc.BindService(serviceDiscoveryGrpcImpl)
                },
                Ports =
                {
                    new ServerPort(_host127001, _port, ServerCredentials.Insecure)
                }
            };

            server.Start();

            return(serviceRegistry);
        }