Spring cloud config 自动即时刷新所有值

leo*_*leo 2 spring-cloud-config

all:在我的 env 中,所有配置都存储在 localfile 中?所以我的服务配置文件存储在 classpath:configs/ 中。

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

Here is my configuration of config server:

应用程序.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
Run Code Online (Sandbox Code Playgroud)

引导程序.yml

spring:
  application:
    name: configurations
  profiles:
     active: native
Run Code Online (Sandbox Code Playgroud)

我在模块资源目录中有一个configuration.yml :

配置(-默认与否).yml

prop1: Hello
prop2: world
etcd:
   conn:
     etcdPassword: 134
Run Code Online (Sandbox Code Playgroud)

Here is my configuration of config client:

引导程序.yml

spring:
  application:
    name: configurations
  cloud:
    config:
      uri: http://localhost:8003/
Run Code Online (Sandbox Code Playgroud)

应用程序.yml

server:
  port: 7002
    management:
  security:
    enabled: false
Run Code Online (Sandbox Code Playgroud)

入口点

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

它可以http://localhost:7002/prop2/在浏览器中访问时打印“world” ,但是当配置服务器resources/configurations.yml发生变化时,curl -X POST http://localhost:7002/refresh什么都没有改变,只返回 [](It should return ["prop2"]) 和访问相同的结果http://localhost:7002/prop2/

发布/刷新时登录控制台:

配置服务器:

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
Run Code Online (Sandbox Code Playgroud)

配置客户端:

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
Run Code Online (Sandbox Code Playgroud)

Rya*_*ter 6

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