consul 使用总结 & Nginx 负责均衡,最大连接数据,超时次数,超时等待时间,权重
-
consul agnet -dev 启动consul
-
启动服务,注册服务:
dotnet OrderServer.dll --urls="http://:5189" --ip="127.0.0.1“ --port=5189
dotnet OrderServer.dll --urls="http://:5188" --ip="127.0.0.1“ --port=5188
3.服务注入及健康检查接口:
program.cs:
app.MapGet("/Api/Health/Index", (HttpContext httpContext) =>
{
Console.WriteLine($"This is Health Check");
httpContext.Response.StatusCode = (int)HttpStatusCode.OK;
return "OK";
});
app.Configuration.ConsulRegist();//在进程启动过程完成-且只完成一次.
4.注册服务代码:
ConsulRegist():
public static class ConsulHelper
{
public static void ConsulRegist(this IConfiguration configuration)
{
ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri("http://localhost:8500/");
c.Datacenter = "dc1";
});//找到consul
string ip = string.IsNullOrWhiteSpace(configuration["ip"]) ? "127.0.0.1" : configuration["ip"];
int port = string.IsNullOrWhiteSpace(configuration["port"]) ? 5726 : int.Parse(configuration["port"]);//命令行参数必须传入
int weight = string.IsNullOrWhiteSpace(configuration["weight"]) ? 1 : int.Parse(configuration["weight"]);
client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = "service" + Guid.NewGuid(),
Name = "OrderService",//Group--分组,通过这个方法注册的服务都属于OrderService
Address = ip,//
Port = port,//
Tags = new string[] { weight.ToString() },//标签
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(12),//间隔12s一次
HTTP = $"http://{ip}:{port}/Api/Health/Index",//控制器
Timeout = TimeSpan.FromSeconds(5),//检测等待时间
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(120)//失败后多久移除
}
});
//命令行参数获取
Console.WriteLine($"注册成功:{ip}:{port}--weight:{weight}");
}
}
--------------Nginx 负责均衡,最大连接数据,超时次数,超时等待时间,权重 -----------------------
1.api:
dotnet OrderServer.dll --urls="http://:5177" --ip="127.0.0.1“ --port=5177
dotnet OrderServer.dll --urls="http://:5177" --ip="127.0.0.1“ --port=5178
2.nginx配置:
代理服务
upstream myserver{
server 127.0.0.1:5177 max_fails=3 fail_timeout=20s weight=100 max_conns=10;
server 127.0.0.1:5178 max_fails=3 fail_timeout=20s weight=100 max_conns=10;
}
server {
listen 8082;
server_name localhost;
location / {
proxy_next_upstream error timeout invalid_header http_502 http_504 http_404;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 20s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_pass http://myserver;
}
nginx -s reload
标签:http,weight,--,ip,consul,等待时间,proxy,超时,port From: https://www.cnblogs.com/chenshaojun2008/p/17551372.html