Spring cloud config会自动刷新所有值

时间:2017-06-12 02:36:12

标签: spring-cloud-config

所有:    在我的环境中,所有配置都存储在localfile中,因此我的服务配置文件存储在classpath中:configs /.

因此,当classpath中的文件:配置/更改,需要动态刷新以提供最新属性时,我需要自动刷新所有值,我怎样才能满足这种需求?

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位于moudule 资源目录:

配置(-default与否).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/时可以打印“世界”,但是当配置服务器 resources/configurations.yml 发生更改时,curl -X POST http://localhost:7002/refresh没有任何更改,只返回[] (它应该返回[“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

配置客户端

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

1 个答案:

答案 0 :(得分:2)

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