ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Envoy Thrift Proxy 配置实践:传输与协议自动识别、路由、统计与 Header 元数据互操作

Envoy Thrift Proxy 配置实践:传输与协议自动识别、路由、统计与 Header 元数据互操作 Envoy Thrift Proxy 配置实践传输与协议自动识别、路由、统计与 Header 元数据互操作【免费下载链接】envoyCloud-native high-performance edge/middle/service proxy项目地址: https://gitcode.com/GitHub_Trending/en/envoy本篇围绕 Envoy 的 Thrift Proxy 网络过滤器envoy.filters.network.thrift_proxy展开覆盖其类型 URL 与 API 配置结构、传输Transport与协议Protocol类型、集群级上游协议选项、下游请求数限制、完整统计指标清单以及 Thrift Header 传输元数据的路由与跨连接互操作机制。读完本文你可以编写出可运行的 Thrift 代理监听器配置理解各参数在源码中的落地方式并基于统计指标完成线上问题定位。过滤器定位与类型 URLThrift Proxy 是一个 L4网络层过滤器工作在 Envoy 的 listener filter chain 中能够透明地解析、路由和转换 Thrift RPC 流量。配置该过滤器时使用以下类型 URLtype.googleapis.com/envoy.extensions.filters.network.thrift_proxy.v3.ThriftProxy其完整的 API 定义位于 thrift_proxy.proto路由消息定义位于 route.proto。从源码结构看过滤器工厂在 config.cc 中通过REGISTER_FACTORY注册为NamedNetworkFilterConfigFactory每个下游连接会实例化一个ConnectionManagerconfig.cc#L46-L70负责解码请求、执行 Thrift 过滤器链并管理上游连接。一个最小可运行的 listener 配置示例静态路由表 默认 router 过滤器如下static_resources: listeners: - name: thrift_listener address: socket_address: address: 0.0.0.0 port_value: 9090 filter_chains: - filters: - name: envoy.filters.network.thrift_proxy typed_config: type: type.googleapis.com/envoy.extensions.filters.network.thrift_proxy.v3.ThriftProxy stat_prefix: thrift_router transport: AUTO_TRANSPORT # 默认值可不写 protocol: AUTO_PROTOCOL # 默认值可不写 route_config: name: local_thrift_route validate_clusters: true routes: - match: method_name: # 空字符串匹配任意方法 route: cluster: thrift_backend clusters: - name: thrift_backend type: STRICT_DNS lb_policy: ROUND_ROBIN load_assignment: cluster_name: thrift_backend endpoints: - lb_endpoints: - endpoint: address: socket_address: address: thrift.service.local port_value: 9090ThriftProxy 配置消息全解ThriftProxy消息thrift_proxy.proto#L78-L127是过滤器的根配置字段与行为如下字段类型默认值/约束说明stat_prefixstring必填最小长度 1统计指标的人类可读前缀transportTransportType枚举AUTO_TRANSPORT下游连接使用的传输类型protocolProtocolType枚举AUTO_PROTOCOL下游连接使用的协议类型route_configRouteConfiguration与trds二选一静态路由表trdsTrds与route_config二选一通过 xDS 拉取路由配置thrift_filtersrepeated ThriftFilter为空时自动使用envoy.filters.thrift.routerThrift 过滤器链按序处理payload_passthroughboolfalse尝试跳过解码元数据之后的 payload 数据以提升性能max_requests_per_connectionUInt32Value未设置即无限制单条下游连接允许的最大请求数access_logrepeated AccessLog—Thrift 代理访问日志header_keys_preserve_caseboolfalse保留 Thrift header key 的大小写默认序列化为小写几个值得注意的实现细节默认路由过滤器若thrift_filters为空Envoy 会自动注入内置的envoy.filters.thrift.router过滤器见 config.cc#L90-L102proto 注释中列出的内置过滤器还有envoy.filters.thrift.rate_limit。TRDS 限制若同时配置route_config和trds会直接抛异常且trds使用api_config_source时仅支持AGGREGATED_GRPC/AGGREGATED_DELTA_GRPC见 config.cc#L104-L121。payload_passthrough 的适用前提仅当下游与上游协议相同、传输为 Framed 或 Header、且协议不是 Twitter 时才生效否则回退为完整解码proto 中的注释明确了这一约束。启用后对应request_passthrough/response_passthrough统计计数。access_log逐条构建访问日志处理器见 config.cc#L123-L125。传输与协议类型TransportType与ProtocolType枚举定义在 thrift_proxy.proto#L29-L64在源码内部通过 thrift.h#L109-L144 的ProtoUtils映射到实现名称传输TransportFRAMEDframed、UNFRAMEDunframed、HEADERheader即 Thrift Header 传输、AUTO_TRANSPORTauto下游连接上尝试自动识别。自动识别的实现位于 auto_transport_impl.cc。协议ProtocolBINARYbinary、LAX_BINARY非严格 binary名为binary/non-strict、COMPACTcompact、TWITTERfinagle 的 Twitter 协议已标记废弃、AUTO_PROTOCOLauto自动识别。注意 proto 注释明确非严格的 lax binary 协议不在自动识别范围内需要显式指定实现文件分别为 binary_protocol_impl.cc、compact_protocol_impl.cc、auto_protocol_impl.cc。AUTO 语义是下游自动识别、上游跟随下游这一点在 proto 注释与上游选项章节中一致。上游集群协议选项ThriftProtocolOptions对上游主机建立 Thrift 连接的行为可以通过在该 Cluster 的typed_extension_protocol_options下添加以envoy.filters.network.thrift_proxy为 key 的条目来配置消息类型为ThriftProtocolOptionsthrift_proxy.proto#L153-L172。它只有两个字段transportTransportType默认AUTO_TRANSPORT上游使用的传输protocolProtocolType默认AUTO_PROTOCOL上游使用的协议。选择 AUTO 时代理会沿用下游连接的传输/协议这在需要把流量从旧客户端协议转换为新服务端协议的网关场景非常有用。其取值逻辑在 config.cc#L36-L42TransportType ProtocolOptionsConfigImpl::transport(TransportType downstream_transport) const { return (transport_ TransportType::Auto) ? downstream_transport : transport_; }上游覆盖下游协议的配置示例clusters: - name: thrift_backend typed_extension_protocol_options: envoy.filters.network.thrift_proxy: type: type.googleapis.com/envoy.extensions.filters.network.thrift_proxy.v3.ThriftProtocolOptions transport: UNFRAMED protocol: BINARY解析上下游传输/协议不一致时的转换如 framed↔unframed 帧重组、header 帧头重序列化由 decoder.cc 与协议转换层完成这也是后文元数据互操作能力的底层基础。下游请求数限制max_requests_per_connectionThrift Proxy 可以为每条下游连接设置最大可处理请求数max_requests_per_connectionUInt32Value包装类型未设置表示不限。当某条连接处理过的请求数超过该限制时Thrift Proxy 会主动断开与 Thrift 客户端的连接从而促使客户端重建连接帮助流量在多个上游主机间再平衡。超限断开会体现在downstream_cx_max_requests计数器中可配合cx_destroy_local_with_active_rq等连接类统计观测其影响。该限制在ConfigImpl构造时读取config.cc#L87。完整统计指标清单每个已配置的 Thrift proxy 过滤器都会输出以下统计指标原文档统计表的完整继承NameTypeDescriptioncx_destroy_local_with_active_rqCounterConnections destroyed locally with an active requestcx_destroy_remote_with_active_rqCounterConnections destroyed remotely with an active requestdownstream_cx_max_requestsCounterConnections that have been closed due to reaching the max requests limitdownstream_response_drain_closeCounterConnections that have received the drain close header in a responserequestCounterTotal number of requestsrequest_callCounterTotal number of requests of type callrequest_decoding_errorCounterTotal number of requests that caused a decoding errorrequest_invalid_typeCounterTotal number of requests with an invalid typerequest_onewayCounterTotal number of requests of type onewayrequest_passthroughCounterTotal number of requests with payload passthrough enabledrequest_internal_errorCounterTotal number of requests that caused an internal errorresponseCounterTotal number of responsesresponse_decoding_errorCounterTotal number of responses with a decoding errorresponse_errorCounterTotal number of responses with an errorresponse_exceptionCounterTotal number of responses with an exceptionresponse_invalid_typeCounterTotal number of responses with an invalid typeresponse_passthroughCounterTotal number of responses with payload passthrough enabledresponse_replyCounterTotal number of responses of type replyresponse_successCounterTotal number of responses of type successrequest_activeGaugeNumber of currently active requestsrequest_time_msHistogramRequest time in milliseconds这些指标在源码中集中声明于 stats.h#L16-L37 的ALL_THRIFT_FILTER_STATS宏与文档表格一一对应。实际指标名会带上前缀从 config.cc#L82 可以看到前缀格式为thrift.stat_prefix.例如stat_prefix: thrift_router时请求计数指标全名为thrift.thrift_router.request。指标语义与 Thrift 协议消息类型直接对应MessageType枚举定义了Call / Reply / Exception / Oneway四种消息类型ReplyType区分Success / ErrorIDL 异常见 thrift.h#L150-L224。例如request_call/request_oneway统计的是MessageType而response_reply、response_success、response_error、response_exception区分的是响应侧的消息类型与应答结果。Thrift 请求元数据Header 传输HEADER传输Thrift Header transport支持以 key/value 形式携带信息性元数据。Envoy 对此提供了两项关键能力作为路由匹配条件与跨连接格式的自动转换。Header 元数据可用于路由Header 传输中携带的 key/value 对可以作为 :ref:headers匹配条件参与路由即RouteMatch.headers字段route.proto#L65-L102。RouteMatch还支持method_name精确匹配方法名空字符串匹配任意方法名常用于 catch-all 兜底路由service_name以服务名作为方法名前缀匹配仅与服务多路复用service multiplexing相关空字符串匹配任意服务invert反转method_name/service_name的匹配结果不能与通配匹配组合headers一组HeaderMatcher要求请求中所有列出的 header 均存在且值相等或仅检查存在性。注释明确指出这仅对支持 header 的传输/协议生效。配合RouteActionroute.proto#L104-L171路由可以指向单集群cluster、加权集群weighted_clusters、或从请求 header 中读取目标集群名的cluster_header找不到 header 返回 unknown method 异常引用的集群不存在则返回 internal error 异常并支持 subset 负载均衡的metadata_match、基于:method-name的rate_limits、strip_service_name去除Service:method形式的方法名前缀以及request_mirror_policies流量镜像。元数据互操作Metadata Interoperability可用于路由的请求元数据即上一节的 header 匹配值在下游与上游连接之间发生传输/协议转换时会自动在两种线上格式wire format之间转换。例如下游客户端使用 Header 传输携带x-service: foo而上游集群通过ThriftProtocolOptions被配置为 Framed/BinaryBinary 协议本身不携带 headerEnvoy 会负责在解码时提取、在转发时按目标格式重新编码保证路由判断所依据的元数据不丢失。这一转换由传输/协议实现层完成Header 传输的帧解码实现可参考 header_transport_impl.cc#L48-L180其decodeFrameStart解析帧大小、magic、flags、序列号与可变 header 区将 info blockinfo id 1中的 key/value 对写入请求/响应的 header 元数据供路由与过滤器链消费。与元数据处理相关的两个实现细节key 的大小写与非法字符默认行为是将 header key 序列化为小写NUL、CR、LF 字符按 Thrift 规范会被保留。设置header_keys_preserve_case: true后 Envoy 将保留原始大小写。从 header_transport_impl.cc#L136-L163 可以看到解码时先通过 formatter 处理 keypreserve case 时再显式剔除\0、\n、\r后构造LowerCaseString存入元数据。元数据到动态元数据的桥接从源码结构看thrift_proxy过滤器链下还提供header_to_metadata与payload_to_metadata等 Thrift 过滤器filters/header_to_metadata可将 header 或请求 payload 内容转换为动态元数据进一步支撑基于任意请求内容的路由与决策。适用前提与限制小结本文所有配置均基于 v3 APIudpa.annotations.file_status ACTIVE类型 URL 以envoy.extensions.filters.network.thrift_proxy.v3为准TWITTER协议已在 proto 中标记deprecated3.0 起新配置不建议使用LAX_BINARY无法被自动协议识别覆盖必须显式声明payload_passthrough仅在上下游协议一致、传输为 Framed/Header 且非 Twitter 协议时生效否则自动回退为完整解码可通过request_passthrough/response_passthrough计数器确认其是否真正生效max_requests_per_connection达到上限时会主动断开下游连接客户端需具备重连能力建议配合downstream_cx_max_requests统计评估阈值合理性指标前缀为thrift.stat_prefix.做监控告警与仪表盘命名时需注意这一约定。更多源码入口过滤器工厂与路由提供器在 config.cc协议抽象接口Protocol、DirectResponse、协议工厂注册在 protocol.h连接状态机说明见 thrift_state_machine.md。【免费下载链接】envoyCloud-native high-performance edge/middle/service proxy项目地址: https://gitcode.com/GitHub_Trending/en/envoy创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表