Ocelot 负载均衡

Ocelot 负载均衡

以上文中项目为例:https://www.cnblogs.com/1285026182YUAN/p/15234331.html

 1. 在OService1与OService2中 都增加以下控制器接口

namespace OService1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TeaController : ControllerBase
    {
        [Route("GetList")]
        [HttpGet]
        public IActionResult GetList()
        {
            return new JsonResult(new { name = "tea", Host = HttpContext.Request.Host.Value });
        }
    }
}

 访问接口:

https://localhost:6001/api/tea/getlist
https://localhost:6002/api/tea/getlist

2. 修改 OService.json文件配置

{
  "Routes": [
    //路由一
    {
      "DownstreamPathTemplate": "/api/Tea/GetList", //下游路径
      "DownstreamScheme": "https", //http,https
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost", //下游地址
          "Port": 6001 //下游端口
        },
        {
          "Host": "localhost", //下游地址
          "Port": 6002 //下游端口
        }
      ],
      "UpstreamPathTemplate": "/ocelot/GetTea", //上游路径
      "UpstreamHttpMethod": [ "Get" ],
      "LoadBalancerOptions": { "Type": "RoundRobin" } //轮询
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5001"
  }
}

访问地址:

https://localhost:5001/ocelot/gettea

测试: 

LeadConnection负载均衡器算法共有4种:

  • LeastConnection 把新请求发送到现有请求最少的服务上

  • RoundRobin 轮询可用的服务并发送请求

  • NoLoadBalancer 不负载均衡,总是发往第一个可用的下游服务

  • CookieStickySessions 使用cookie关联所有相关的请求到制定的服务

参考:http://letyouknow.net/ocelot/ocelot-tutorial-2.html
项目:https://gitee.com/wuxincaicai/ocelothost.git

原文地址:https://www.cnblogs.com/1285026182YUAN/p/15238452.html