博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ocelot 配置初始
阅读量:4550 次
发布时间:2019-06-08

本文共 4327 字,大约阅读时间需要 14 分钟。

{  "ReRoutes": [    {      /*将用户的请求 /post/1 转发到 localhost/api/post/1*/      /*      DownstreamPathTemplate:转到的地址      DownstreamScheme:转到的请求协议      DownstreamHostAndPorts:转到的端口地址及端口信息      UpstreamPathTemplate:监听路由地址      UpstreamHttpMethod:监听路由请求类型 可用数组      Priority:路由的优先级Prority是大的会被优先选择      万能模版转发:                  {                      "DownstreamPathTemplate": "/{url}",                      "DownstreamScheme": "https",                      "DownstreamHostAndPorts": [                              {                                  "Host": "localhost",                                  "Port": 80,                              }                          ],                      "UpstreamPathTemplate": "/{url}",                      "UpstreamHttpMethod": [ "Get" ]                  }      */      "DownstreamPathTemplate": "/api/post/{postId}",      "DownstreamScheme": "https",      "DownstreamHostAndPorts": [        {          "Host": "localhost",          "Port": 80        }      ],      "UpstreamPathTemplate": "/post/{postId}",      "UpstreamHttpMethod": [ "Get" ],      "Priority": 1,      /*设置HttpHeaders*/      "AddHeadersToRequest": {},      "AddClaimsToRequest": {},      /*      鉴权      我们通过认证中的AllowedScopes 拿到claims之后,如果要进行权限的鉴别需要添加以下配置      "RouteClaimsRequirement": {         "UserType": "registered"      }      */      "RouteClaimsRequirement": {},      "AddQueriesToRequest": {},      "RequestIdKey": "",      /*      缓存      Ocelot可以对下游请求结果进行缓存 ,目前缓存的功能还不是很强大。它主要是依赖于CacheManager 来实现的,我们只需要在路由下添加以下配置即可      Region是对缓存进行的一个分区,我们可以调用Ocelot的 administration API来移除某个区下面的缓存       */      "FileCacheOptions": {        "TtlSeconds": 0,        "Region": "somename"      },      "ReRouteIsCaseSensitive": false,      "ServiceName": "",      /*      服务质量与熔断:熔断的意思是停止将请求转发到下游服务。当下游服务已经出现故障的时候再请求也是功而返,并且增加下游服务器和API网关的负担。这个功能是用的Pollly来实现的,我们只需要为路由做一些简单配置即可      ExceptionsAllowedBeforeBreaking 允许多少个异常请求      DurationOfBreak 熔断的时间,单位为秒      TimeoutValue 如果下游请求的处理时间超过多少则自如将请求设置为超时      */      "QoSOptions": {        "ExceptionsAllowedBeforeBreaking": 0,        "DurationOfBreak": 0,        "TimeoutValue": 0      },      /*      LeastConnection – 将请求发往最空闲的那个服务器      RoundRobin – 轮流发送      NoLoadBalance – 总是发往第一个请求或者是服务发现      */      "LoadBalancer": "", //将决定负载均衡的算法      /*      限流      对请求进行限流可以防止下游服务器因为访问过载而崩溃,      非常优雅的实现,我们只需要在路由下加一些简单的配置即可以完成      ClientWihteList 白名单      EnableRateLimiting 是否启用限流      Period 统计时间段:1s, 5m, 1h, 1d      PeroidTimeSpan 多少秒之后客户端可以重试      Limit 在统计时间段内允许的最大请求数量      */      "RateLimitOptions": {        "ClientWhitelist": [],        "EnableRateLimiting": false,        "Period": "",        "PeriodTimespan": 0,        "Limit": 0      },      /*      认证      如果我们需要对下游API进行认证以及鉴权服务的,则首先Ocelot 网关这里需要添加认证服务。这和我们给一个单独的API或者ASP.NET Core Mvc添加认证服务没有什么区别。      JWT  Token      public void ConfigureServices(IServiceCollection services)      {    var authenticationProviderKey = "ProviderKey";    services.AddAuthentication()        .AddJwtBearer(authenticationProviderKey, x =>        {        });}        然后在ReRoutes的路由模板中的AuthenticationOptions进行配置,只需要我们的AuthenticationProviderKey一致即可。        "AuthenticationOptions": {            "AuthenticationProviderKey": "ProviderKey",            "AllowedScopes": []        }    }]   添加Identity Server的认证也是一样public void ConfigureServices(IServiceCollection services){    var authenticationProviderKey = "TestKey";    var options = o =>        {            o.Authority = "identityserver4的地址";            o.ApiName = "api";             o.SupportedTokens = SupportedTokens.Both;            o.ApiSecret = "secret";        };    services.AddAuthentication()        .AddIdentityServerAuthentication(authenticationProviderKey, options);    services.AddOcelot();}      */      "AuthenticationOptions": {        "AuthenticationProviderKey": "",        "AllowedScopes": []      },      /*      */      "HttpHandlerOptions": {        "AllowAutoRedirect": true,        "UseCookieContainer": true,        "UseTracing": true      },      "UseServiceDiscovery": false,      "Key": "keys1"    }  ],  "Aggregates": [    {      "ReRouteKeys": [        "keys1",        "keys2"      ],      "UpstreamPathTemplate": "/"    }  ],  "GlobalConfiguration": {    "BaseUrl": "http://localhost:5002"  }}

 

转载于:https://www.cnblogs.com/liyouming/p/8856722.html

你可能感兴趣的文章
常用模块-02
查看>>
接口测试总结
查看>>
测试的基本概念
查看>>
【ZOJ】3209 Treasure Map
查看>>
ActiveMQ 消息队列服务
查看>>
《程序是给自己看的还是给别人看的》
查看>>
(12) PHP 随笔---Smarty模板引擎 单模板多缓存、局部不缓存 20--21
查看>>
【转】Math.Atan2 方法
查看>>
C++设计模式之工厂方法模式
查看>>
poj3984_bfs+回溯路径
查看>>
MyEclipse使用技巧
查看>>
[译]径向镜片反畸变滤波
查看>>
畅通工程-最小生成树+并查集
查看>>
top命令输出解释以及load average 详解及排查思路
查看>>
Ajax的封装
查看>>
Java传入参数个数不确定可用(Type ... values)
查看>>
POJ 2081
查看>>
记录下zend studio 的xdebug 在调试安装
查看>>
ES6阅读笔记
查看>>
数字基带信号分类
查看>>