- tRPC-Gateway Routing Module
- Routing Configuration Methods
- Routing Configuration Details
- Router Configuration
- method
- is_regexp
- id
- rewrite
- strip_path
- report_method
- target_service
- target_service.service
- target_service.weight
- target_service.rewrite
- target_service.strip_path
- hash_key
- host
- Route Plugins
- plugins[0].name
- plugins[0].type
- plugins[0].props
- rule
- rule.conditions
- rule.conditions[0].key
- rule.conditions[0].val
- rule.conditions[0].oper
- rule.conditions[0].expression
- client
- Global Plugins
- Router Configuration
- Execution of Gateway Plugins
Exact match -> Prefix match -> Regex match -> Fine-grained match
For multiple routes with the same path, they are matched in the order of configuration.
【Attention】Note the difference in matching rules compared to nginx:
- Nginx first matches the host and then matches the path under the host.
- tRPC-Gateway first matches the path and then matches the host.
【Attention】After all fine-grained matches fail, no further matching will be performed!
Example scenario::
Request: http://r.inews.qq.com/user/info
There are the following route items:
- method: /user/info host: f.inews.qq.com
- method: /user/info host: w.inews.qq.com
- method: /user/ host:
First, two exact matches are made based on the URI:
- method: /user/info host: f.inews.qq.com
- method: /user/info host: w.inews.qq.com
Then, a precise match is made based on the host, but none of them match, so a 404 response is returned.
Prefix matching to /user/ will not occur.
- method: /user/ host:
Routing configuration supports:
- Local file
- Set global.conf_provider=file in the trpc.yaml file, refer to trpc.yaml
- Specify the configuration file through the startup parameter --router={your_router_conf}
- Configuration center, currently supports Etcd
- Etcd
- refer to etcd loader
- Etcd
The configuration is divided into three parts: router for forwarding rule configuration, client for backend service configuration, and plugins for global plugin configuration.
The gateway routing configuration is done using the YAML format. For a complete configuration example, refer to router.yaml
The router configuration is an array, where each element represents a routing forwarding rule item. Here is an example configuration:
router: # Router configuration
- method: ^/v1/user/ # Regex route
is_regexp: true # Whether it is a regex route, set to true for regex matching
id: "path:^/v1/user/" # Route ID, used to identify a route for debugging (method can be duplicated)
rewrite: /v1/user/info # Rewrite path
strip_path: false
target_service: # Upstream service
- service: trpc.user.service # Service name, corresponding to the name in the client configuration
weight: 10 # Service weight, the sum of weights cannot be 0
rewrite: /search # Service-level rewrite path, higher priority than router-level; higher priority than strip_path configuration
strip_path: false # Whether to strip the prefix
hash_key: ""
host:
- test.shizi.qq.com # Host, only matches the current route if matched. If empty, matches all request hosts
plugins: # Route-level plugins
- name: demo # Plugin name, required
type: gateway
props: # Plugin properties
suid_name: suidxxx
rule:
conditions:
- key: devid
val: xxxx,yyyyy
oper: in
expression: "0"
Each field is explained as follows:
Path matching rule, type string, with the following three matching rules:
- Exact match: The path and method are exactly the same. For example, if the request path is /user/info, it will match the route item with method = /user/info.
- Prefix match: The path contains the prefix of the method. For example, if the request path is /user/info, it will match the route item with method = /user/.
- Regex match: The path matches the regex rule of the method. For example, if the request path is /user/info, it will match the route item with method = (/user/info|/user/add) and is_regexp = true.
The priority of the three matching logics is: Exact match > Prefix match > Regex match.
When there are duplicate methods, meaning multiple route items match, it will try to perform fine-grained matching using the rule (see rule), and return the first matched route item. If the rule does not match either, it will return the first route item that does not have a rule configured.
Whether it is a regex match, type bool, default is false. If true, it indicates that it is a regex route. Since regex routes are matched through iteration, explicitly marking it as a regex route can improve matching efficiency.
The unique identifier of the route, used to identify a route item for development and debugging.
The rewritten path, divided into exact path and prefix path.
- Exact path: It does not end with /. It has the highest priority.
- For example, if the client request is /user/info and it needs to be forwarded to /v1/user/info, then configure rewrite=/v1/user/info. If not configured, it will still be forwarded to /user/info.
- Prefix path: It ends with /.
- For example, if the client request is /user/info and it needs to be forwarded to /v1/user/info, then configure rewrite=/v1/. If not configured, it will still be forwarded to /user/info.
Whether to remove the prefix when forwarding.
For example, if the request path is /v1/user/info and it is expected to be forwarded to /user/info, you can configure method = /v1/ and strip_path = true. The priority is lower than the exact path configuration of rewrite.
It can be combined with the prefix path configuration of rewrite to achieve prefix rewriting of interfaces.
For example, if the request path is /v1/user/info and it is expected to be forwarded to /v2/user/info, you can configure method = /v1/, strip_path = true, and rewrite = /v2/.
Only report the method of router, not the request path, to prevent explosion of monitoring dimensions for path like /a/{article_id}.
Target service configuration, which is an array. The weight field of each element can be used to configure traffic weight.
Service name, corresponding to the name field in the client configuration.
Traffic weight, the sum of weights of multiple services needs to be > 0. Only one service can be configured without weight.
Service-level rewrite, the logic is the same as the rewrite at the router level.
Service-level strip_path, the logic is the same as the strip_path at the router level.
Used in conjunction with multiple target_service configurations. For example, if devid is configured, requests that contain the same devid in the request parameters (query parameters, headers, cookies) will be routed to the same target_service.
The list of target request hosts. The current route item will only match if the host is in this list. If empty, it matches all hosts.
Route-level plugin configuration, which is an array and can have multiple plugins. Only applicable to the current route item.
Plugin name, required. It needs to be the same as the name defined in the plugin definition.
Plugin type, optional. Default is gateway. It needs to be the same as the type defined in the plugin definition.
Plugin properties, optional. Each plugin can have its own configuration fields.
Fine-grained matching rules for routing based on request parameters.
List of rule conditions.
Request parameter name. For example, if devid is configured, it will search for the parameter named devid in the query parameters, headers, and cookies.
Service providers can customize their own parameter retrieval logic by overriding the core/router.DefaultGetString method, such as retrieving parameters from the JSON request body.
Value of the request parameter. For example, matching requests with devid equal to xxx.
逻辑运算符,支持以下操作
Operator | Description | Note |
---|---|---|
== | Equal | |
!= | Not equal | |
> | Greater | |
>= | Greater or equal | |
< | Less | |
<= | Less or equal | |
in | In set | val values are separated by ',' |
!in | Not in set | val values are separated by ',' |
regexp | Regex match |
Logical operators for conditions, supporting || (OR) and && (AND) operations. For example, 0&&1||2 means that the current route item will be matched when conditions[0] AND conditions[1] OR conditions[2] are satisfied.
The client configuration is based on the trpc client logic, and the configuration fields and logic are mostly consistent with the trpc client.
Configuration example:
client:
- name: trpc.inews.user.User
disable_servicerouter: false
namespace: Production
target: polaris://trpc.inews.user.User
network: tcp
timeout: 500
protocol: fasthttp # Currently, the supported protocols are fasthttp, trpc, and grpc. Among them, fasthttp represents the HTTP protocol.
serialization: null
plugins: # Service-level plugins
- name: auth
type: gateway
props:
There are the following differences with tRPC client:
- The protocol field currently supports fasthttp, where fasthttp represents the HTTP protocol.
- The plugins field has been added, which allows configuring gateway plugins at the service level. The configuration is the same as the global plugin configuration.
- The disable_filter configuration has been removed.
Global plugin configuration that applies to all requests.
Configuration example:
plugins:
- name: demo # Plugin name, required
type: gateway # Plugin type, optional. Default is gateway, should match the type defined in the plugin definition
props: # Plugin properties
suid_name: suidxxx
Plugin name, required. It should be the same as the name defined in the plugin definition.
Plugin type, optional. Default is gateway. It should match the type defined in the plugin definition.
Plugin properties, optional. Each plugin can have its own configuration fields.
Gateway plugins can be configured at three levels: global plugins (plugins), service plugins (client[0].plugins), and route plugins (router[0].plugins).
The execution order is: global plugins > service plugins > route plugins.
When plugins are duplicated, the priority is determined by proximity: route plugins > service plugins > global plugins.
For information on developing and registering gateway plugins, please refer to Gateway plugin development