net5集成consul
1、新建站点,net5
2、nuget添加引用consul,目前最新版是1.6.1.1
3、添加扩展类
public static class ConsulExtensions { public static void UseConsul(this IConfiguration configuration, ConsulConfigInfo consulInfo) { if (consulInfo == null) consulInfo = new ConsulConfigInfo(); ConsulClient client = new ConsulClient(m => { m.Address = new Uri(consulInfo.Address); m.Datacenter = consulInfo.Datacenter; m.Token = consulInfo.Token; }); string ip = consulInfo.RegIP; int port = consulInfo.RegPort; string[] tags = consulInfo.Tags; var agentsvrInfo = new AgentServiceRegistration() { ID = consulInfo.agentsvrInfo.Name + "-" + ip + "-" + port.ToString(), Name = consulInfo.agentsvrInfo.Name, Address = ip, Port = port, Tags = tags, Check = new AgentServiceCheck() { Interval = TimeSpan.FromSeconds(consulInfo.agentsvrInfo.IntervalSec), HTTP = consulInfo.agentsvrInfo.CheckAddress,//$"http://{ip}:{port}/Api/Health/Index", Timeout = TimeSpan.FromSeconds(consulInfo.agentsvrInfo.CheckTimeout), DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(consulInfo.agentsvrInfo.DeregisterSecond) } }; client.Agent.ServiceRegister(agentsvrInfo); } }
4、在Config中使用consul
this.Configuration.UseConsul(config)//添加自己的配置类
5、配置文件consulcfg.json:
{ "Address": "http://192.168.1.118:8500", "Datacenter": "dc-center", "Token": "3G4C2T6E-6524-7JKD-MJ85-5336EB6N6530", "RegIP": "192.168.1.18", "RegPort": 6666, "Tags": [ "1" ], "AgentServiceRegInfo": { "Name": "mytestapi", "IntervalSec": 5, "CheckAddress": "http://192.168.1.18:6666/api/MyHealth/Check", "CheckTimeout": 10, "DeregisterSecond": 2 } }
至此集成完成。consul的安装配置看另外的文章,consul安装