Script Routing Rules

Script routing provides maximum flexibility for traffic management. All traffic will dynamically execute a rule script before performing load balancing selection, and the available address subset is determined based on the script execution results.

Script routing only affects consumers and only supports application-level management. Therefore, the key must be set to the consumer application name. The script syntax supports multiple languages. Taking the Dubbo Java SDK as an example, the script syntax supports Javascript, Groovy, Kotlin, etc. For specific limitations, refer to the implementation of each language.

Since script routing can dynamically load and execute remote code, there are potential security risks. Ensure that the script rules run in a secure sandbox before enabling script routing.

  1. configVersion: v3.0
  2. key: demo-provider
  3. type: javascript
  4. enabled: true
  5. script: |
  6. (function route(invokers,invocation,context) {
  7. var result = new java.util.ArrayList(invokers.size());
  8. for (i = 0; i < invokers.size(); i ++) {
  9. if ("10.20.3.3".equals(invokers.get(i).getUrl().getHost())) {
  10. result.add(invokers.get(i));
  11. }
  12. }
  13. return result;
  14. } (invokers, invocation, context));

ScriptRule

The main body of the script routing rule. It defines the target consumer application for which the script rule is effective, the traffic filtering script, and behaviors in specific scenarios.

FieldTypeDescriptionRequired
configVersionstringThe version of the script rule definition, currently available version is v3.0Yes
keystringThe identifier of the target application that this rule is about to apply to.Yes
typestringThe script language used to define script.Yes
enabledboolWhether to enable this rule or not, set enabled:false to disable this rule.Yes
scriptstringThe script definition used to filter dubbo provider instances.Yes
forceboolThe behavior when the instance subset is empty after routing. true means return no provider exception while false means ignore this rule.No

Script

script is the main body of the script routing rule, and its type is a string with a specific structure, depending on the script language specified by type.

The following is an example of a script rule with type: javascript:

  1. (function route(invokers,invocation,context) {
  2. var result = new java.util.ArrayList(invokers.size());
  3. for (i = 0; i < invokers.size(); i ++) {
  4. if ("10.20.3.3".equals(invokers.get(i).getUrl().getHost())) {
  5. result.add(invokers.get(i));
  6. }
  7. }
  8. return result;
  9. } (invokers, invocation, context));

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)