在本地启动了1个 Consul。consul agent -dev 添加了配置
spring: application: name: spring-cloud-consul-ok cloud: consul: host: 127.0.0.1 port: 8500 discovery: enabled: true register: true healthCheckInterval: 15s health-check-timeout: 1s instance-id: ${spring.application.name}-${spring.cloud.consul.host}-${spring.cloud.consul.port} serviecName: ${spring.application.name} config: enabled: true format: YAML data-key: configuration
属性配置类:
@Configuration @ConfigurationProperties("sample") @Data @RefreshScope public class SampleProperties { private String prop = "default data"; }
引用方式:
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@RestController
@EnableConfigurationProperties
@EnableFeignClients
@Slf4j
public class ConsulAreYouOkApplication {
@Autowired
private RibbonHelloService ribbonHelloService;
@Autowired
private SampleProperties sampleProperties;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String areyouok(@RequestParam String name) {
return ribbonHelloService.sayHi(name)+" "+sampleProperties.getProp();
}
看着没什么错,但是一直 拿不到 Consul上面的配置,从日志里面 也可以看到Spring已经检测到了 属性的变化,但是就是一直获取不到新值。 版本如下 spring-boot-dependencies 1.5.10.RELEASE spring-cloud-consul-config 1.3.2.RELEASE spring-cloud-starter-consul-discovery 1.3.2.RELEASE
我已经自己找到原因了,但是不告诉你们,O(∩_∩)O哈哈~。。过年好######支持,其实遇到技术问题问没用的,没人能回答你######这种精神就不对啊,要开源��######把consol的配置 配置到bootstrap.yml就可以了######@J-Fla 我用consul的key-value可以,但是使用yaml 就不行,请指教,consul的key和注解的java类怎么个对应关系。######把consol的配置 配置到bootstrap.yml就可以了 , 已经在第2个留言那里回复过了######@J-Fla ,首先我真的认真的看了你的帖子了,我的主要代码如下: 这种yaml方式说什么也拿不到name的值,但是我用key-value方式是可以的,请指正。######你好,因为我之前实验过,如果consol的链接信息不写在bootstrap.yml中的话,springboot 实际上相当于没有配置使用 consul 的这个config ,明白了吗,所以你看一下 是否链接consul的信息是在boostrap.yml中配置的######https://my.oschina.net/u/1262235/blog/3035425
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。