leo
leo

Reputation: 133

Spring cloud config automatically refresh all values on the fly

all: In my env, all configurations store in localfile,so my service configuration file stores in classpath:configs/.

So, when files in classpath:configs/ changes, needing refresh on the fly to provide latest properties, I need automatically refresh all values, How could i fulfill this demand?

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

I have an configurations.yml lays in moudule resources dir:

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

Entrypoint

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

It could print "world" when visit http://localhost:7002/prop2/ in browser, but when config server resources/configurations.yml changed, then curl -X POST http://localhost:7002/refresh nothing changed and just return [](It should return ["prop2"]) and the same result by visiting http://localhost:7002/prop2/.

logs in console when post /refresh:

Config Server:

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

Config Client:

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

Upvotes: 2

Views: 6543

Answers (1)

Ryan Baxter
Ryan Baxter

Reputation: 1257

I think this is a restriction of loading configuration from the classpath. Since you cannot change the classpath dynamically while the application is running we cannot reload the changes. The recommended way (as pointed out in the documentation) is to specify a search path location outside of the application (and also a location that is highly available) for production use cases. When you specify a search path location you can update the configuration and the config server will pick up those changes. https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_file_system_backend

Upvotes: 6

Related Questions