vs2022 .net6 webapi
consul
Consul最常用的就是服务注册与发现,健康检查,接下来演示一下在Windows上如何使用
Step1:在官网下载consul.exe(下载较慢,耐心等待)
Step2:cmd到这个路径下,输入consul.exe agent -dev开启后,打开http://localhost:8500/
Step3:在代码中,进行服务注册
public static class ConsulHelper
{
public static void Init( this IConfiguration _configuration)
{
ConsulClient clinet = new ConsulClient(c =>
{
c.Address = new Uri("http://localhost:8500/");
c.Datacenter = "dcl";
});
string ip = _configuration["ip"];
int port = int.Parse(_configuration["port"]);
clinet.Agent.ServiceRegister(new AgentServiceRegistration() {
ID="service"+Guid.NewGuid(),
Name="shenqing",
Address = ip,
Port = port,
Check = new AgentServiceCheck() {
Interval = TimeSpan.FromSeconds(12),//间隔12秒一次
HTTP = $"http://{ip}:{port}/API/Health/Index",
Timeout = TimeSpan.FromSeconds(52),//检测等待时间
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)//失败多久后移除,最小值60秒
}
}).Wait();
}
}
Step4:示例 同一个服务,用三个不同的端口开启