【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
在本地启动了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哈哈~。。过年好
@J-Fla 我用consul的key-value可以,但是使用yaml 就不行,请指教,consul的key和注解的java类怎么个对应关系。
https://my.oschina.net/u/1262235/blog/3035425