二、.Net Core搭建Ocelot

上一篇文章介绍了Ocelot的基本概念:https://www.cnblogs.com/yangleiyu/p/15043762.html

本文介绍在.net core中如何使用ocelot。

Ocelot是系统中对外暴露的一个请求入口,所有外部接口都必须通过这个网关才能向下游API发出请求

1、Nuget引用Ocelot(注意版本,我用的是16.0.1)

2、根目录添加配置文件Ocelot.json

csharp;gutter:true; { "ReRoutes": [], "GlobalConfiguration": {} }</p> <pre><code> 说明:ReRoutes是一个数组,将会包含服务器的路由配置,GlobalConfiguration则是一个全局配置项。 3、修改Program.cs,引用添加的配置文件 ![二、.Net Core搭建Ocelot](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230809/1535554-20221031164055355-477075830.png) 4、修改Startup.cs注册服务 ![二、.Net Core搭建Ocelot](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230809/1535554-20221031164102171-1562895155.png) 5、配置文件 ![二、.Net Core搭建Ocelot](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230809/1535554-20221031164114650-230672430.png) ;gutter:true;
{
//全局配置
"GlobalConfiguration": {
"BaseUrl": "http://192.168.50.118:8003/" //网关暴露的的地址。
},
//路由配置
"routes": [
{
///{url}转发所有
//"UpstreamHost": "localhost:4023"转发特定服务
"UpstreamPathTemplate": "/QiantoonService/Oam", //上游Api请求路由规则
"DownstreamPathTemplate": "/QiantoonService/Oam/Oam", //网关转发到下游路由规则
"UpstreamHttpMethod": [ "GET", "POST" ], //上下游支持请求方法
"DownstreamScheme": "http", //下游服务配置
"DownstreamHostAndPorts": [
{
"Host": "192.168.50.118", //下游地址
"Port": 8001 //下游端口号
}
]
},
{
"UpstreamPathTemplate": "/QiantoonService/SelfReg", //上游Api请求路由规则
"DownstreamPathTemplate": "/QiantoonService/SelfReg/SelfReg", //网关转发到下游路由规则
"UpstreamHttpMethod": [ "GET", "POST" ], //上下游支持请求方法
"DownstreamScheme": "http", //下游服务配置
"DownstreamHostAndPorts": [
{
"Host": "192.168.50.118", //下游地址
"Port": 8002 //下游端口号
}
]
}
]
}

其他说明:

GlobalConfiguration,它是一个全局配置项,通常我们都要在这个配置项中添加一个属性 BaseUrl,BaseUrl就是Ocelot服务对外暴露的Url。

"GlobalConfiguration": {"BaseUrl": "http://localhost:4727"}

ReRoutes是一个数组,其中的每一个元素代表了一个路由,而一个路由所包含的所有可配置参数如下:

{
    "DownstreamPathTemplate": "/",
    "UpstreamPathTemplate": "/",
    "UpstreamHttpMethod":
    [
        "Get"
    ],
    "AddHeadersToRequest": {},
    "AddClaimsToRequest": {},
    "RouteClaimsRequirement": {},
    "AddQueriesToRequest": {},
    "RequestIdKey": "",
    "FileCacheOptions":
    {
        "TtlSeconds": 0,
        "Region": ""
    },
    "ReRouteIsCaseSensitive": false,
    "ServiceName": "",
    "DownstreamScheme": "http",
    "DownstreamHostAndPorts":
    [
        {
        "Host": "localhost",
        "Port": 8001,
        }
    ],
    "QoSOptions":
    {
        "ExceptionsAllowedBeforeBreaking": 0,
        "DurationOfBreak": 0,
        "TimeoutValue": 0
    },
    "LoadBalancer": "",
    "RateLimitOptions":
    {
        "ClientWhitelist": [],
        "EnableRateLimiting": false,
        "Period": "",
        "PeriodTimespan": 0,
        "Limit": 0
    },
    "AuthenticationOptions":
    {
        "AuthenticationProviderKey": "",
        "AllowedScopes": []
    },
    "HttpHandlerOptions":
    {
        "AllowAutoRedirect": true,
        "UseCookieContainer": true,
        "UseTracing": true
    },
    "UseServiceDiscovery": false
}

具体含义介绍:

Downstream 下游服务配置

UpStream 上游服务配置

Aggregates 服务聚合配置

ServiceName, LoadBalancer, UseServiceDiscovery 服务发现配置

AuthenticationOptions 服务认证配置

RouteClaimsRequirement Claims 鉴权配置

RateLimitOptions 限流配置

FileCacheOptions 缓存配置

QosOptions 服务质量与熔断配置

DownstreamHeaderTransform 头信息转发配置

注意

配置文件中” routes “关键字为新版本,旧版本关键字为” ReRoutes

此处巨坑,小杨被坑了半天

Original: https://www.cnblogs.com/yangleiyu/p/16847439.html
Author: yangleiyu
Title: 二、.Net Core搭建Ocelot

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/799651/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球