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

  

Net Core 网关 Ocelot 简单案例的更多相关文章

  1. asp.net core网关Ocelot的简单介绍& Ocelot集成Identity认证

    文章简介  Ocelot网关简介 Ocelot集成Idnetity认证处理 Ocelot网关简介 Ocelot是一个基于netcore实现的API网关,本质是一组按特定顺序排列的中间件.Ocelot内 ...

  2. 服务网关Ocelot 入门Demo系列(01-Ocelot极简单Demo及负载均衡的配置)

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

  3. .Net Core的API网关Ocelot使用 (一)

    1.什么是API网关 API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API.它可以具有身份验证,监控,负载均衡,缓存,请求分片与管理,静态响应处理等.API ...

  4. .Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换

    1.什么是API网关 API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API.它可以具有身份验证,监控,负载均衡,缓存,请求分片与管理,静态响应处理等.API ...

  5. .NET Core 微服务—API网关(Ocelot) 教程 [二]

    上篇文章(.NET Core 微服务—API网关(Ocelot) 教程 [一])介绍了Ocelot 的相关介绍. 接下来就一起来看如何使用,让它运行起来. 环境准备 为了验证Ocelot 网关效果,我 ...

  6. 初探.Net Core API 网关Ocelot(一)

    一.介绍 Ocelot 是基于.NetCore实现的开源的API网关,支持IdentityServer认证.Ocelot具有路由.请求聚合.服务发现.认证.鉴权.限流熔断等功能,并内置了负载均衡器与S ...

  7. .NET Core 微服务—API网关(Ocelot) 教程 [三]

    前言: 前一篇文章<.NET Core 微服务—API网关(Ocelot) 教程 [二]>已经让Ocelot和目录api(Api.Catalog).订单api(Api.Ordering)通 ...

  8. 解决微服务网关Ocelot使用AddStoreOcelotConfigurationInConsul后请求404问题

    一个小插曲,最近研究 netcore 微服务网关,在使用AddStoreOcelotConfigurationInConsul将配置存到consul后,任何经过网关的请求都出现404,并且没有任何有用 ...

  9. 庐山真面目之四微服务架构Consul和Ocelot简单版本实现

    庐山真面目之四微服务架构Consul和Ocelot简单版本实现 一.简介      在上一篇文章<庐山真面目之三微服务架构Consul简单版本实现>中,我们已经探讨了如何搭建基于Consu ...

  10. Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网关Ocelot+Consul

    相关文章 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网 ...

随机推荐

  1. 基于EasyCode定制Mybatisplus全自动单表实现:新增/批量新增/修改/批量删除/分页查询/ID查询

    基于EasyCode定制Mybatisplus全自动单表实现CRUD接口 分页查询 ID查询 新增 批量新增 修改 批量删除 注意使用了MybatisPlus的自动填充功能,和insertBatchS ...

  2. 跟我学Python图像处理丨带你入门OpenGL

    摘要:介绍Python和OpenGL的入门知识,包括安装.语法.基本图形绘制等. 本文分享自华为云社区<[Python图像处理] 二十七.OpenGL入门及绘制基本图形(一)>,作者:ea ...

  3. Jenkins服务器上创建项目和配置

    大体步骤:General(基础配置)-->源码管理-->构建触发器-->构建环境-->构建-->构建后操作 1.创建一个工程 2.General(基础配置) 仅需填写标准 ...

  4. 过debugger的几种方法+案例

    受益匪浅 https://mp.weixin.qq.com/s/559so0RheeiQdA670J23yghttps://blog.csdn.net/weixin_43834227/article/ ...

  5. Vue element 自定义表单验证(验证手机号)

    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" ...

  6. 总结开源项目中的常见坏实践(Bad Practice)

    一些开源项目包含了各种编程的最佳实践供人参考学习和借鉴.但是也有一些开源项目虽然初衷是好的.但是包含了一些代码的坏实践.特别是对于一部分刚入行的大学生来说,可能会给到一些错误的示范.于是在此列举一些项 ...

  7. git remote update origin --prune命令失败

    1.问题描述 我使用git remote update origin --prune命令更新远程分支上的代码,结果出现如下报错. 2.产生原因 本地关联的远程仓库失效了,需要重新再关联一下. 3.解决 ...

  8. JavaScript 浅拷贝和深拷贝

    JavaScript 中的拷贝分为两种:浅拷贝和深拷贝. 一.浅拷贝 浅拷贝是指在拷贝过程中,只拷贝一个对象中的指针,而不拷贝实际的数据.所以,浅拷贝中修改新对象中的数据时,原对象中的数据也会被改变. ...

  9. 不止ChatGPT,谷歌云 AI 方案早已厉兵秣马!

    近日 ChatGPT 爆火,掀起热议,能聊天能写代码,还能写策划稿,AI 似乎已逐渐变得无所不能. 不过在 AI 对话上,谷歌早在17年就提出了 Dialogflow 这一AI对话平台.相比起 Cha ...

  10. Java 进阶P-6.4+P-6.5

    狐狸和兔子 狐狸和兔子都有年龄 当年龄到了一定的上限就会自然死亡 狐狸可以随即决定在周围的兔子中吃一个 狐狸和兔子可以随即决定生一个小的,放在旁边的空的格子里 如果不吃也不生,狐狸和兔子可以随机决定走 ...