我正在点击此链接: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage
我测试了一遍又一遍,没有看到Spring cloud客户端正在从云服务器加载配置,请帮忙看看错误在哪里:
POM:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
应用:
@Configuration
@EnableAutoConfiguration
@RestController
public class ConfigclientApplication {
@Value("${spring.cloud.config.uri}")
String url;
@Value("${production.host}")
String host;
@RequestMapping("/")
public String home() {
return "Host is => " + this.host ;
}
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
}
bootstrap.properties: spring.cloud.config.uri=http://localhost:8888
- 配置服务器很好: http://localhost:8888/spirent/default
{
"name": "spirent",
"profiles": [
"default"
],
"label": "master",
"propertySources": [
{
"name": "classpath:/spirent.yml",
"source": {
"production.host": "server1",
"production.port": 9999,
"production.value1": 12345,
"test.host": "server2.com",
"test.port": 4444,
"test.value": "hello123"
}
}
]
}
现在http://localhost:8080/根本无法启动。
创建名称为“configclientApplication”的 bean 时出错 看来@Value的自动注入(inject)找不到product.host环境值。
从配置服务器加载后如何读取客户端中的配置?
感谢您的帮助。
最佳答案
如果您使用的是 2020.0.0 版本的 Spring Cloud,则需要将此依赖项添加到您的 Maven 依赖项中以启用配置 Bootstrap ,该功能在 2020.0.0 中默认禁用。
这对我有用。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
关于Spring Cloud配置客户端未从配置服务器加载配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016868/