Tonny Tc
Tonny Tc

Reputation: 930

Config value not auto changed in spring cloud consul

I have a very simple spring cloud app as follows:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableAutoConfiguration
public class SpringCloudConsulDemoApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SpringCloudConsulDemoApplication.class).web(true).run(args);
    }

    @Value("${my.message}")
    private String message; 

    @RequestMapping("/")
    public String home() {
        return message;
    }

    @RequestMapping("/health")
    public String health() {
        return "Hello world";
    }
}

I config the "my.message" in consul under /config/SpringCloudConsulDemo,dev/. When I changed the value of "my.message", I got some output in spring cloud app:

2016-06-26 12:41:18.621  INFO 1187 --- [pool-1-thread-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@601c9895: startup date [Sun Jun 26 12:41:18 CST 2016]; root of context hierarchy
2016-06-26 12:41:18.631  INFO 1187 --- [pool-1-thread-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-06-26 12:41:18.632  INFO 1187 --- [pool-1-thread-1] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7facc2d7] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-06-26 12:41:18.687  INFO 1187 --- [pool-1-thread-1] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource@1251328325 [name='config/SpringCloudConsulDemo,dev/', properties=com.ecwid.consul.v1.ConsulClient@58b50524], ConsulPropertySource@119653652 [name='config/SpringCloudConsulDemo/', properties=com.ecwid.consul.v1.ConsulClient@58b50524], ConsulPropertySource@2105869565 [name='config/application,dev/', properties=com.ecwid.consul.v1.ConsulClient@58b50524], ConsulPropertySource@427716065 [name='config/application/', properties=com.ecwid.consul.v1.ConsulClient@58b50524]]]
2016-06-26 12:41:18.691  INFO 1187 --- [pool-1-thread-1] o.s.boot.SpringApplication               : The following profiles are active: dev
2016-06-26 12:41:18.692  INFO 1187 --- [pool-1-thread-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@e4a3026: startup date [Sun Jun 26 12:41:18 CST 2016]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@601c9895
2016-06-26 12:41:18.693  INFO 1187 --- [pool-1-thread-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-06-26 12:41:18.699  INFO 1187 --- [pool-1-thread-1] o.s.boot.SpringApplication               : Started application in 0.123 seconds (JVM running for 1113.16)
2016-06-26 12:41:18.699  INFO 1187 --- [pool-1-thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@e4a3026: startup date [Sun Jun 26 12:41:18 CST 2016]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@601c9895
2016-06-26 12:41:18.699  INFO 1187 --- [pool-1-thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@601c9895: startup date [Sun Jun 26 12:41:18 CST 2016]; root of context hierarchy
2016-06-26 12:41:18.758 DEBUG 1187 --- [pool-1-thread-1] o.s.c.c.discovery.HeartbeatProperties    : Computed heartbeatInterval: PT20S
2016-06-26 12:41:18.808  INFO 1187 --- [pool-1-thread-1] o.s.c.e.event.RefreshEventListener       : Refresh keys changed: [spring.cloud.client.hostname, my.message]

I just also call the POST method of /refresh and get some output:

2016-06-26 12:44:12.962  INFO 1187 --- [nio-8093-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2757ca3e: startup date [Sun Jun 26 12:44:12 CST 2016]; root of context hierarchy
2016-06-26 12:44:12.974  INFO 1187 --- [nio-8093-exec-3] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-06-26 12:44:12.978  INFO 1187 --- [nio-8093-exec-3] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7facc2d7] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-06-26 12:44:13.056  INFO 1187 --- [nio-8093-exec-3] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='consul', propertySources=[ConsulPropertySource@1023386348 [name='config/SpringCloudConsulDemo,dev/', properties=com.ecwid.consul.v1.ConsulClient@2fc3016a], ConsulPropertySource@1663976669 [name='config/SpringCloudConsulDemo/', properties=com.ecwid.consul.v1.ConsulClient@2fc3016a], ConsulPropertySource@1066555341 [name='config/application,dev/', properties=com.ecwid.consul.v1.ConsulClient@2fc3016a], ConsulPropertySource@2117822993 [name='config/application/', properties=com.ecwid.consul.v1.ConsulClient@2fc3016a]]]
2016-06-26 12:44:13.060  INFO 1187 --- [nio-8093-exec-3] o.s.boot.SpringApplication               : The following profiles are active: dev
2016-06-26 12:44:13.060  INFO 1187 --- [nio-8093-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@f738b82: startup date [Sun Jun 26 12:44:13 CST 2016]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@2757ca3e
2016-06-26 12:44:13.061  INFO 1187 --- [nio-8093-exec-3] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-06-26 12:44:13.066  INFO 1187 --- [nio-8093-exec-3] o.s.boot.SpringApplication               : Started application in 0.534 seconds (JVM running for 1287.532)
2016-06-26 12:44:13.066  INFO 1187 --- [nio-8093-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@f738b82: startup date [Sun Jun 26 12:44:13 CST 2016]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@2757ca3e
2016-06-26 12:44:13.066  INFO 1187 --- [nio-8093-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2757ca3e: startup date [Sun Jun 26 12:44:12 CST 2016]; root of context hierarchy
2016-06-26 12:44:13.103 DEBUG 1187 --- [nio-8093-exec-3] o.s.c.c.discovery.HeartbeatProperties    : Computed heartbeatInterval: PT20S

But after all the operations, the value of "message" has not been changed accordingly. Could anyone help for the case, please?

Upvotes: 1

Views: 1740

Answers (1)

spencergibb
spencergibb

Reputation: 25157

The Config values need to be in a @ConfigurationProperties object or your class need to be annotated with @RefreshScope.

Upvotes: 5

Related Questions