Spring Cloud Config客户端未从配置服务器加载配置 [英] spring cloud config client not loading configuration from config server
问题描述
我正在关注此链接: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage
我一遍又一遍地进行了测试,但没有看到Spring cloud客户端正在从云服务器加载配置,请帮助查看错误在哪里:
I tested this again and again and not seeing Spring cloud client is loading configuration from cloud server, please help to see where is the error:
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
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/.
now http://localhost:8080/ can not be started at all.
创建名称为'configclientApplication'的bean时出错似乎@Value的自动注入无法找到production.host环境值.
Error creating bean with name 'configclientApplication' It seemed the auto inject of @Value can not find the production.host environment value.
从配置服务器加载后,如何读取客户端中的配置?
How can I read the configuration in client once loaded from config server?
感谢您的帮助.
推荐答案
正如Deinum所暗示的,我将确保您已将客户端配置为 parent 作为spring-cloud-starter-parent并给出它是一个版本.当您将Spring Cloud提供的依赖项包含在依赖项中时,请记住,Spring和boot是一个不同的项目,它将无法正常工作.更改为:
As Deinum implies, I'd ensure you have the client configured with the parent as spring-cloud-starter-parent and give it a version. Maven plugins provided by spring cloud wont work when you include in your dependencies and remember cloud is a different project than boot. Change it to:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>1.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
第二,作为一门新学科(这里可能不是您的问题),我将在您的应用程序上使用新注释,而不是@Configuration和@EnableAutoConfiguration
Second, as a new discipline (and likely not your problem here), I'd use the new annotation on your Application instead of @Configuration and @EnableAutoConfiguration
@SpringBootApplication
第三,再次检查您的配置服务器上是否有@EnableConfigServer
Third, double check you have @EnableConfigServer on your config server
第四,请确保您的客户端上的bootstrap.properties指定了您的spring应用程序名称:
Fourth, be sure your bootstrap.properties on your client has your spring application name specified:
spring.application.name=spirent
最后,如果您使用了spring-cloud-config示例项目,则必须在URI中设置默认的用户名和安全密码:
Finally, if you used the spring-cloud-config example project, there is a default user and security password that you have to set in your URI:
http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c@localhost:8888
否则,请尝试从此处的spring-cloud-config项目开始,以确保正确设置了配置服务器:
Otherwise, try starting from the spring-cloud-config project located here to ensure your config server is setup correctly:
https://github.com/spring-cloud/spring-cloud-config
这篇关于Spring Cloud Config客户端未从配置服务器加载配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!