概述目标:通过Envoy与外部限流服务实现跨集群一致的限流,支持按用户/路径/方法的精细策略,减少滥用与保护后端。适用:API网关、微服务入口、跨区域统一限流。核心与实战HTTP过滤器配置:http_filters: - name: envoy.filters.http.ratelimit typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit domain: api rate_limit_service: grpc_service: envoy_grpc: cluster_name: ratelimit transport_api_version: V3 failure_mode_deny: false Cluster到限流服务:clusters: - name: ratelimit type: STRICT_DNS http2_protocol_options: {} load_assignment: { ... } 描述符生成(HTTP路由):routes: - match: { prefix: "/v1" } route: { cluster: api } typed_per_filter_config: envoy.filters.http.ratelimit: "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimitPerRoute rate_limit: actions: - request_headers: { header_name: "x-api-key", descriptor_key: "api_key" } - header_value_match: { descriptor_value: "v1", headers: [{ name: ":path", exact_match: "/v1" }] } 限流服务规则示例(envoyproxy/ratelimit):domain: api descriptors: - key: api_key descriptors: - value: "key-123" rate_limit: { unit: second, requests_per_unit: 100 } 示例启动与测试:envoy -c envoy.yaml --drain-time-s 2 curl -H 'x-api-key: key-123' http://gateway/v1/resource 观察限流命中:curl -s http://ratelimit:8080/metrics | grep rate_limit 验证与监控指标:Envoy的`ratelimit.*`度量与外部服务的命中/拒绝;记录拒绝原因。规则治理:使用GitOps管理描述符与规则;区分按用户/路径/方法的组合键。失败模式:设置`failure_mode_deny`为false以在限流服务不可用时放行(根据风险评估)。常见误区描述符键设计不合理导致不公平;需选择能代表用户与资源的维度。限流服务不可用导致全面拒绝;需设置故障模式与重试策略。未监控限流指标;无法评估策略效果与误伤。结语Envoy与外部限流服务结合可实现灵活与统一的限流治理,通过描述符策略与监控确保公平与稳定。

发表评论 取消回复