ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.net ocelot网关入门操作

2021-06-29 10:32:08  阅读:227  来源: 互联网

标签:网关 UpstreamPathTemplate 请求 0.1 ocelot http net 模板


记录微服务的搭建过程

介绍

Ocelot是一个用.NET Core实现且开源的网关,只需要配置配置就能完成路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly Tracing集成等功能。下面我会对这些功能的配置和说明。

ocelot的安装

在这里插入图片描述
选择第一个根据自己的vs选择不同的版本

ocelot的搭建过程

配置startup
using一下命名空间
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
在这里插入图片描述
新建一个配置文件
当然你也可以把配置信息写在appsettings.json里面
在这里插入图片描述
配置完后需要在program进行注册
在这里插入图片描述
额外补一点配置文件的优先级
自定义配置文件>命令行>环境(launchSettings)>appsettings>UseSetting

ocelot配置文件介绍

 {
  "ReRoutes": [
  ],//路由配置
  "GlobalConfiguration": {} //全局配置
}

路由ReRoutes基本配置
假设我的网关地址是 www.wangguan.vip/post/test/get?id=3
他会转发到路由 http://127.0.0.1:5005/api/test/get?id=3
或者是http://127.0.0.1:5006/api/test/get?id=3

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{postId}", //下游(指访问服务的接口)
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "127.0.0.1",
          "Port": 5005
        },
        {
          "Host": "127.0.0.1",
          "Port": 5006
        }
      ],
       "LoadBalancer": "LeastConnection ", //负载均衡策略
      "UpstreamPathTemplate": "/post/{postId}", //上游也就是用户输入的请求Url模板
      "UpstreamHttpMethod": [ "Get" ], //上游请求http方法,可使用数
      "Priority":1,//优先级
    }
  ],
  "GlobalConfiguration": {}
}
  • DownstreamPathTemplate 下游模板
  • DownstreamScheme 请求的scheme(http或者https)
  • DownstreamHostAndPorts 比如你一个用户注册你部署了集群 这个时候就可以配置host和Port来访问,
  • LoadBalancer 负载均衡策略
  • UpstreamPathTemplate 上游模板
  • UpstreamHttpMethod 上游请求方式
  • Priority 优先级

优先级

{
"ReRoutes": [
    {
      "UpstreamPathTemplate": "/post/{postId}", 
      "Priority":0,//优先级
    }
    {
      "UpstreamPathTemplate": "/post/test/get", 
      "Priority":1,//优先级
    }
  ],
}

如果路由模板出现了冲突 优先级越大的会先被匹配
负载均衡策略(本次会使用consul做服务注册与发现)
DownstreamHostAndPorts存在多个hostandports 则可以分配一下匹配策略

  • LeastConnection – 将请求发往最空闲的那个服务器
  • RoundRobin – 轮流发送
  • NoLoadBalance – 总是发往第一个请求或者是服务发现

请求聚合
就是把多个响应的结果一次性返回给客户端

{
  "ReRoutes": [
   
    {
      "DownstreamPathTemplate": "/api/test/GetUserInfo",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "127.0.0.1",
          "Port": 5005
        }
      ],

      "UpstreamPathTemplate": "/GetUserInfo", //上游也就是用户输入的请求Url模板
      "UpstreamHttpMethod": [ "Get" ], //游请求http方法,可使用数
      "Key": "GetUserInfo"
    },
    {
      "DownstreamPathTemplate": "/api/test/GetUserIntegral",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "127.0.0.1",
          "Port": 5006
        }
      ],

      "UpstreamPathTemplate": "/GetUserIntegral", //上游也就是用户输入的请求Url模板
      "UpstreamHttpMethod": [ "Get" ], //游请求http方法,可使用数
      "Key": "GetUserIntegral"
    }
  ],
    "Aggregates": [
    {
      "ReRouteKeys": [
        "GetUserInfo",
        "GetUserIntegral"
      ],
      "UpstreamPathTemplate": "/GetUserInfoAndIntegral" //上游模板
    }
  ],
  "GlobalConfiguration": {}

在路由中需要用Key来标记且在Aggregates添加一个匹配模板
这边我将演示客户端获取用户信息和用户积分
单独请求获取用户信息接口和用户接口接口
在这里插入图片描述
在这里插入图片描述
使用请求集合得到的接口
在这里插入图片描述
显然 把2个接口的数据合并到一个请求返回
注意

  • 聚合返回的是json数据
  • 如果其中一个服务挂掉了 则会返回502
  • 下游只支持get请求
  • 下游的response header并会被丢弃

限流
限制某个接口访问的频率,来减少服务器的压力,或者并行带来的数据错乱

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{postId}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "127.0.0.1",
          "Port": 5005
        }

      ],
      "RateLimitOptions": {
        "ClientWhitelist": [],
        "EnableRateLimiting": true,
        "Period": "5m",
        "PeriodTimespan": 100,
        "Limit": 1
      },
      "UpstreamPathTemplate": "/post/{postId}", //上游也就是用户输入的请求Url模板
      "UpstreamHttpMethod": [ "Get" ] //游请求http方法,可使用数
    }
 
  ],
  "GlobalConfiguration": {
   
  }
}

  • ClientWihteList 白名单
  • EnableRateLimiting 是否启用限流
  • Period 时间区间:1s, 5m,1h, 1d
  • PeroidTimeSpan 多少秒之后客户端可以重试
  • Limit 在时间段内允许的最大请求数量
    熔断
    熔断的意思是停止将请求转发到下游服务。当下游服务已经出现故障了,没必要再去请求了,并且增加下游服务器和API网关的负担。这个功能是用的Pollly来实现的,我们只需要为路由做一些简单配置即可
  "QoSOptions": {
      "ExceptionsAllowedBeforeBreaking": 5,
      "DurationOfBreak": 5,
      "TimeoutValue": 5000
    }
  • ExceptionsAllowedBeforeBreaking 允许多少个异常请求
  • DurationOfBreak 熔断的时间,单位为秒
  • TimeoutValue 如果下游请求的处理时间超过多少则自如将请求设置为超时
    鉴权(结合IdentityServer4.X)

标签:网关,UpstreamPathTemplate,请求,0.1,ocelot,http,net,模板
来源: https://blog.csdn.net/qq_41990222/article/details/112565541

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有