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. 使用pycharm or vscode来编写python代码?

    pycharm社区版可用于商业项目 pycharm社区版可用于商业项目,来源于官方的回答:Can I use Community Editions of JetBrains IDEs for deve ...

  2. 铁威马NAS如何开启二次验证提高系统安全性

    想到登录TNAS时更安全?直接开启OTP二次验证,通过 TNAS mobile生成的一次性密码登录NAS存储,简单设置,提升TOS系统访问安全性给你TNAS双重保护. 1.首先,确认你的TOS系统在5 ...

  3. dotTrace工具

    1.dotTrace简介: (1)dotTrace是一个包含了可用于.NET应用程序的性能分析器(dotTrace Performance)和内存分析器(dotTrace Memory)的套件: (2 ...

  4. 注意看,她叫小美,在地址栏输入URL地址后发生了什么?

    注意看,这个用户叫小美,他在地址栏输入了一串URL地址,然后竟然发生了不可思议的事情! 01.输入URL发生了什么? 从输入URL开始,到页面呈现出来,简单来说分为四个步骤: ① 建立连接:建立与服务 ...

  5. C++11(列表初始化+变量类型推导+类型转换+左右值概念、引用+完美转发和万能应用+定位new+可变参数模板+emplace接口)

    列表初始化 用法 在C++98中,{}只能够对数组元素进行统一的列表初始化,但是对应自定义类型,无法使用{}进行初始化,如下所示: // 数组类型 int arr1[] = { 1,2,3,4 }; ...

  6. 【转载】【Word】项目编号应用样式后出现黑框的解决方案

    本文中的宏代码来自: https://www.zhihu.com/question/38985919 Word使用项目编号后,出现黑框,如下图所示: 网上大多数解决方案在重新打开文档后,依然还是有黑框 ...

  7. Proxyless Mesh 在 Dubbo 中的实践

    背景 随着 Dubbo 3.1 的 release,Dubbo 在云原生的路上又迈出了重要的一步.在这个版本中添加了 Proxyless Mesh 的新特性,Dubbo Proxyless Mesh ...

  8. [深度学习] imgaug库使用笔记

    imgaug是一款非常有用的python图像增强库,非常值得推荐应用于深度学习图像增强.其包含许多增强技术,支持图像分类,目标检测,语义分割,热图.关键点检测等一系列任务的图像增强.本文主要介绍img ...

  9. GIT安装步骤记录以及Git 常用命令,忽略文件,推送本地代码到仓库示例以及报错解决

    下载 下载地址 git-scm.com 或 gitforwindows.org 安装(凡是下面没有给出图片的,都按默认选项就行) 选择安装组件 调整你的 path 环境变量 第一种是仅从 Git Ba ...

  10. 学习ASP.NET Core Blazor编程系列二十三——登录(2)

    学习ASP.NET Core Blazor编程系列文章之目录 学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应 ...