Service routing

Updated at:

Routing rules determine which service provider a service consumer uses when multiple options are available. The service routing feature lets you define flexible rules for a wide range of scenarios.

Overview

Service routing is commonly used in scenarios such as offline integration testing, blue-green deployment, and canary release to minimize the impact of issues caused by version upgrades.

  • Offline integration testing

    During offline testing, you might not have the required environment. You can register your test application online, enable a routing rule, and then run tests locally.

  • Blue-green deployment

    For a blue-green deployment, you can use a routing rule to switch traffic to the green group to verify that the new application version works as expected.

  • Canary release

    For a canary release, you can use a routing rule to direct traffic to the canary version of the application and verify that it works as expected.

Note

Currently, service routing supports SOFA and Dubbo services but not Spring Cloud services.

Routing rules run on the service consumer (client) to filter service providers. The rules are executed by either RPC-Client (for classic microservices) or MOSN (for service mesh).

When deploying a service provider, you can add a label in a fixed format to the machine, such as LABEL=A.mv The following diagram illustrates how a routing rule takes effect:版本号

Add a routing rule

  1. Log on to the SOFAStack console.

  2. In the left navigation pane, choose middleware > Microservice Platform > Microservices > Service governance, and then click the Service routing tab.

  3. Click Add routing group rule and configure the following parameters:

    Section

    Parameter

    Description

    Basic information

    Rule group name

    The name of the routing rule group.

    Provider application name

    The name of the provider application.

    Service

    The services included in the application.

    Click Switch input mode to toggle between manual entry and selection from a drop-down list.

    Rule properties

    Rule name

    The name of the routing rule.

    Priority

    The priority of the routing rule. A higher number indicates a higher priority.

    If multiple routing rules exist, they are matched in order of priority.

    Rule switch

    Specifies whether to enable this routing rule. This rule is enabled by default.

    Group fallback switch

    Specifies whether to forward traffic to other groups when no service instance is found in the traffic destination group.

    • If enabled, traffic that does not match a service instance in a high-weight group is then matched against other groups.

    • If disabled, traffic is not matched against other groups.

    Failover switch

    Specifies whether to try lower-priority rules when the current rule does not match any service instance.

    • If enabled, traffic is matched against lower-priority rules if the current rule finds no matching service instance.

    • If disabled, traffic is not matched against other routing rules.

    Traffic matching (Optional)

    Defines conditions to filter requests. This rule applies only to traffic that meets these conditions. Unmatched traffic is handled according to the failover switch setting. If no conditions are specified, the rule matches all traffic.

    A condition consists of the following parts:

    • Field: Includes system fields, request headers, and trace.

    • Field name: Varies depending on the field type.

      • System field: Includes Traffic Type, Caller Application Name, Caller IP, and Provider Application Name.

      • Request header: Refers to the header of the protocol. For example, for the Dubbo protocol, it refers to the attachment, and for the HTTP protocol, it is the request header. You can customize the request header parameters and values in your application.

      • Trace: Refers to trace information, which is obtained from the configuration file in your code.

    • Selection logic: Includes Equals, Not Equals, In, Not In, and Regular Expression.

    • Field value: The value corresponding to the field name.

    Traffic destination

    Weight

    The weight of the current traffic destination group. The sum of weights for all traffic destinations must be 100%.

    You can click the Add button at the bottom to configure multiple traffic destination groups.

    Service instance grouping conditions

    The conditions for grouping service instances. Multiple conditions are combined with AND logic.

    • Field name: You can select a value from the drop-down list or enter a custom value.

      Note

      Routing rules can match any field (key-value pair) in the service metadata. In addition to standard labels, use "Custom Input" to match system metadata fields such as startTime and ip. To find available keys, check for the rpcVer information in the server-side log/registry/registry-client.log file.

    • Logic: Equals.

    • Field value: The value corresponding to the field name.

    You can click the Add button below to configure multiple grouping conditions.

  4. Click Submit.

  5. In the service routing list, set the status of the new rule to Enabled.

Important

You can add multiple routing rules. Rules for the same application name are merged.

Edit a routing rule

You can edit an existing routing rule at any time. Changes take effect immediately upon submission.

  1. On the Service routing tab, click the plus sign (+) to the left of the target application.

  2. Click Edit to the right of the target routing rule.

  3. Edit the routing rule as needed, and then click Submit.

Delete a routing rule

You can delete an existing routing rule. Deletions take effect immediately. Proceed with caution.

  1. On the Service routing tab, click the plus sign (+) to the left of the target application.

  2. Click Delete to the right of the target routing rule.

  3. Click OK.