1、什么是Ocelot

  Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成。

2、前期准备工作

  新建一个Web API,返回IP+Port字符串(有利于我们直观感受)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; namespace StudyGateway.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
private IConfiguration _configuration;
public WeatherForecastController(IConfiguration configuration)
{
this._configuration = configuration;
} [HttpGet]
public string Get()
{
string ip = _configuration["IP"];
string port = _configuration["Port"];
return $@"{ip} {port}";
}
}
}

  CMD启动该站点

dotnet StudyGateway.dll --urls="http://*:5177" --ip="127.0.0.1" --port=5177

  显示截图:

  

3、开始设计网关(Gateway)
  创建一个新的Net Core Api项目,引用Ocelot包(注意版本对应 core 3.x 对应 Ocelot 15)

  安装完成后,修改Startup,因为这个站点只是作为网关,所以只需要以下配置

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware; namespace MyOcelot
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot(new ConfigurationBuilder().AddJsonFile("configuration.json").Build());
} public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseOcelot();
}
}
}

  新增一个configuration.json,作为Ocelot的配置文件(记得修改 [复制到输出目录])

  配置下游站点

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "127.0.0.1",
"Port": 5177
}
],
"UpstreamPathTemplate": "/GateWay/{url}", //这个url会转到上面的url
"UpstreamHttpMethod": [ "Get", "POST" ]
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration"
}
}

  启动项目,访问

  

  成功转载!!!

4、负载均衡策略

  前期准备工作的网站,通过CMD再启动一个新实例

dotnet StudyGateway.dll --urls="http://*:5178" --ip="127.0.0.1" --port=5178

  修改Ocelot配置文件,新增一个下游站点

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "127.0.0.1",
"Port": 5177
},
{
"Host": "127.0.0.1",
"Port": 5178
}
],
"UpstreamPathTemplate": "/GateWay/{url}", //这个url会转到上面的url
"UpstreamHttpMethod": [ "Get", "POST" ],
"LoadBalancerOptions": {
"Type": "RoundRobin" //负载均衡类型:轮询(其他类型也有,不一一列举了)
}
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration"
}
}

  重新生成,访问:

  

  

5、缓存策略

  安装Nuget包:Ocelot.Cache.CacheManager

  修改Ocelot配置文件,添加FileCacheOptions节点

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{url}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "127.0.0.1",
"Port": 5177
},
{
"Host": "127.0.0.1",
"Port": 5178
}
],
"UpstreamPathTemplate": "/GateWay/{url}", //这个url会转到上面的url
"UpstreamHttpMethod": [ "Get", "POST" ],
"LoadBalancerOptions": {
"Type": "RoundRobin" //负载均衡类型:轮询(其他类型也有,不一一列举了)
},
"FileCacheOptions": {
"TtlSeconds": 3,
"Region": "somename"
}
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration"
}
}

  刷新

http://localhost:57789/GateWay/weatherforecast

  访问后会缓存地址3秒。

  比如:第一次访问,转载到Port=5177,3秒内都会访问5177

  

最新文章

  1. HDU--跑道相遇
  2. echarts统计图使用
  3. Discrete.Differential.Geometry-An.Applied.Introduction(sig2013) 笔记
  4. 避免在WHERE条件中,在索引列上进行计算或使用函数,因为这将导致索引不被使用
  5. gulp ---攻略一
  6. 配置navigation bar外观
  7. System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
  8. 锋利的jQuery读书笔记---jQuery中Ajax--get、post等方法
  9. Layer 1: Single Objects
  10. makefile知识点归纳的
  11. CentOS 7 安装Python pip
  12. 分享两个细致、全面讲解Vue和React源码的链接
  13. JVM运行时数据区(二)
  14. 关于 Spring Security OAuth2 中 Feign 调用 Token 问题
  15. 调整JVM虚拟机的内存大小
  16. JavaScript 第六章总结: Getting to know the DOM
  17. Linux 下Qt实现守护进程实例(转)
  18. Django 之Form
  19. ChinaTest测试感悟
  20. 深入浅出React的一些细节——State

热门文章

  1. python 爬取豆瓣电影评论,并进行词云展示
  2. 我做了第一个ChatGPT .net api聊天库
  3. Redis——02 学习
  4. vivo 云原生容器探索和落地实践
  5. CLI框架:klish安装与使用
  6. 8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
  7. 一文告诉你AVM中设置字体的方法
  8. git remote update origin --prune命令失败
  9. NoClassDefFoundError的两种情况
  10. springcloud与微服务