如果 Config-Server 在初始启动期间关闭,则 Spring Config-Client 不会刷新 [英] Spring Config-Client doesn't refresh if Config-Server is down during initial startup
问题描述
我正在使用准系统 Spring 云配置服务器和客户端应用程序运行测试.我执行了一个刷新场景(通过在客户端应用程序上调用/refresh 端点)在配置服务器最初关闭之后.这是我发现的
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
- 当在启动时无法访问 config-server 时,客户端会使用本地打包的属性启动.(我在 application.yml 中有与客户端应用程序捆绑在一起的属性)
- 与本地打包版本相比,Git 后端具有不同的属性值.config-server 知道 git 的变化(通过直接连接到 config-server 确认)
- 我打开 config-server 并对客户端应用程序上的/refresh 端点执行 POST.
- 客户端应用程序不知道来自 config-server 的新属性.
在第二个用例中
- 客户端应用程序启动并成功连接到配置服务器.我看到客户端应用程序已成功获取来自 config-server 的值
- 我在 Git 中进行了更改并调用客户端应用程序上的/refresh 端点.属性已成功刷新.
此时,如果客户端应用程序最初启动而无法成功连接到配置服务器,则/refresh 似乎不起作用.我这样做是为了测试如果客户端应用程序启动时无法访问配置服务器,则客户端应用程序的回退策略.(回退策略是拥有本地打包的属性如果 config-server 在启动时不可用,将使用它.如果 config-server 可用,则覆盖本地属性).任何关于为什么这不起作用的指针和我可以做些什么不同的事情?提前致谢.
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.
编辑
服务器代码
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
客户端代码
@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(在配置客户端应用程序上)
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!
Git 中的配置(通过 config-server 提供)
Config in Git (Served through config-server)
message:
greeting: Hello from Git-Edited!
推荐答案
根据spring-cloud-config文档-
According to spring-cloud-config documentation -
如果您预计配置服务器可能偶尔不可用当您的应用程序启动时,您可以要求它在失败后继续尝试.首先你需要设置spring.cloud.config.failFast=true,然后你需要将 spring-retry 和 spring-boot-starter-aop 添加到您的类路径.默认行为是使用初始值重试 6 次退避间隔为 1000 毫秒,指数乘数为 1.1随后的退避.您可以配置这些属性(和其他)使用 spring.cloud.config.retry.* 配置属性.
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.
参考 -> http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.1.RELEASE/
这篇关于如果 Config-Server 在初始启动期间关闭,则 Spring Config-Client 不会刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!