Spring云领事中的配置值未自动更改

时间:2016-06-26 04:47:50

标签: spring-cloud consul

我有一个非常简单的Spring云应用程序如下:

@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";
    }
}

我在/ config / SpringCloudConsulDemo,dev /下的consul中配置了“my.message”。当我更改“my.message”的值时,我在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]

我也只是调用/ refresh的POST方法并得到一些输出:

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

但是在所有操作之后,“消息”的值没有相应地改变。请问有人帮忙吗?

1 个答案:

答案 0 :(得分:5)

Config值必须位于@ConfigurationProperties对象中,否则您的班级需要使用@RefreshScope进行注释。