Spring 云配置自动即时刷新所有值

Spring cloud config automatically refresh all values on the fly

所有: 在我的环境中,所有配置都存储在本地文件中,所以我的服务配置文件存储在 classpath:configs/.

因此,当 classpath:configs/ 中的文件发生变化时,需要即时刷新以提供最新的属性,我需要自动刷新所有值,我该如何满足这个需求?

Here is my configuration of config server:

application.yml

server:
  port: 8003
endpoints:
  restart:
    enabled: true
  refresh:
    enabled: true
spring:
  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/
 etcd:
   conn:
     etcdPassword: 134
     etcdUrls:
     - http://localhost:2379
     etcdUsername: root
     enabled: true
   etcdServicePrefix: /congiguration/project1/
   enabled: true
   timeout: 1

bootstrap.yml

spring:
  application:
    name: configurations
  profiles:
     active: native

我有一个 configurations.yml 模块 resources 目录:

configurations(-default or not).yml

prop1: Hello
prop2: world
etcd:
   conn:
     etcdPassword: 134

Here is my configuration of config client:

bootstrap.yml

spring:
  application:
    name: configurations
  cloud:
    config:
      uri: http://localhost:8003/

application.yml

server:
  port: 7002
    management:
  security:
    enabled: false

入口点

@RefreshScope
@RestController
class TestController {
@Value("${prop2}")
private String prop2;
@RequestMapping("/prop2")
public String from() {
    return this.prop2;
  }
}

在浏览器中访问http://localhost:7002/prop2/时可以打印"world",但是当配置服务器resources/configurations.yml改变时,然后curl -X POST http://localhost:7002/refresh什么都没有改变,只是 return [](它应该 return ["prop2"])和访问 http://localhost:7002/prop2/.

相同的结果

在 post /刷新时登录控制台:

配置服务器:

017-06-14 19:03:07.301  INFO 69939 --- [nio-8003-exec-4] 
s.c.a.AnnotationConfigApplicationContext 
Refreshingorg.springframework.context.annotation.
AnnotationConfigApplicationContext@45daa065: startup date [Wed Jun 14 19:03:07 
CST 2017]; root of context hierarchy
2017-06-14 19:03:07.320  INFO 69939 --- [nio-8003-exec-4] 
o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: 
classpath:configs/configurations.yaml
2017-06-14 19:03:07.320  INFO 69939 --- [nio-8003-exec-4] 
s.c.a.AnnotationConfigApplicationContext : Closing 
org.springframework.context.annotation.AnnotationConfigApplicationContext
@45daa065: startup date [Wed Jun 14 19:03:07 CST 2017]; root of context 
hierarchy

配置客户端:

2017-06-14 19:03:07.064  INFO 69942 --- [nio-7002-exec-3] 
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: 
http://localhost:8003/
2017-06-14 19:03:07.322  INFO 69942 --- [nio-7002-exec-3] 
c.c.c.ConfigServicePropertySourceLocator : Located environment: 
name=configurations, profiles=[default], label=master, version=null
2017-06-14 19:03:07.322  INFO 69942 --- [nio-7002-exec-3] 
b.c.PropertySourceBootstrapConfiguration : Located property source: 
CompositePropertySource [name='configService', propertySources=
[MapPropertySource [name='classpath:configs/configurations.yaml']]]
2017-06-14 19:03:07.324  INFO 69942 --- [nio-7002-exec-3] 
o.s.boot.SpringApplication               : No active profile set, falling back 
to default profiles: default
2017-06-14 19:03:07.326  INFO 69942 --- [nio-7002-exec-3] 
s.c.a.AnnotationConfigApplicationContext : Refreshing 
 org.springframework.context.annotation.AnnotationConfigApplicationContext
@2ff4dec0: startup date [Wed Jun 14 19:03:07 CST 2017]; parent: 
org.springframework.context.annotation.AnnotationConfigApplicationContext@109b
36f8
2017-06-14 19:03:07.336  INFO 69942 --- [nio-7002-exec-3] 
o.s.boot.SpringApplication               : Started application in 0.511 
seconds (JVM running for 231.593)
2017-06-14 19:03:07.336  INFO 69942 --- [nio-7002-exec-3] 
s.c.a.AnnotationConfigApplicationContext : Closing 
org.springframework.context.annotation.AnnotationConfigApplicationContext@2ff
4dec0: startup date [Wed Jun 14 19:03:07 CST 2017]; parent: 
org.springframework.context.annotation.AnnotationConfigApplicationContext@109b
36f8

我认为这是从类路径加载配置的限制。由于您无法在应用程序 运行 时动态更改类路径,因此我们无法重新加载更改。推荐的方法(如文档中所指出的)是为生产用例指定应用程序外部的搜索路径位置(以及高度可用的位置)。当您指定搜索路径位置时,您可以更新配置,配置服务器将获取这些更改。 https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_file_system_backend