路由規則
路由規則簡介
簡介
路由規則,簡而言之,就是根據特定條件,將特定請求流量發送到特定服務提供者。從而實現流量的分發。
在 Dubbo3 統一路由規則的定義中,需要提供兩個 yaml 格式的資源:虛擬服務和目標規則。其格式與服務網格定義的路由規則非常相似。
- 虛擬服務 (virtual service)
定義主機,用於與目標規則建立關係。
定義服務匹配規則
定義匹配規則
匹配特定請求後,搜尋並驗證目標叢集,並針對空的情況使用後備機制。
目標規則 (destination rule)
定義特定的叢集子集以及子集適配的標籤。標籤從提供者端暴露的 url 中獲取並嘗試匹配。
提供能力
基於配置中心的路由配置
範例程式碼,請參考 Mesh Router
1. 路由規則檔案註釋
路由規則僅適用於用戶端。對於伺服器端,只需在提供服務時標記特定參數即可。
1.1 虛擬服務 (virtual-service)
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 目標規則 (destination-rule)
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 直接執行此範例
執行後,可以觀察到所有客戶端流量都路由到伺服器。根據來源標籤,沒有虛擬服務命中,因此路由到自下而上的測試。
最後修改日期:2024 年 1 月 17 日:修復失效連結 (6651e217e73)