public async Task <List <ServiceMeta> > FindAll() { List <ServiceMeta> list = new List <ServiceMeta>(); if (_lastIndex > 0) { _queryOptions.WaitIndex = _lastIndex + 1; } var reslut = await this._client.Health.Service(Constants.SERVICE_NAME, _serviceCategory, true, _queryOptions); if (reslut.StatusCode == System.Net.HttpStatusCode.OK) { if (reslut.LastIndex > this._lastIndex) { _lastIndex = reslut.LastIndex; if (reslut.Response != null && reslut.Response.Length > 0) { foreach (ServiceEntry entry in reslut.Response) { string[] splitId = entry.Service.ID.Split('$'); if (splitId.Length != 2) { continue; } string serviceId = splitId[1]; if (!this._requireServices.Contains(serviceId)) { continue; } ServiceMeta meta = new ServiceMeta { Id = entry.Service.ID, ServiceName = splitId[0], Address = entry.Service.Address, Port = entry.Service.Port }; list.Add(meta); } } } } else if (reslut.StatusCode == System.Net.HttpStatusCode.NotFound) { //找不到的话 就不做处理了 } else { throw new Exception($"call find services error,return code {reslut.StatusCode}"); } return(list); }
public async Task Register(ServiceMeta service) { await this._client.Agent.ServiceDeregister(service.ServiceId.ToString()); var reg = new AgentServiceRegistration { ID = service.Id, Name = service.ServiceName, Address = service.Address, Port = service.Port, Tags = service.Tags }; reg.Check = new AgentServiceCheck(); reg.Check.DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(50); reg.Check.TCP = $"{service.Address}:{service.Port}"; reg.Check.Interval = TimeSpan.FromSeconds(10); await this._client.Agent.ServiceRegister(reg); }