public List <string> ServiceDiscovery(string serviceName, bool isGreyTestServer = false, int greyTestFlag = 0) { try { ServiceEntity serviceEntity = new ServiceEntity(); serviceEntity.ServiceName = serviceName; List <CatalogService> resultService = new List <CatalogService>(); var services = _consulClient.Catalog.Service(serviceEntity.ServiceName).Result.Response.ToList(); if (services == null || (!services.Any())) { LogService.Default.Fatal("consul:找不到服务实例"); return(null); } //灰度测试中 if (greyTestFlag == 1) { if (isGreyTestServer) { resultService = services.Where(x => x.ServiceTags.Contains(SysConstants.GreyTestTagStr)).ToList(); } else { resultService = services.Where(x => !x.ServiceTags.Contains(SysConstants.GreyTestTagStr)).ToList(); } } else { resultService = services; } if (resultService == null || (!resultService.Any())) { LogService.Default.Fatal($"consul:找不到指定服务实例|{greyTestFlag}"); return(null); } return(resultService.Select(o => $"{ o.ServiceAddress}:{o.ServicePort}").ToList()); //return $"{service.Address}:{service.Port}"; } catch (Exception ex) { if (ex.InnerException != null) { LogService.Default.Fatal("服务发现失败(当前配置的服务写地址" + $"{_consulOption.Host}" + "):" + ex.InnerException.Message); } else { LogService.Default.Fatal("服务发现失败;(当前配置的服务写地址" + $"{_consulOption.Host}" + "):" + ex.Message); } return(null); } }
/// <summary> /// 注册服务 /// </summary> /// <param name="serviceEntity"></param> public void RegisterConsul(ServiceEntity serviceEntity) { //注册本地Client节点 try { var httpcheck = new AgentServiceCheck() //配置健康检查 { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5), //服务启动多久后注册 Interval = TimeSpan.FromSeconds(10), //健康检查时间间隔,或者称为心跳间隔 HTTP = $"http://{serviceEntity.IP}:{serviceEntity.Port}/{serviceEntity.CheckUrl}", //健康检查地址 Timeout = TimeSpan.FromSeconds(20), }; var registration = new AgentServiceRegistration()//配置服务注册参数 { Checks = new[] { httpcheck }, ID = serviceEntity.ServiceID(), Name = serviceEntity.ServiceName, Address = serviceEntity.IP, Port = serviceEntity.Port, Tags = serviceEntity.Tags //new[] { $"urlprefix-/{serviceEntity.ServiceName}" }//添加 urlprefix-/servicename 格式的 tag 标签,以便 Fabio 识别 }; //定义重试机制 5次重试 延时加载 var policy = Polly.Policy.HandleResult <List <CatalogService> >(r => { return(r.Find(x => x.ServiceID == registration.ID && x.ServiceName == registration.Name) == null); }).WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); List <CatalogService> findService = policy.Execute(() => { _consulClient.Agent.ServiceRegister(registration).Wait();//服务注册 var services = _consulClient.Catalog.Service(registration.Name).Result.Response.ToList(); return(services); }); } catch (Exception ex) { if (ex.InnerException != null) { LogService.Default.Fatal("服务注册失败:(当前配置的服务写地址" + $"{_consulOption.Host}" + "):" + ex.InnerException.Message); } else { LogService.Default.Fatal("服务注册失败:(当前配置的服务写地址" + $"{_consulOption.Host}" + "):" + ex.Message); } } }
/// <summary> /// 取消服务注册 /// </summary> /// <param name="serviceEntity"></param> public void DisposeRegister(ServiceEntity serviceEntity) { try { _consulClient.Agent.ServiceDeregister(serviceEntity.ServiceID()).Wait(); } catch (Exception ex) { if (ex.InnerException != null) { LogService.Default.Fatal("取消服务注册失败(当前配置的服务写地址" + $"{_consulOption.Host}" + "):" + ex.InnerException.Message); } else { LogService.Default.Fatal("取消服务注册失败;(当前配置的服务写地址" + $"{_consulOption.Host}" + "):" + ex.Message); } } }