Net Core 网关 Ocelot 简单案例
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 简单案例的更多相关文章
- asp.net core网关Ocelot的简单介绍& Ocelot集成Identity认证
文章简介 Ocelot网关简介 Ocelot集成Idnetity认证处理 Ocelot网关简介 Ocelot是一个基于netcore实现的API网关,本质是一组按特定顺序排列的中间件.Ocelot内 ...
- 服务网关Ocelot 入门Demo系列(01-Ocelot极简单Demo及负载均衡的配置)
[前言] Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butt ...
- .Net Core的API网关Ocelot使用 (一)
1.什么是API网关 API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API.它可以具有身份验证,监控,负载均衡,缓存,请求分片与管理,静态响应处理等.API ...
- .Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换
1.什么是API网关 API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API.它可以具有身份验证,监控,负载均衡,缓存,请求分片与管理,静态响应处理等.API ...
- .NET Core 微服务—API网关(Ocelot) 教程 [二]
上篇文章(.NET Core 微服务—API网关(Ocelot) 教程 [一])介绍了Ocelot 的相关介绍. 接下来就一起来看如何使用,让它运行起来. 环境准备 为了验证Ocelot 网关效果,我 ...
- 初探.Net Core API 网关Ocelot(一)
一.介绍 Ocelot 是基于.NetCore实现的开源的API网关,支持IdentityServer认证.Ocelot具有路由.请求聚合.服务发现.认证.鉴权.限流熔断等功能,并内置了负载均衡器与S ...
- .NET Core 微服务—API网关(Ocelot) 教程 [三]
前言: 前一篇文章<.NET Core 微服务—API网关(Ocelot) 教程 [二]>已经让Ocelot和目录api(Api.Catalog).订单api(Api.Ordering)通 ...
- 解决微服务网关Ocelot使用AddStoreOcelotConfigurationInConsul后请求404问题
一个小插曲,最近研究 netcore 微服务网关,在使用AddStoreOcelotConfigurationInConsul将配置存到consul后,任何经过网关的请求都出现404,并且没有任何有用 ...
- 庐山真面目之四微服务架构Consul和Ocelot简单版本实现
庐山真面目之四微服务架构Consul和Ocelot简单版本实现 一.简介 在上一篇文章<庐山真面目之三微服务架构Consul简单版本实现>中,我们已经探讨了如何搭建基于Consu ...
- Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网关Ocelot+Consul
相关文章 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网 ...
随机推荐
- python前言
目录 一.typora软件以及markdown语法介绍 1.输入标题的两种方法 2.无序列表 3.有序列表 4.在typora里插入多行代码块 5.制作表格 6.表情包 7.链接 8.Typora查看 ...
- week_Last
Andrew Ng 机器学习笔记 总结! 注:等下用xmind做一个树状图! 来回忆知识! 所以...树状图待补.... 最后,谢谢吴恩达老师!! 感谢吴恩达老师的无私付出!!
- 使用Typora写博客,图片即时上传
背景 习惯使用markdown的人应该都知道Typora这个神器,它非常简洁高效.虽然博客园的在线markdown编辑器也不错,但毕竟是网页版,每次写东西需要登录系统-进后台-找到文章-编辑-保存草稿 ...
- Ubuntu:Docker 容器操作
创建容器 1.docker run [option] 镜像名 [向启动容器中传入的命令] 常用可选说明 -i 表示以"交互模式"运行容器 -t 表示容器启动后会进入其命令行.加入这 ...
- Android的诞生
Android操作系统最初由Andy Rubin开发,刚开始主要支持手机,被Google收购后,对Android进行了改良,使其可以用于平板电脑等其它领域. 1.1.1 Android的发展史Andr ...
- [OpenCV实战]18 Opencv中的单应性矩阵Homography
目录 1 介绍 1.1 什么是Homography 1.2 使用Homography进行图像对齐 1.3 Homography的应用-全景拼接 2 Homography的计算 3 总结 4 参考 &l ...
- 路由分发、名称空间、视图层之必会的三板斧、JsonResponse对象、request获取文件、FBV与CBV、模板层语法传值
路由分发.名称空间.视图层之必会的三板斧.JsonResponse对象.request获取文件.FBV与CBV.模板层语法传值 一.昨日内容回顾 二.路由分发 1.djiango的每个应用都有主见的t ...
- Java入门与进阶 P-1.9+P-1.10
计算机的优先级 所有的数学运算都认为是从左向右运算的,Java 语言中大部分运算符也是从左向右结合的,只有单目运算符.赋值运算符和三目运算符例外,其中,单目运算符.赋值运算符和三目运算符是从右向左结合 ...
- statefulset详解及为何结合headless service部署有状态应用
1.1 有状态应用管理statefulset StatefulSet(有状态集,缩写为sts)常用于部署有状态的且需要有序启动的应用程序,比如在进行SpringCloud项目容器化时,Eureka的部 ...
- 记一次 .NET 某游戏网站 CPU爆高分析
一:背景 1. 讲故事 这段时间经常有朋友微信上问我这个真实案例分析连载怎么不往下续了,关注我的朋友应该知道,我近二个月在研究 SQLSERVER,也写了十多篇文章,为什么要研究这东西呢? 是因为在 ...