The consul 1.NeGet 添加
2. Add consul
public static class ConsulRegister { public static void RegistConsul ( the this IConfiguration Configuration) { #region register Consul String IP Configuration = [ " IP " ] ?? " Localhost " ; // time to deploy to different servers can not be written or 0.0 127.0.0.1. 0.0, because the address to getting the service consumer calls // int port = int.Parse (the Configuration [ "Consul: ServicePort"]); // service port int port = String .IsNullOrWhiteSpace (the Configuration [ " port " ])? 44344: Int .Parse (Configuration [ " Port " ]); ConsulClient Client = new new ConsulClient (obj => { obj.Address = new new Uri ( " http://127.0.0.1:8500 " ); obj.Datacenter = " DC1 " ; }); // the registrar consul the Task <WriteResult> Result = client.Agent.ServiceRegister ( new new AgentServiceRegistration () { ID = " apiserviceTest_ " + Guid.NewGuid (), // service number, can not be duplicated = The Name " apiserviceTest " , // service name - use is this future calls Address = ip, Port = Port, Tags = new new String [] {}, // can be used to set the weight of the Check = new new AgentServiceCheck () { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds ( 5 ), // service stops after long unregister interval = TimeSpan.FromSeconds ( 10 ), // health check interval, called the heartbeat interval or HTTP = $ " HTTP: // {ip} : {Port} / API / health " , // health check address, Timeout = TimeSpan.FromSeconds(5)//超时 } }); #endregion } }
3. In the Startup registered Consul
this.Configuration.RegistConsul();