二、.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

{    
    "ReRoutes": [],    
    "GlobalConfiguration": {}
}

说明:ReRoutes是一个数组,将会包含服务器的路由配置,GlobalConfiguration则是一个全局配置项。

3、修改Program.cs,引用添加的配置文件

 

4、修改Startup.cs注册服务

 

5、配置文件

 配置如下:

{
  //全局配置
  "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

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

posted @ 2022-11-01 13:59  yangleiyu  阅读(875)  评论(4编辑  收藏  举报