一. 自身负载
1. 含义
实现Ocelot转发多个业务服务器,不去Consul中获取,直接在配置文件中配置。
LoadBalancer将决定负载均衡的算法
- LeastConnection – 将请求发往最空闲的那个服务器
- RoundRobin – 轮流发送
- NoLoadBalance – 总是发往第一个请求或者是服务发现
2. 用到的项目
OcelotGateWay:网关
GoodsService:业务服务器
OrderService:业务服务器
3. 测试步骤
(1).启动Consul,【consul.exe agent -dev】,因为业务服务器中进行了服务注册,与Ocelot无关
(2).启动网关:【dotnet OcelotGateWay.dll --urls="http://*:7020" --ip="127.0.0.1" --port=7020 】
其中GoodsService配置LeastConnection算法,OrderService配置RoundRobin算法。
配置文件
{ "Routes": [ { "DownstreamPathTemplate": "/api/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "127.0.0.1", "Port": 7001 }, { "Host": "127.0.0.1", "Port": 7002 }, { "Host": "127.0.0.1", "Port": 7003 } ], "UpstreamPathTemplate": "/GoodsService/{url}", "LoadBalancerOptions": { "Type": "RoundRobin" //轮询 }, "UpstreamHttpMethod": [ "Get", "Post" ] }, { "DownstreamPathTemplate": "/api/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "127.0.0.1", "Port": 7004 }, { "Host": "127.0.0.1", "Port": 7005 }, { "Host": "127.0.0.1", "Port": 7006 } ], "UpstreamPathTemplate": "/OrderService/{url}", "LoadBalancerOptions": { "Type": "LeastConnection" //最少连接数 }, "UpstreamHttpMethod": [ "Get", "Post" ] } ] }