Пример #1
0
        protected static Channel TryGetChannelFromLoadBalancer(Channel lbChannel,
                                                               ChannelCredentials credentials,
                                                               string serviceName,
                                                               int maxMesssageLength)
        {
            ServiceDiscoveryGrpc.ServiceDiscoveryGrpcClient lbClient =
                new ServiceDiscoveryGrpc.ServiceDiscoveryGrpcClient(lbChannel);

            DiscoverServicesResponse lbResponse = lbClient.DiscoverTopServices(
                new DiscoverServicesRequest
            {
                ServiceName = serviceName,
                MaxCount    = 1
            });

            if (lbResponse.ServiceLocations.Count > 0)
            {
                ServiceLocationMsg serviceLocation = lbResponse.ServiceLocations[0];

                Channel result = GrpcUtils.CreateChannel(serviceLocation.HostName,
                                                         serviceLocation.Port, credentials,
                                                         maxMesssageLength);

                _msg.DebugFormat("The load balancer is suggesting {0}", result.ResolvedTarget);

                return(result);
            }

            // Assumption: A load balancer is never also serving real requests -> lets not use it at all!
            _msg.Debug("The load balancer has no service locations available.");

            return(null);
        }
Пример #2
0
        private IEnumerable <ServiceLocationMsg> UseTopServices(
            IEnumerable <QualifiedService> qualifiedServices,
            int maxCount = -1)
        {
            int resultCount = 0;

            foreach (QualifiedService qualifiedService in qualifiedServices)
            {
                if (maxCount > 0 && resultCount >= maxCount)
                {
                    yield break;
                }

                ServiceLocationMsg serviceLocationMsg =
                    ToServiceLocationMsg(qualifiedService.ServiceLocation);

                // And add it to the least-recently-used queue
                qualifiedService.LastUsed = DateTime.Now;
                _recentlyUsedServices.Enqueue(qualifiedService);

                yield return(serviceLocationMsg);

                resultCount++;
            }
        }
Пример #3
0
        private static ServiceLocationMsg ToServiceLocationMsg(ServiceLocation serviceLocation)
        {
            ServiceLocationMsg serviceLocationMsg = new ServiceLocationMsg
            {
                Scope       = serviceLocation.Scope,
                ServiceName = serviceLocation.ServiceName,
                HostName    = serviceLocation.HostName,
                Port        = serviceLocation.Port
            };

            return(serviceLocationMsg);
        }
Пример #4
0
        private static IEnumerable <ServiceLocationMsg> ToServiceLocationMessages(
            IEnumerable <ServiceLocation> serviceLocations,
            int maxCount = -1)
        {
            int resultCount = 0;

            foreach (ServiceLocation serviceLocation in serviceLocations)
            {
                if (maxCount > 0 && resultCount >= maxCount)
                {
                    yield break;
                }

                ServiceLocationMsg serviceLocationMsg = ToServiceLocationMsg(serviceLocation);

                yield return(serviceLocationMsg);

                resultCount++;
            }
        }
        public void CanDiscoverSingleService()
        {
            DeregisterServices(_host127001, _startPort127001);

            ServiceDiscoveryGrpc.ServiceDiscoveryGrpcClient client = GetClient();

            DiscoverServicesResponse response = client.DiscoverServices(
                new DiscoverServicesRequest
            {
                ServiceName = _serviceName,
                MaxCount    = 1
            });

            Assert.AreEqual(1, response.ServiceLocations.Count);

            ServiceLocationMsg serviceLocation = response.ServiceLocations[0];

            Assert.AreEqual(_serviceName, serviceLocation.ServiceName);
            Assert.AreEqual(_localHost, serviceLocation.HostName);
        }
 private static ServiceLocation ToServiceLocation(ServiceLocationMsg serviceLocationMsg)
 {
     return(new ServiceLocation(serviceLocationMsg.ServiceName, serviceLocationMsg.HostName,
                                serviceLocationMsg.Port, false));
 }