动态配置规则

动态配置规则 (ConfigurationRule) 是 Dubbo 设计的在无需重启应用的情况下,动态调整 RPC 调用行为的一种能力,也称为动态覆盖规则,因为它是通过在运行态覆盖 Dubbo 实例或者 Dubbo 实例中 URL 地址的各种参数值,实现改变 RPC 调用行为的能力。

使用动态配置规则,有以下几条关键信息值得注意:

  • 设置规则生效过滤条件。 配置规则支持一系列的过滤条件,用来限定规则只对符合特定条件的服务、应用或实例才生效。
  • 设置规则生效范围。 一个 rpc 服务有服务发起方(消费者)和服务处理方(提供者)两个角色,对某一个服务定义的规则,可以具体到限制是对消费者还是提供者生效。
  • 选择规则管理粒度。 Dubbo 支持从服务和应用两个粒度来管理和下发规则。

以下一个应用级别的配置示例,配置生效后,shop-detail 应用下提供的所有服务都将启用 accesslog,对 shop-detail 部署的所有实例生效。

  1. configVersion: v3.0
  2. scope: application
  3. key: shop-detail
  4. configs:
  5. - side: provider
  6. parameters:
  7. accesslog: 'true'

以下是一个服务级别的配置示例,key: org.apache.dubbo.samples.UserServiceside: consumer 说明这条配置对所有正在消费 UserService 的 Dubbo 实例生效,在调用失败后都执行 4 次重试。match 条件进一步限制了消费端的范围,限定为只对应用名为 shop-frontend 的这个消费端应用生效。

  1. configVersion: v3.0
  2. scope: service
  3. key: org.apache.dubbo.samples.UserService
  4. configs:
  5. - match:
  6. application:
  7. oneof:
  8. - exact: shop-frontend
  9. side: consumer
  10. parameters:
  11. retries: '4'

ConfigurationRule

配置规则主体,定义要设置的目标服务或应用、具体的规则配置。具体配置规则 (configs) 可以设置多条。

FieldTypeDescriptionRequired
configVersionstringThe version of the configuration rule definition, currently available version is v3.0Yes
scopestringSupports service and application scope configurations.Yes
keystringThe identifier of the target service or application that this rule is about to apply to.

- If scope:serviceis set, then keyshould be specified as the Dubbo service key that this rule targets to control.
- If scope:application is set, then keyshould be specified as the name of the application that this rule targets to control.
Yes
enabledboolWhether enable this rule or not, set enabled:false to disable this rule.Yes
configsConfig[]The match condition and configuration of this rule.Yes

Config

具体的规则配置定义,包含生效端 (consumer 或 provider) 和过滤条件。

FieldTypeDescriptionRequired
sidestringEspecially useful when scope:serviceis set.

- side: providermeans this Config will only take effect on the provider instances of the service key.
- side:consumermeans this Config will only take effect on the consumer instances of the service key
Yes
parametersmap<string, string>The keys and values this rule aims to change.Yes
matchMatchConditionA set of criterion to be met in order for the rule/config to be applied to the Dubbo instance.No
enabledboolWhether enable this Config or not, will use the value in ConfigurationRule if not setNo
addressesstring[]replaced with address in MatchConditionNo
providerAddressesstring[]not supported anymoreNo
servicesstring[]replaced with service in MatchConditionNo
applicationsstring[]replaced with application in MatchConditionNo

MatchCondition

过滤条件,用来设置规则对哪个服务 (service)、应用 (application)、实例 (address),或者包含哪些参数 (param) 的实例生效。

FieldTypeDescriptionRequired
addressStringMatchThe instance address matching condition for this config rule to take effect.

- xact: “value” for exact string match
- prefix: “value” for prefix-based match
- regex: “value” for RE2 style regex-based match (https://github.com/google/re2/wiki/Syntax)).
No
serviceStringMatch (oneof)The service matching condition for this config rule to take effect. Effective when scope: application is set.

- exact: “value” for exact string match
- prefix: “value” for prefix-based match
- regex: “value” for RE2 style regex-based match (https://github.com/google/re2/wiki/Syntax)).
No
applicationStringMatch (oneof)The application matching condition for this config rule to take effect. Effective when scope: service is set.

- exact: “value” for exact string match
- prefix: “value” for prefix-based match
- regex: “value” for RE2 style regex-based match (https://github.com/google/re2/wiki/Syntax)).
No
paramParamCondition[]The Dubbo url keys and values matching condition for this config rule to take effect.No

ParamCondition

定义实例参数 (param) 过滤条件,对应到 Dubbo URL 地址参数。

FieldTypeDescriptionRequired
keystringThe name of the key in the Dubbo url address.Yes
valueStringMatch (oneof)The matching condition for the value in the Dubbo url address.Yes

StringMatch

FieldTypeDescriptionRequired
exactstring (oneof)exact string matchNo
prefixstring (oneof)prefix-based matchNo
regexstring (oneof)RE2 style regex-based match (https://github.com/google/re2/wiki/Syntax)).No

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)