Prefetch subscriptions

Updated at:

By default, Spring Cloud applications subscribe to a service only when that service is first invoked. This approach can negatively impact the performance of the first service invocation and delay error detection until runtime. To address this, Spring Cloud SOFA provides a subscription prefetch feature. This feature completes service subscriptions during the application startup process, which reduces the performance impact of the subscription logic on the first service access.

Subscription prefetch configuration

The subscription prefetch feature is enabled by default. If your application uses OpenFeign as its service framework, no additional configuration is required. Spring Cloud SOFA automatically detects the OpenFeign client and initiates the service subscriptions.

If you use other service invocation frameworks, such as RestTemplate, Spring Cloud SOFA cannot detect which services to invoke. In this case, you must explicitly specify the names of the services to prefetch by configuring spring.cloud.sofa.discovery.warmUpServices in the application.properties file. For example:image.png

Separate multiple service names with commas (,). If the subscription prefetch runs as expected, the following information appears in the application startup log:

image.png

If multiple services are specified for prefetching, they are subscribed to sequentially.

Confirmation

The SOFA registry center uses a fully asynchronous method for service subscriptions. Therefore, the subscription action completes very quickly. After the subscription is complete, the client waits for the registry center to push the service list. This process usually completes within a few seconds. When the registry center pushes the service list, the following information appears in the application startup log:image.png

When Spring Cloud SOFA subscribes to a service, it subscribes to two types of data: a list of service endpoints and service configuration information. Therefore, after a successful subscription, the application receives two data pushes from the registry center. In the preceding figure, the log shows that a service endpoint was received, but the service configuration is empty. For more information about how to use service configuration data, see View services.

Important

When you use the SOFA registry center for service subscriptions, a subscription is considered complete only after the client receives at least one service list and one service configuration. If either type of data is missing, the push is considered unsuccessful.

Disable subscription prefetching

If you do not want to use the subscription prefetch feature, you can disable it by setting spring.cloud.sofa.discovery.warm-up-enabled to false in the application.properties file. After you disable this feature, the service subscription logic reverts to the default behavior of Spring Cloud.

spring.cloud.sofa.discovery.warm-up-enabled=false