简体   繁体   English

Spring Cloud Config Server并在整个Spring Boot应用程序上应用@RefreshScope

[英]Spring Cloud Config Server and applying @RefreshScope on an entire Spring Boot application

I'm trying to refresh my properties, application-wide, on the fly, with Spring Cloud Config Server. 我正在尝试使用Spring Cloud Config Server即时刷新应用程序范围内的属性。 I'm embedding my Config Server with my Spring Boot Client App and the app boots up perfectly. 我将自己的Config Server嵌入到Spring Boot Client App中,并且该应用程序完美启动。

When I try to update a property, for example: spring.datasource.url then I send a POST request to refresh the actuator : http://localhost:9190/management/refresh , I get the expected result : 当我尝试更新属性时,例如: spring.datasource.url然后发送POST请求以刷新执行器: http://localhost:9190/management/refresh ,我得到了预期的结果:

[
   "spring.datasource.url"
]

I also created a controller for testing purposes : 我还创建了一个用于测试目的的控制器:

@Value("${spring.datasource.url}")
private String message;

@RequestMapping("/message")
String getMessage() {
    return this.message;
}

However, when I hit this controller on : http://localhost:9290/message I'm still getting the old unrefreshed value for ${spring.datasource.url} . 但是,当我在http:// localhost:9290 / message上命中此控制器时,我仍在获得${spring.datasource.url}的旧未刷新值。

I have to explicitly add a @RefreshScope on my controller's class to get the updated value. 我必须在控制器的类上显式添加@RefreshScope才能获取更新的值。

My question is : how can I achieve this application-wide ? 我的问题是:如何在整个应用程序范围内实现? or for each of these use cases ? 还是对于每个用例? :

  • @Value s throughout my code, 我的代码中@Value
  • my jdbcTemplate that uses my datasource 我的使用我的datasource jdbcTemplate
  • <context:property-placeholder> for my spring integration job <context:property-placeholder>用于我的春季整合工作
  • logging with log4j 使用log4j记录

I'm using : 我正在使用 :

<spring-cloud.version>Edgware.SR1</spring-cloud.version>
<spring-boot-version>1.5.9.RELEASE</spring-boot-version>

@RefreshScope is needed to recreate the bean which in turn gets the updated value from the Spring Environment. 需要@RefreshScope来重新创建bean,该bean又从Spring Environment获取更新后的值。 The other option is to use @ConfigurationProperties beans which automatically are re-bound on /refresh without using @RefreshScope . 另一个选择是使用@ConfigurationProperties Bean,它们会自动在/refresh上重新绑定,而不使用@RefreshScope There is no option to make all beans @RefreshScope automatically. 没有选项可以自动使所有bean @RefreshScope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM