Пример #1
0
        /// <summary>
        /// 使用consul服务发现
        /// </summary>
        /// <param name="consulUrl"></param>
        /// <param name="serviceName"></param>
        /// <param name="cacheMinute"></param>
        public void UseConsulDiscover(string consulUrl, string serviceName, double cacheMinute = 0.5)
        {
            if (string.IsNullOrEmpty(consulUrl))
            {
                throw new ArgumentNullException("consulUrl");
            }
            var consulClient = new ConsulClient.Consul(consulUrl);

            //发现consul服务注册,返回服务地址
            __GetConsulAgent = () =>
            {
                var serviceInfo = consulClient.GetServiceInfo(serviceName, false, cacheMinute);
                return(new HostAddress()
                {
                    address = serviceInfo.ServiceAddress, port = serviceInfo.ServicePort
                });
            };
        }
Пример #2
0
        /// <summary>
        /// 使用OcelotAPI网关服务发现
        /// </summary>
        /// <param name="gatewayUrl"></param>
        /// <param name="serviceName"></param>
        /// <param name="cacheMinute"></param>
        public void UseOcelotApiGatewayDiscover(string gatewayUrl, string serviceName, double cacheMinute = 0.5)
        {
            if (string.IsNullOrEmpty(gatewayUrl))
            {
                throw new ArgumentNullException("gatewayUrl");
            }
            __GatewayUrl = gatewayUrl;
            var gatewayClient = new ConsulClient.Consul(""); gatewayClient.UseOcelotGatewayDiscover(gatewayUrl);

            __GetConsulAgent = () =>
            {
                //使用真实服务地址
                var serviceInfo = gatewayClient.GetServiceInfo(serviceName, false, cacheMinute);
                return(new HostAddress()
                {
                    address = serviceInfo.ServiceAddress, port = serviceInfo.ServicePort
                });
            };
        }