Ocelot+Consul的使用配置

客户端
代码配置 "ConsulRegisterOptions": { "Address": "http://localhost:8500", //"http://host.docker.internal:8500", //Consul 客户端地址 "HealthCheck": "/healthcheck", //健康检查地址 "Name": "ServiceA", //服务名 "Ip": "localhost", //"host.docker.internal", //服务绑定Ip "Port": "5032" //服务绑定端口 } 代码配置 "ConsulRegisterOptions": { "Address": "http://localhost:8500", //"http://host.docker.internal:8500", //Consul 客户端地址 "HealthCheck": "/healthcheck", //健康检查地址 "Name": "ServiceA", //服务名 "Ip": "localhost", //"host.docker.internal", //服务绑定Ip "Port": "5096" //服务绑定端口 } 这里用于ocelot切换,两个项目

  

客户端
builder.Services.Configure<ConsulRegisterOptions>(builder.Configuration.GetSection("ConsulRegisterOptions")); builder.Services.AddConsulRegister();

  

app.UseHealthCheckMiddleware();

app.MapGet("/test", (IConfiguration configuration) =>
{
    return $"{Assembly.GetExecutingAssembly().FullName};当前时间:{DateTime.Now:G};Port:{configuration["ConsulRegisterOptions:Port"]}";
});

await app.Services.GetService<IConsulRegister>()!.ConsulRegistAsync();
ocelot端这里服务端名称ServiceName要和客户端一样,不然gg,找了半天,操蛋
{
"Routes": [ { "DownstreamPathTemplate": "/{url}", //下游(转发的服务地址模板) "DownstreamScheme": "http", "UpstreamPathTemplate": "/ocelot/{url}", //上游(请求路径模板) "UpstreamHttpMethod": [ "Get", "Post" ], "ServiceName": "ServiceA", "UseServiceDiscovery": true, "LoadBalancerOptions": { "Type": "RoundRobin" //轮询 } } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:5181", "ServiceDiscoveryProvider": { //consul 配置信息 "Host": "localhost", "Port": 8500, "Type": "Consul" } } }

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot().AddConsul();


await app.UseOcelot();

 

 

posted @ 2024-02-12 19:00  孤海飞雁  阅读(46)  评论(0编辑  收藏  举报