【问题标题】:Spring-Cloud configuration server ignores configuration properties fileSpring-Cloud 配置服务器忽略配置属性文件
【发布时间】:2015-01-03 09:30:19
【问题描述】:

我正在尝试创建一个 Spring Cloud 配置服务器,它从属性文件而不是 github 读取配置数据。服务器启动,但不提供文件中的属性。我在 classpapath 上有两个配置文件:

bootstrap.yml

spring:
application:
    name: config-server

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 的建议添加本地配置文件并没有帮助。我的 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

响应总是如上

【问题讨论】:

    标签: java spring-cloud


    【解决方案1】:

    默认情况下,配置服务器从 git 提供属性。您需要使用--spring.profiles.active=native 将配置文件设置为native,以便配置服务器为spring 环境提供服务。配置服务器的spring.config.name 以编程方式设置为spring.config.name=configserver,因此您的属性文件需要是configserver.properties。

    【讨论】:

    【解决方案2】:

    配置服务器中的“/admin/env”端点仅服务于服务器本身的本地配置。服务器通常只是一个常规的 Spring Boot 应用程序,因此它从“application.properties”获取配置。如果您想从“config-server.properties”中获取它,您需要设置“spring.config.name”或“spring.config.location”(就像普通的 Boot 应用程序一样)。

    【讨论】:

      猜你喜欢
      • 2015-06-18
      • 1970-01-01
      • 2020-11-25
      • 2016-02-07
      • 2017-09-07
      • 2018-08-24
      • 2016-07-12
      • 2019-04-06
      • 2015-02-18
      相关资源
      最近更新 更多