Envoy实现.NET架构的网关(五)集成Redis实现限流
2021/11/2 19:13:20
本文主要是介绍Envoy实现.NET架构的网关(五)集成Redis实现限流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
什么是限流
限流即限制并发量,限制某一段时间只有指定数量的请求进入后台服务器,遇到流量高峰期或者流量突增时,把流量速率限制在系统所能接受的合理范围之内,不至于让系统被高流量击垮。而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配置限速规则
具体配置如下
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
启动Envoy
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次请求,至此限速验证完成!!
这篇关于Envoy实现.NET架构的网关(五)集成Redis实现限流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用
- 2024-01-23.NET开源的简单、快速、强大的前后端分离后台权限管理系统