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)-网 ...
随机推荐
- java抽象类的定义和使用
1. 抽象类的规则 ● 抽象类不可以被实例化,也就是不能被new,会出现编译错误.抽象类如果想实例化可以通过非抽象子类的方式去实现. ● 抽象类中不一定有抽象方法,但有抽象方法的类一定是抽象类.而且抽 ...
- Java单例模式的最佳实践?
"读过书,--我便考你一考.茴香豆的茴字,怎样写的?"--鲁迅<孔乙己> 0x00 大纲 目录 0x00 大纲 0x01 前言 0x02 单例的正确性 new关键字 c ...
- Relational Learning with Gated and Attentive Neighbor Aggregator for Few-Shot Knowledge Graph Completion 小样本关系学习论文解读
小样本知识图补全--关系学习.利用三元组的邻域信息,提升模型的关系表示学习,来实现小样本的链接预测.主要应用的思想和模型包括:GAT.TransH.SLTM.Model-Agnostic Meta-L ...
- input限制只能输入汉字
<el-form class="det_foot" :model="form" :rules="rules" ref="fo ...
- Window注册表的学习记录
注册表的结构: 概述:注册表是一种树状结构,在很早之前是系统的其他配置信息存放的文件,通常以.ini结尾的文件,因为数量太多不方便管理,后来就整合在一起形成了注册表.你可以按住键盘win+r,然后输入 ...
- [python] 基于NetworkX实现网络图的绘制
NETWORK CHART(网络图) 代码下载地址 网络图 (或图表或图形)显示了一组实体之间的互连.每个实体由一个或多个节点表示.节点之间的连接通过链接(或边)表示.网络的理论与实现是一个广阔的研究 ...
- 阿里云kafka使用记录(python版本)
kafka端 consumer vpc版代码 import socket from kafka import KafkaConsumer from kafka.errors import Ka ...
- strapi系列--如何自定义非界面化的接口,定制化自己的业务逻辑
为什么要进行后端定制呢? 在实际开发过程中,项目中有些需求是不需要创建界面化接口的,需要我们定制化自己的业务逻辑,那么我们该如何处理这个需求呢?本文以图文并茂的形式,定制一个我们自己的业务逻辑接口. ...
- hashmap的一些性能测试
目录 0.前言 1.准备工作. 1.1模拟哈希冲突 1.2 java的基准测试. 2.测试初始化长度 3.模拟一百万个元素put,get的差异. 4.模拟无红黑树情况下get效率 4.1 将rando ...
- day14-JdbcTemplate-01
JdbcTemplate-01 看一个实际需求: 如果希望使用spring框架做项目,Spring框架如何处理对数据库的操作呢? 方案一:使用之前的JdbcUtils类 方案二:spring提供了一个 ...