上一篇文章介绍了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

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

原文地址:http://www.cnblogs.com/yangleiyu/p/16847439.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性