Introduction
Sometimes the client needs to retry when the config server is unresponsive to give config server time to recover. Using the retry component provided by spring, we can easily configure the retry mechanism, including retry interval, number of retry times, etc. I won’t say much below, let’s take a look at the detailed introduction together.
Project source code
Click to download
Add dependencies for web projects
To enable the client retry function, two new dependencies, spring-retry and spring-boot-starter-aop, add the following code to the pom.xml file of the web project:
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.2.2.RELEASE</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
Then add the following configuration in the bootstrap.yml file:
spring: application: name: web-client cloud: config: uri: http://localhost:8888 fail-fast: true retry: initial-interval: 1000 max-attempts: 6 max-interval: 2000 multipleer: 1.1
First, let spring.cloud.config.fail-fast be true, that is, if the remote configuration cannot be obtained, it will fail immediately, but try again with the following configuration.
spring.cloud.config.retry All children are default values:
test
If we use the project in this tutorial, we need to start the configserver project first, then start the registry project and start eureka, because the web client uses the eureka service, then close the configserver, and then start the web project. We will see the following log:
2018-05-15 16:04:58.421 INFO 2663 --- [ main] cccConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
After 6 retry failures, the client fails to start. If configserver is enabled midway, the web client starts successfully.
Fine-grained control retry
We can implement a more granular control retry mechanism in the code, create a new java class cn.zxuqian.configurations.RetryConfiguration in the web project, and add the following code:
package cn.zxuqian.configurations;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.retry.interceptor.RetryInterceptorBuilder;import org.springframework.retry.interceptor.RetryOperationsInterceptor;public class RetryConfiguration { private static Logger log = LoggerFactory.getLogger(RetryConfiguration.class); @Bean @ConditionalOnMissingBean(name = "configServerRetryInterceptor") public RetryOperationsInterceptor configServerRetryInterceptor() { log.info(String.format( "configServerRetryInterceptor: Changing backOffOptions " + "to initial: %s, multipleer: %s, maxInterval: %s", 1000, 1.2, 5000)); return RetryInterceptorBuilder .stateless() .backOffOptions(1000, 1.2, 5000) .maxAttempts(10) .build(); }} Here we define the configServerRetryInterceptor method for Spring Retry using our custom retry interceptor. Methods Use RetryInterceptorBuilder to create a stateless RetryOperationsInterceptor as required, and set the initial retry interval to 1000 milliseconds, the increase multiple is 1.2 times, the maximum retry interval is 5000 milliseconds, and the maximum retry times is 10 times. The builder also provides interfaces such as configuring the retry mechanism, which interested readers can study on their own.
@ConditionalOnMissingBean indicates that this bean is matched when there is no bean named configServerRetryInterceptor in the BeanFactory.
Finally, in src/main/resources/META-INF/ (no folder can be created) Create a new spring.factories file, specifying that we just created the class as the configuration at startup, so that it takes effect before obtaining the remote configuration:
org.springframework.cloud.bootstrap.BootstrapConfiguration=cn.zxuqian.configurations.RetryConfiguration
test
Finally, start the web project under the condition of closing configserver, and then you will see that the project starts fail after ten retrying.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.