前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Envoy实现.NET架构的网关(五)集成Redis实现限流

Envoy实现.NET架构的网关(五)集成Redis实现限流

作者头像
Chester Chen
修改2022-08-18 15:17:46
6390
修改2022-08-18 15:17:46
举报
文章被收录于专栏:chester技术分享chester技术分享

什么是限流

限流即限制并发量,限制某一段时间只有指定数量的请求进入后台服务器,遇到流量高峰期或者流量突增时,把流量速率限制在系统所能接受的合理范围之内,不至于让系统被高流量击垮。而Envoy可以通过envoy.filters.http.ratelimit插件实现限流。

限流服务

Envoy实现限流需要依赖限速服务,Envoy官方为我们提供了基于Redis和Memcached的限速服务 https://github.com/envoyproxy/ratelimit

我们将其从github下载到本地,来看看其中的docker-compose.yaml的工作模式,我们注意到其中的example文件挂载目录

并且其中还制定了配置目录是example/ratelimit

我们来看看config.yaml和example.yaml的内容,发现其中定义了domain和描述符

所以根据文档的提示,我们在envoy配置中应该命中这些描述符才有效,下面我们通过docker-compose up启动ratelimit服务,启动之前我们需要调整docker-compose.yaml,需要将go mod代理指向国内的代理

启动ratelimit服务

配置Envoy

配置envoy之前我们需要注意一下几点

  • 需要用到envoy.filters.http.ratelimit过滤器
  • 指定其domain为example.yaml中对应的rl
  • 需要为其指定ratelimit服务的cluste
  • 需要为route配置限速规则

具体配置如下

代码语言:javascript
复制
admin:
  address:
    socket_address:
      protocol: TCP
      address: 0.0.0.0
      port_value: 9902
static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address:
        protocol: TCP
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          codec_type: AUTO
          stat_prefix: ingress
          http_filters:
          - name: envoy.filters.http.ratelimit
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit
              domain: rl
              request_type: both
              stage: 0
              rate_limited_as_resource_exhausted: true
              failure_mode_deny: false
              enable_x_ratelimit_headers: DRAFT_VERSION_03
              rate_limit_service:
                grpc_service:
                  envoy_grpc:
                    cluster_name: ratelimit
                transport_api_version: V3
          - name: envoy.filters.http.router
            typed_config: {}
          route_config:
            name: route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: service_envoyproxy_io
                  rate_limits:
                  - actions:
                      - request_headers:
                          header_name: "foo"
                          descriptor_key: "foo"
  clusters:
  - name: ratelimit
    type: STRICT_DNS
    connect_timeout: 1s
    lb_policy: ROUND_ROBIN
    protocol_selection: USE_CONFIGURED_PROTOCOL
    http2_protocol_options: {}
    load_assignment:
      cluster_name: ratelimit
      endpoints:
        - lb_endpoints:
            - endpoint:
                address:
                  socket_address:
                    address: 192.168.43.94
                    port_value: 8081
  - name: service_envoyproxy_io
    connect_timeout: 30s
    type: strict_dns
    # Comment out the following line to test on v6 networks
    # dns_lookup_family: V4_ONLY
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: service_envoyproxy_io
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 192.168.43.94
                port_value: 5000
代码语言:javascript
复制

启动Envoy

代码语言:javascript
复制
docker run --rm -it -p 9902:9902 -p 10000:10000 -v D:/gateway/envoy/config/static/:/etc/envoy/ -v D:/gateway/envoy/logs:/logs envoyproxy/envoy-dev  -c /etc/envoy/envoy-ratelimit.yaml

测试限速

调用接口http://192.168.43.94:10000/Name,第三次的时候就会触发429超限请求

我们看看example/ratelimit/config/example.yaml,可以看到每分钟只允许2次请求,至此限速验证完成!!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-11-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 chester技术分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 什么是限流
  • 限流服务
  • 配置Envoy
  • 启动Envoy
  • 测试限速
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档