java Spring-Cloud 配置服务器忽略配置属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26786865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:36:07  来源:igfitidea点击:

Spring-Cloud configuration server ignores configuration properties file

javaspring-cloud

提问by MrkK

I am trying to create a Spring Cloud configuration server which reads the configuration data from the properties file and not a github. The server starts, but does not serve the properties from the file. I have two configuration files on the classpapath:

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

bootstrap.yml

引导程序.yml

spring:
application:
    name: config-server

config-server.properties

config-server.properties

foo=bar

when I go to the url which supposedly should give me the value of the foo property:

当我转到应该给我 foo 属性值的 url 时:

curl  http://localhost:8888/admin/env/foo

I get an error: "timestamp":1415298615005,"status":404,"error":"Not Found","exception":"org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint$NoSuchPropertyException","message":"No such property: foo","path":"/admin/env/foo"}

我收到一个错误:"timestamp":1415298615005,"status":404,"error":"Not Found","exception":"org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint$NoSuchPropertyException","message ":"没有这样的属性:foo","path":"/admin/env/foo"}

I am wondering what I am doing wrong? As far as I understand the properties file name should match the server name in order to be recognized by the server.

我想知道我做错了什么?据我了解,属性文件名应该与服务器名称匹配才能被服务器识别。



adding native profile as spencergibb suggested did not help. my application.properties looks like:

按照 spencergibb 的建议添加本机配置文件没有帮助。我的 application.properties 看起来像:

server.port=8888
spring.profiles.active=native
spring.config.name=configserver
spring.application.name=configserver

Note, that I had to specify the server port. According to Spring Cloud Config Server documentation the configuration server starts on port 8888 by default. In my case however unless I specify the port in my config the server starts at 8080.

请注意,我必须指定服务器端口。根据 Spring Cloud Config Server 文档,配置服务器默认在端口 8888 上启动。但是,就我而言,除非我在配置中指定端口,否则服务器将从 8080 开始。

The POM file has no parent and a single dependency:

POM 文件没有父文件和单个依赖项:

    <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.0.0.M2</version>
    </dependency>
</dependencies>

The application has nothing special in it:

该应用程序没有什么特别之处:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigurationApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigurationApp.class, args);
    }
}

The configserver.properties file contains a single entry: foo=bar

configserver.properties 文件包含一个条目:foo=bar

First of all I am always getting a startup error

首先,我总是收到启动错误

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

Regardless of which command I execute I am always getting the same response from the server:

无论我执行哪个命令,我总是从服务器得到相同的响应:

{"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"}}]}

I tried:

我试过:

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

The response is always as above

响应总是如上

回答by spencergibb

By default, the config server serves properties from git. You will need to set the profile to nativeusing --spring.profiles.active=nativefor the configserver to serve the spring environment. The spring.config.namefor the config server is programmatically set to spring.config.name=configserverso your properties file will need to be configserver.properties.

默认情况下,配置服务器提供来自 git 的属性。您需要将配置文件设置为native使用--spring.profiles.active=native的configserver服务于春季环境。在spring.config.name为配置服务器编程设置为spring.config.name=configserver使您的属性文件将需要configserver.properties。

回答by Dave Syer

The "/admin/env" endpoint in a config server only serves the local configuration of the server itself. The server is usually just a regular Spring Boot app, so it gets its configuration from "application.properties". If you want to pick it up from "config-server.properties" you need to set "spring.config.name" or "spring.config.location" (just like a normal Boot app).

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