Dynamic routing

Updated at:

The QuickStart document demonstrates the dynamic routing feature. A key prerequisite for using dynamic routing is that the provider's metadata must contain information to distinguish between different providers. For more information about how to mark your application, see Configure service metadata.

Configure the DRM data source

Using DRM as the data source for service configuration is different from using it for an application's dynamic configuration. To enable DRM as a data source, you can import the following dependency:

<dependency>
    <groupId>com.alipay.cloud</groupId>
    <artifactId>sofa-service-governance-datasource-drm</artifactId>
</dependency>
  • If your application imports the DRM data source for both service configuration and dynamic configuration, the DRM client is not imported twice. This simply indicates the different uses of DRM.

  • The DRM data source is enabled after you set the common SOFA configurations.

When you import the dependency for the DRM data source, autoconfiguration enables configuration subscription for DRM by default. To disable the DRM data source, you can add the following configuration to the application.properties file:

spring.cloud.sofa.service.ds.drm.enabled=false

Configure the Nacos data source

To use Nacos as a data source, you can import the Nacos service configuration data source into your project.

<dependency>
    <groupId>com.alipay.cloud</groupId>
    <artifactId>sofa-service-governance-datasource-nacos</artifactId>
</dependency>

You can import the Spring Cloud Alibaba dependencies POM file to select a Nacos client version that matches your runtime environment.

In the application.properties file, you can add the following configurations to use Nacos as the data source for SOFA dynamic routing:

Configuration item

Description

com.alipay.sofa.ms.service.ds.nacos.serverAddr

A list of Nacos server addresses. This property has a higher priority than the Endpoint.

com.alipay.sofa.ms.service.ds.nacos.username

If authentication is enabled, this property sets the username.

com.alipay.sofa.ms.service.ds.nacos.password

If authentication is enabled, this property sets the password.

com.alipay.sofa.ms.service.ds.nacos.endpoint

The Nacos server Endpoint. For more information, see Nacos environment isolation.

com.alipay.sofa.ms.service.ds.nacos.namespace

The port for the Nacos server connection point. For more information, see Nacos Environment Fencing.

com.alipay.sofa.ms.service.ds.nacos.accessKey

The AccessKey for the Nacos server Endpoint.

com.alipay.sofa.ms.service.ds.nacos.secretKey

The Secret Key for the Nacos server Endpoint.

com.alipay.sofa.ms.service.ds.nacos.timeout

The timeout for reading configurations from Nacos. The default is 3 seconds.

com.alipay.sofa.ms.service.ds.nacos.order

The priority of the Nacos data source. The default is 100. A smaller ordinal number indicates a higher priority.

Important

You must configure either com.alipay.sofa.ms.service.ds.nacos.serverAddr or com.alipay.sofa.ms.service.ds.nacos.endpoint. Nacos is considered a valid data source only if one of these properties is set. Ensure that the connection information is configured correctly.

Configuration migration

To migrate the dynamic routing configuration from Nacos to DRM, you can configure both DRM and Nacos data sources. Both data sources are enabled simultaneously to facilitate data migration.

When an application first loads routing rules, if both DRM and Nacos have rules for the same service or URI of the request, the DRM configuration has higher priority by default. If you modify these duplicate rules, the change takes effect on the data source where the modification was made. Priority is not considered in this case.

Multi-data source support lets you migrate configurations while the application is running. You can use the grayscale release feature in the SOFA microservice console for grayscale validation. You can also roll back the configuration at any time.

To migrate configurations from DRM to Nacos, you can adjust the priority of the two data sources. The default priority for the DRM data source is 0, and for the Nacos data source is 100. A smaller ordinal number indicates a higher priority. Therefore, the DRM data source has a higher priority by default. You can lower the priority of the DRM data source or raise the priority of the Nacos data source. To do this, you can configure the following parameters in the application.properties file:

# Lower the priority of the DRM data source
spring.cloud.sofa.router.ds.drm.order=200
# Or raise the priority of the Nacos data source
spring.cloud.sofa.router.ds.nacos.order=-100

Configure routing rules

When you use Spring Cloud SOFA Registry to publish and subscribe to services and use Spring Cloud SOFA DRM as the configuration data source, you can configure routing rules in the SOFA microservice console. For more information, see Service routing.

Shut down dynamic routing

To disable the SOFA dynamic routing feature, you can set spring.cloud.sofa.router.gov.enabled to false in the application.properties file.

spring.cloud.sofa.router.gov.enabled=false