【发布时间】:2015-07-13 01:20:35
【问题描述】:
我正在关注此链接: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage
我反复测试,没有看到Spring云客户端正在从云服务器加载配置,请帮忙看看错误在哪里:
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 的自动注入似乎找不到 production.host 环境值。
从配置服务器加载后如何读取客户端中的配置?
感谢您的帮助。
【问题讨论】:
-
如果您正在学习教程,请遵循教程。本教程提到你应该使用
spring-cloud-starter依赖而不是spring-cloud-config-client。对于应用程序而言,配置来自哪里并不重要,它们只是属性,您可以像任何其他属性一样通过Environment或@Value访问它们。 -
我改成client或者starter,但是在配置服务器中找不到配置值,有什么办法吗?
-
你能解决这个问题吗,我面临的问题是配置服务器的属性是从方法中开始读取的,如果它用 @RequestMapping 注释而不是其他方法。不知道在这种情况下可以做什么。