路由规则
路由规则介绍
介绍
路由规则,简单来说,就是根据**特定条件**将**特定请求**流量发送到**特定服务提供者**。从而实现流量的分配。
在 Dubbo3 统一路由规则的定义中,需要提供两个 yaml 格式的资源:虚拟服务和目标规则。其格式与服务网格定义的路由规则非常相似。
- 虚拟服务
定义主机,用于与目标规则建立关系。
定义服务匹配规则
定义匹配匹配规则
匹配到特定请求后,搜索并验证目标集群,对于空情况使用回退机制。
-目标规则
定义特定的集群子集以及子集适应的标签。标签从提供者端暴露的 url 中获取并尝试匹配。
提供的能力
基于配置中心的路由配置
示例示例,请参见 网格路由器
1. 路由规则文件注释
路由规则仅适用于客户端。对于服务器,您只需要在提供服务时标记特定的参数。
1.1 虚拟服务
apiVersion: service.dubbo.apache.org/v1alpha1
kind: VirtualService
metadata: {name: demo-route}
spec:
dubbo:
# Use a regular expression to match the service name, only a request that satisfies the service name can be routed.
# For this example, if the request does not satisfy the service name, the provider will not be found directly
# - services:
# - { regex: org.apache.dubbo.UserProvider* }
- route detail:
- match:
# Matching rules, if (sourceLabel) client url satisfies the parameter `trafficLabel: xxx`, the match can be successful
- sourceLabels: {trafficLabel: xxx}
name: xxx-project
route: # Once the above match rule is matched, the subset named isolation defined in dest_rule will be selected
- destination: {host: demo, subset: isolation}
- match:
- sourceLabels: {trafficLabel: testing-trunk}
name: testing-trunk
route: # Once the above match rule is matched, the subset named testing-trunk defined in dest_rule will be selected
- destination: {host: demo, subset: testing-trunk}
- name: testing # There is no match, the bottom-up logic, if the above-mentioned dissatisfaction is met, it will be matched.
route:
- destination: {host: demo, subset: testing}
services:
- {exact: com.apache.dubbo.sample.basic.IGreeter}
hosts: [demo] # Match the host in dest_rule.yml as demo
1.2 目标规则
apiVersion: service.dubbo.apache.org/v1alpha1
kind: DestinationRule
metadata: { name: demo-route }
spec:
host: demo
subsets:
- labels: { env-sign: xxx, tag1: hello }
name: isolation
- labels: { env-sign: yyy }
name: testing-trunk
- labels: { env-sign: zzz }
name: testing
trafficPolicy:
loadBalancer: { simple: ROUND_ROBIN }
2. 客户端和服务器路由参数设置
客户端
dubbogo.yml
定义配置中心
config-center:
protocol: zookeeper
address: 127.0.0.1:2181
data-id: "dubbo-go-samples-configcenter-zookeeper-client"
通过代码中的 API 将配置发布到配置中心,或者提前手动配置。
dynamicConfiguration, err := config.GetRootConfig().ConfigCenter.GetDynamicConfiguration()
if err != nil {
panic(err)
}
// publish mesh route config
err = dynamicConfiguration. PublishConfig("dubbo.io. MESHAPPRULE", "dubbo", MeshRouteConf)
if err != nil {
return
}
服务器端
dubbo:
registries:
demoZK:
protocol: zookeeper
timeout: 3s
address: 127.0.0.1:2181
protocols:
triple:
name: tri
port: 20000
provider:
services:
GreeterProvider:
interface: com.apache.dubbo.sample.basic.IGreeter # must be compatible with grpc or dubbo-java
params:
env-sign: zzz # server label, corresponding to the testing in the destination Rule, that is, the bottom-up logic
3. 运行方法
使用 goland 直接运行此示例
运行后,可以观察到所有客户端流量都路由到服务器。根据源标签,没有命中 virtualService,因此路由到底层的测试。
上次修改时间:2024 年 1 月 17 日:修复损坏的链接 (6651e217e73)