/// <summary> /// 获取灰度IP /// </summary> /// <param name="serviceName"></param> /// <param name="serviceVersion"></param> /// <param name="serviceId"></param> /// <returns></returns> public async Task <List <string> > GetServiceTestIp(string serviceName, string serviceVersion, string serviceId) { var result = Get <List <string> >("testip-" + serviceId); if (result == null || result.Count <= 0) { result = (await ConsulKVManager.Instance.GetValue(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.ABTestingIp, serviceName, serviceVersion, serviceId))).Split('\n').ToList(); InitCache(); } return(result); }
/// <summary> /// 获取服务状态 /// </summary> /// <param name="serviceName"></param> /// <param name="serviceVersion"></param> /// <param name="serviceId"></param> /// <returns></returns> public async Task <string> GetServiceStatus(string serviceName, string serviceVersion, string serviceId) { var result = Get <string>("status-" + serviceId); if (string.IsNullOrEmpty(result)) { result = await ConsulKVManager.Instance.GetValue(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.ServiceStatus, serviceName, serviceVersion, serviceId)); InitCache(); } return(result); }
/// <summary> /// 重新注册HTTP服务 /// </summary> /// <param name="serviceName"></param> /// <param name="version"></param> /// <param name="servicePort"></param> /// <param name="checking"></param> /// <returns></returns> public async Task <string> ReRegister(string serviceName, string version, int servicePort, bool checking = true) { string serviceAddress = serviceName; // 先注销 var allServices = await _consul.Agent.Services(); var services = allServices.Response.Where(m => m.Value.Service == serviceName && m.Value.Port == servicePort && m.Value.Tags.Contains(version)); foreach (var service in services) { await _consul.Agent.ServiceDeregister(service.Value.ID); } // 再重新注册 var registration = new AgentServiceRegistration() { ID = serviceName + "@" + Guid.NewGuid(), Address = serviceAddress, Name = serviceName, Tags = new[] { version }, Port = servicePort, }; if (checking) { registration.Check = new AgentServiceCheck { HTTP = "http://127.0.0.1:" + servicePort + "/Health", Interval = new TimeSpan(50000000) }; } string status = await ConsulKVManager.Instance.GetValue("service/abtesting/servicedefaultstatus"); if (!string.IsNullOrEmpty(status) && status == "1") { await ConsulKVManager.Instance.SetValue(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.ServiceStatus, serviceName, version, registration.ID), "测试"); await ConsulKVManager.Instance.SetValue(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.ABTestingIp, serviceName, version, registration.ID), ""); } await ConsulKVManager.Instance.SetValue(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.RegisterInfo, serviceName, version, registration.ID), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); var result = await _consul.Agent.ServiceRegister(registration); return(result.StatusCode.ToString()); }
/// <summary> /// 注销服务 /// </summary> /// <param name="servicePort"></param> /// <returns></returns> public async Task <string> Deregister(int servicePort) { var allServices = await _consul.Agent.Services(); var services = allServices.Response.Where(m => m.Value.Port == servicePort); foreach (var service in services) { await ConsulKVManager.Instance.DeletePath(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.ServiceStatus, service.Value.Service, service.Value.Tags.FirstOrDefault(), service.Value.ID)); await ConsulKVManager.Instance.DeletePath(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.RegisterInfo, service.Value.Service, service.Value.Tags.FirstOrDefault(), service.Value.ID)); await ConsulKVManager.Instance.DeletePath(ConsulKVConfig.GetServiceKVPath(ServiceKVConfigPath.ABTestingIp, service.Value.Service, service.Value.Tags.FirstOrDefault(), service.Value.ID)); LogHelper.WriteLog(this.GetType(), "服务被注销了:" + service.Value.ID); await _consul.Agent.ServiceDeregister(service.Value.ID); } return("200"); }