/// <summary>
        /// Registers the specified agent service registration.
        /// </summary>
        /// <param name="agentServiceRegistration">The agent service registration.</param>
        /// <param name="config">The configuration.</param>
        /// <returns></returns>
        private static async Task Register(AgentServiceRegistration agentServiceRegistration,
                                           ConsulLocalServiceConfig config)
        {
            if (!config.ConsulIntegration)
            {
                InnerLogger.Log(LoggerLevel.Info, "ConsulIntegration=false,当前服务不集成consul环境");
                return;
            }

            InnerLogger.Log(LoggerLevel.Info,
                            "register:" + Newtonsoft.Json.JsonConvert.SerializeObject(agentServiceRegistration));

            try
            {
                var client = CreateConsulClient(config.ConsulAddress);
                var rs     = await client.Agent.ServiceRegister(agentServiceRegistration);

                client.Dispose();
                InnerLogger.Log(LoggerLevel.Info, Newtonsoft.Json.JsonConvert.SerializeObject(rs));
            }
            catch (Exception ex)
            {
                InnerLogger.Log(LoggerLevel.Error, $"consul Register failed {Environment.NewLine}{ex.ToString()}");
            }
        }
        public void LocalServiceConfig_ConsulTagIsNullOrEmpty()
        {
            var localService = new ConsulLocalServiceConfig();

            Assert.Throws <Exception>(() => {
                localService.Validation();
            });
        }
        public void LocalServiceConfig_ConsulTagValid()
        {
            var localService = new ConsulLocalServiceConfig
            {
                ConsulTags = "v-1.2.2"
            };

            localService.Validation();
        }
Exemple #4
0
        /// <summary>
        /// Starts the and register service.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="serviceConfig">The service configuration.</param>
        /// <returns></returns>
        public static async Task <Server> StartAndRegisterService(this Grpc.Core.Server server,
                                                                  ConsulLocalServiceConfig serviceConfig)
        {
            /*
             * 需要解析ServiceAddress
             * 支持的几种类型
             * 1. 全指定 ip:port
             * 2. 指定Ip:0 (由grpc自动选择port)
             * 3. 0.0.0.0:9090 (这是自动选择当前host ip)
             *
             * 支持环境变量设置serviceaddress, consuladdresss
             */
            if (!string.IsNullOrWhiteSpace(EnviromentParameters.ServiceAddress))
            {
                InnerLogger.Log(LoggerLevel.Info, $"使用环境变量中配置的serviceaddress:{EnviromentParameters.ServiceAddress}");
                serviceConfig.SetServiceAddress(EnviromentParameters.ServiceAddress);
            }

            if (!string.IsNullOrWhiteSpace(EnviromentParameters.ConsulAddress))
            {
                InnerLogger.Log(LoggerLevel.Info, $"使用环境变量中的consuladdress:{EnviromentParameters.ConsulAddress}");
                serviceConfig.SetConsulAddress(EnviromentParameters.ConsulAddress);
            }

            //解析ip
            var ipPortPair = serviceConfig.ServiceAddress.Split(':');

            ipPortPair[0] = NetHelper.GetIp(ipPortPair[0]);
            InnerLogger.Log(LoggerLevel.Info, "选择IP:" + ipPortPair[0]);

            server.Ports.Add(new ServerPort(ipPortPair[0], int.Parse(ipPortPair[1]),
                                            ServerCredentials.Insecure));

            server.Start();
            InnerLogger.Log(LoggerLevel.Info, "grpc服务启动");

            //处理端口
            if (ipPortPair[1] == "0") //PickUnused
            {
                ipPortPair[1] = server.Ports.First().BoundPort.ToString();
                InnerLogger.Log(LoggerLevel.Info, "自动选择port:" + ipPortPair[1]);
            }

            //重新设置ServiceAddress
            serviceConfig.ServiceAddress = $"{ipPortPair[0]}:{ipPortPair[1]}";

            var serviceRegisterProxy = new ServiceRegister(serviceConfig);
            await serviceRegisterProxy.Register();

            serviceDict[server.GetHashCode()] = serviceRegisterProxy;

            InnerLogger.Log(LoggerLevel.Info, "注册服务发现");
            return(server);
        }
        public void LocalServiceConfig_ConsulTagErrorFormat()
        {
            var localService = new ConsulLocalServiceConfig {
                ConsulTags = "123232"
            };

            Assert.Throws <FormatException>(() =>
            {
                localService.Validation();
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceRegister"/>
 /// </summary>
 public ServiceRegister(ConsulLocalServiceConfig config)
 {
     ServiceConfig = config;
 }