Spring Cloud Alibaba Traffic Governance Scheme Design | Spring Cloud Alibaba流量治理方案设计

Author: sollhuiCreated Aug 8, 2022Updated Jul 28, 2026
Labelskind/discussion

Background

Spring Cloud Alibaba microservice governance capabilities are relatively weak, we currently intends to dock Istio to complete the traffic governance function, and need to abstract Spring Cloud Alibaba's own governance data rules to facilitate docking.

Scheme Design

Desgin Idea

The initially intended module is called spring-cloud-starter-alibaba-governance, which contains two main modules: resource-transform and gainance-core:

resource-transform
 -istio-resource-transform
  opensergo-resource-transform
  
governance-core
 -label-routing
  service-authentication

CRD Design

java
hosts:
  user-Service:
    name:java-user-service #The caller service name
    version:v2
  target-service:
    name:java-target-service #The service name that is invoked matches the following routing rule
-match:
  rule:
    -type: header 
     condition: exact 
     key: 'region'   
     value: beijing  
   - type: url-path 
     condition: prefix 
     value: /test  
   - type: url-parameter 
     condition: eaxct
     key: uid
     value: 1
  route:
     version: v2
     weight: 10
-match:  
  rule:
   - type: header 
     condition: eaxct
     key: 'cookie'  
     value: shanghai  
   - type: header 
     condition: regex
     key: 'uid'   
     value: /^\+?[1-9][0-9]*$/  
  route:
     version: v2
     weight: 10
default-route:
    version: v1

Design Describe

Each service in the microservice system has its own traffic rules, and considering that each service only needs to save its own service rules, a user-sevice is provided to identify the service corresponding to the service governance rules, and taget-service represents the service name of the call. For traffic matching, the match rule is also an array that allows multiple routes to exist. Each route provides a rule array to match, and a rule array may contain multiple tpye, each with different rules. Finally, if none of the above rules match, it matches to the default path. All the supported types are shown in the following table (more types will be supported later):

Type condition Example
header exact regex -type: header condition: exact key: 'region' value: beijing - type: header condition: regex key: 'uid' value: /^+?[1-9][0-9]*$/
url-path exact prefix regex - type: url-path condition: exact value: /test - type: url-path condition: prefix value: /test - type: url-path condition: regex value: ^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=_|!:,.;]*[-a-zA-Z0-9+&@#/%=_|]
url-parameter exact regex -type: url-parameter condition: exact key: 'region' value: beijing - type: header condition: regex key: 'uid' value: /^+?[1-9][0-9]*$/

背景

Spring Cloud Alibaba微服务治理能力比较弱,目前打算对接istio完成流量治理功能,需要抽象出Spring Cloud Alibaba自己的治理数据规则方便对接。

方案设计

设计思路

初步打算模块名称为spring-cloud-starter-alibaba-governance,其中包含两个主要模块:resource-transform和governance-core:

resource-transform
 -istio-resource-transform
  opensergo-resource-transform
  
governance-core
 -label-routing
  service-authentication

目前打算对接Istio,后续SCA可能还会对接其他控制面进行流量治理,考虑到可拓展性,将数据层抽象出来放在label-routing中,后续对接openSergo等控制面时直接将opensergo数据结构转化成该数据结构即可。无需进行其他修改。

设计方案

java
hosts:
  user-Service:
    name:java-user-service #调用方服务名
    version:v2
  target-service:
    name:java-target-service #被调用的服务名,匹配下面的路由规则
-match:
  rule:
    -type: header # 请求头匹配
     condition: exact # 精确匹配
     key: 'region'   # 参数名
     value: beijing  # 参数值
   - type: url-path # url路径匹配
     condition: prefix #前缀匹配
     value: /test  
   - type: url-parameter # url参数匹配
     condition: eaxct
     key: uid
     value: 1
  route:
     version: v2
     weight: 10
-match:  
  rule:
   - type: header 
     condition: eaxct
     key: 'cookie'  
     value: shanghai  
   - type: header 
     condition: regex
     key: 'uid'   
     value: /^\+?[1-9][0-9]*$/  
  route:
     version: v2
     weight: 10
default-route:
    version: v1

方案说明

微服务体系中的每一个服务都有自己的流量规则,同时,考虑到每一个服务只需保存自己的服务规则,提供了user-sevice来对服务治理规则对应的服务进行标识,taget-service表示调用的服务名称。对于流量匹配,匹配规则(match)也是一个数组,允许多个路径(route)存在。每一个路径(route)都提供了一个rule数组来匹配,一个rule数组中可能包含多个tpye,每一个type的规则都不同。最后,如果上述规则都无法匹配,则匹配到默认的路径中。 支持的所有类型如下表所示(后续会支持更多的的类型):

类型 条件 示例
header exact 精确匹配regex 正则表达式 -type: header condition: exact key: 'region' value: beijing - type: header condition: regex key: 'uid' value: /^+?[1-9][0-9]*$/
url-path exact 精确匹配prefix 前缀regex 正则表达式 - type: url-path condition: exact value: /test - type: url-path condition: prefix value: /test - type: url-path condition: regex value: ^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=_|!:,.;]*[-a-zA-Z0-9+&@#/%=_|]
url-parameter exact 精确匹配regex 正则表达式 -type: header condition: exact key: 'region' value: beijing - type: header condition: regex key: 'uid' value: /^+?[1-9][0-9]*$/

Source: alibaba/spring-cloud-alibaba