Consul服务注册与发现
服务注册与发现
服务注册:
简单理解,就是有一个注册中心,我们的每个服务实例启动时,都去注册中心注册一下,告诉注册中心我的地址,端口等信息。同样的服务实例要删除时,去注册中心删除一下,注册中心负责维护这些服务实例的信息。
服务发现:
既然注册中心维护了各个服务实例的信息,那么客户端通过注册中心就很容易发现服务的变化了。
有了服务注册与发现,客户端就不用再去配置各个服务实例的地址,改为从注册中心统一获取。
那注册中心又是怎么保证每个地址的可用状态呢,假如某个实例挂了怎么办呢?原则上挂掉的实例不应该被客户端获取到,所以就要提到:健康检查 。
健康检查:
每个服务都需要提供一个用于健康检查的接口,该接口不具备业务功能。服务注册时把这个接口的地址也告诉注册中心,注册中心会定时调用这个接口来检测服务是否正常,如果不正常,则将它移除,这样就保证了服务的可用性。
常见注册中心有 Consul、ZooKeeper、etcd、Eureka。
Consul下载安装
Consul官网:https://www.consul.io/
Consul的主要功能有服务注册与发现、健康检查、K-V存储、多数据中心等。
Consul安装:很简单,直接在官网下载解压即可。
Consul运行:在consul.exe目录下打开命令行执行 consul.exe agent -dev
浏览器访问:http://localhost:8500/
Consul接入
1、引入 Consul 包
2、新增 Consul 配置
在 appsettings.json 中新增Consul 配置
"ConsulSetting": {
"ConsulAddress": "http://localhost:8500/", //注意,docker容器内部无法使用localhost访问宿主机器,如果是控制台启动的话就用localhost
"ServiceName": "MyApi", //服务名
"ServiceHealthApi": "/api/health", //心跳检查api
"ServiceHealthInterval": "10", //心跳检查频率
"ServiceHealthTimeout": "5" //心跳检查超时时间
},
3、增加 ConsulHelper 类
public static class ConsulHelper
{
public static void ConsulRegist(this IConfiguration configuration)
{
ConsulClient client = new ConsulClient(c =>
{
// c.Address = new Uri("http://localhost:8500/");
c.Address = new Uri(configuration["ConsulSetting:ConsulAddress"]);
//c.Datacenter = "dc1";
});
string ip = configuration["ip"];// ip 命令行参数必须传入
int port = int.Parse(configuration["port"]);//命令行参数必须传入
int weight = string.IsNullOrWhiteSpace(configuration["weight"]) ? 1 : int.Parse(configuration["weight"]);
var serviceName = configuration["ConsulSetting:ServiceName"];
var ServiceHealthApi = configuration["ConsulSetting:ServiceHealthApi"];
var serviceHealthInterval = string.IsNullOrWhiteSpace(configuration["ConsulSetting:ServiceHealthInterval"]) ? 10: int.Parse(configuration["ConsulSetting:ServiceHealthInterval"]);
var serviceHealthTimeout = string.IsNullOrWhiteSpace(configuration["ConsulSetting:ServiceHealthTimeout"]) ? 10 : int.Parse(configuration["ConsulSetting:ServiceHealthTimeout"]);
client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = Guid.NewGuid().ToString(),//服务实例唯一标识 //$"{serviceName}_{port}",//唯一的
Name = serviceName,//组名称-Group
Address = ip,//其实应该写ip地址
Port = port,//不同实例
Tags = new string[] { weight.ToString() },//标签
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(serviceHealthInterval),//间隔10s一次
HTTP= $"http://{ip}:{port}{ServiceHealthApi}", //心跳地址
Timeout = TimeSpan.FromSeconds(serviceHealthTimeout),//检测等待时间
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20)//失败后多久移除
}
});
}
}
4、在 Startup 启动时注册ConsulHelper
this.Configuration.ConsulRegist();
5、新增心跳检测 HealthController
[ApiController]
[Route("api/[controller]")]
public class HealthController : ControllerBase
{
/// <summary>
/// 健康检查接口
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Get() => Ok("ok");
}
6、通过Consul运行,查看结果
打开启动命令提示符窗口,进入项目dll目录
运行命令
dotnet MyAPI.dll --urls=http://127.0.0.1:5011 --ip=127.0.0.1 --port=5011
浏览器打开 http://localhost:5011/api/health ,查看结果
http://localhost:8500 控制台查看,多了一个MyApi
注:
dotnet MyAPI.dll --urls=http://127.0.0.1:5011 --ip=127.0.0.1 --port=5011 命令中: ip、port 必须和ConsulHelper 代码中的参数一致
weight 可以不指定,代码中默认1 ,也可以指定,如:
dotnet MyAPI.dll --urls=http://127.0.0.1:5011 --ip=127.0.0.1 --port=5011 --weight 1
7、再运行两个端口
打开两个窗口,分别运行
dotnet MyAPI.dll --urls=http://127.0.0.1:5012 --ip=127.0.0.1 --port=5012
dotnet MyAPI.dll --urls=http://127.0.0.1:5013 --ip=127.0.0.1 --port=5013
打开 http://localhost:5012/api/health 、 http://localhost:5013/api/health,查看结果
http://localhost:8500/ 控制台查看
查看详情
关闭5013端口,查看效果:
下一章我们将介绍,Consul在Docker 中运行等
https://blog.csdn.net/weixin_41003771/article/details/118726566