我正在尝试创建一个 Spring Cloud 配置服务器,它从属性文件而不是 github 中读取配置数据。服务器启动,但不提供文件中的属性。我在 classpapath 上有两个配置文件:
bootstrap.yml
spring:
application:
name: config-server
配置服务器.properties
foo=bar
当我转到据称应该给我 foo 属性值的 url 时:
curl http://localhost:8888/admin/env/foo
我得到一个错误: “时间戳”:1415298615005,“状态”:404,“错误”:“未找到”,“异常”:“org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint $NoSuchPropertyException”,“消息”:“没有这样的属性:foo","path":"/admin/env/foo"
我想知道我做错了什么?据我了解,属性文件名应与服务器名称匹配,以便服务器识别。
按照 spencergibb 的建议添加 native 配置文件没有帮助。我的 application.properties 看起来像:
server.port=8888
spring.profiles.active=native
spring.config.name=configserver
spring.application.name=configserver
请注意,我必须指定服务器端口。根据 Spring Cloud Config Server 文档,配置服务器默认在端口 8888 上启动。然而,在我的例子中,除非我在我的配置中指定端口,否则服务器从 8080 开始。
POM 文件没有父文件和单个依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.0.0.M2</version>
</dependency>
</dependencies>
应用程序没有什么特别之处:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigurationApp {
public static void main(String[] args) {
SpringApplication.run(ConfigurationApp.class, args);
}
}
configserver.properties 文件包含一个条目:foo=bar
首先我总是遇到启动错误
2014-11-07 09:35:42.852 ERROR 6972 --- [ main] b.c.PropertySourceBootstrapConfiguration : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/configserver/default/master":Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
无论我执行哪个命令,我总是从服务器得到相同的响应:
{"name":"info","label":"master","propertySources":[{"name":"bootstrap","source":{}},{"name":"applicationConfig: [classpath:/application.properties]","source":{"spring.config.name":"configserver","spring.application.name":"configserver","server.port":"8888","spring.profiles.active":"native"}},{"name":"defaultProperties","source":{"spring.application.name":"bootstrap"}}]}
我试过:
http://localhost:8888/configserver/env
http://localhost:8888/configserver/env/foo
http://localhost:8888/configserver/info
http://localhost:8888/configserver/beans
http://localhost:8888/configserver/health
响应总是如上
最佳答案
默认情况下,配置服务器提供来自 git 的属性。您需要使用 --spring.profiles.active=native
将配置文件设置为 native
,以便配置服务器为 spring 环境提供服务。配置服务器的 spring.config.name
以编程方式设置为 spring.config.name=configserver
因此您的属性文件需要是 configserver.properties。
关于java - Spring-Cloud 配置服务器忽略配置属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26786865/