Spring Config-Client doesn't refresh if Config-Server is down during initial startup

折月煮酒 提交于 2021-02-07 04:33:05

问题


I am running a test with a barebones Spring cloud config-server and a client-application. I executed a refresh scenario (by calling /refresh endpoint on the client-application) after config-server was down initially. Here is what I found

  • Client starts up with locally packaged properties when config-server is not reachable on startup. (I have the properties in application.yml that is bundled with client-application)
  • Git backend has different values for the properties compared to locally packaged version. Config-server is aware of the changes in git (Confirmed by connecting directly to config-server)
  • I bring up config-server and do a POST to /refresh endpoint on the client-application.
  • Client-application is not aware of the new properties from config-server.

In the second usecase

  • Client-application starts up and connects to config-server successfully. I see that the values from config-server have been fetched by the client-application successfully
  • I make a change in Git and call the /refresh endpoint on the client-application. Properties are refreshed successfully.

At this point it looks like /refresh doesn't work if the client-application comes up initially without being able to successfully connect to config-server. I am doing this to test a fallback strategy for the client-application if config-server is not reachable when the client-application is starting up. (The fallback strategy is to have locally packaged properties that will be used if config-server is not available on startup. If the config-server is available then the local properties are overriden). Any pointers to why this is not working and what I could do differently? Thanks in advance.

Edit

Server-Code

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

Client-Code

@RestController
@RefreshScope
@Component
public class Greeter {
    @Value("${message.greeting}")
    String greeting;

    @RequestMapping(value = "/",produces = "application/json")
    public List<String> index(){
        List<String> env = Arrays.asList("message.greeting: " + greeting);
        return env;
    }

}

bootstrap.yml (On config-client application)

spring:
  application:
    name: configclient
  cloud:
    config:
      uri: http://localhost:8888
management:
  security:
    enabled: false
logging:
  config: classpath:logback.xml
server:
  port: 8000

application.yml

message:
  greeting: Hello from Local!

Config in Git (Served through config-server)

message:
  greeting: Hello from Git-Edited!

回答1:


According to spring-cloud-config documentation -

If you expect that the config server may occasionally be unavailable when your app starts, you can ask it to keep trying after a failure. First you need to set spring.cloud.config.failFast=true, and then you need to add spring-retry and spring-boot-starter-aop to your classpath. The default behaviour is to retry 6 times with an initial backoff interval of 1000ms and an exponential multiplier of 1.1 for subsequent backoffs. You can configure these properties (and others) using spring.cloud.config.retry.* configuration properties.

Reference -> http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.1.RELEASE/



来源:https://stackoverflow.com/questions/42745501/spring-config-client-doesnt-refresh-if-config-server-is-down-during-initial-sta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!