Spring Cloud Configuration Server无法使用本地属性文件

我一直在github上玩Spring Cloud项目: https : //github.com/spring-cloud/spring-cloud-config

但是我遇到了一些问题,让它读取本地属性文件而不是从github中提取属性。 即使我删除了对github的所有引用,spring似乎也忽略了本地文件。 此处发布了类似的问题: Spring-Cloud配置服务器忽略配置属性文件

但我还没有看到任何好的答案。 我想知道是否有人能指出我的一个例子? 我想在本地设置我的属性,而不是使用任何类型的git repo。 我假设有人之前遇到过这种情况,如果有某个例子,我真的很想看到它,这样我才能朝着正确的方向前进。

我的所有代码都在这里https://github.com/spencergibb/communityanswers/tree/so27131143

的src /主/爪哇/ Application.java

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

SRC /主/资源/ application.yml

 spring: application: name: myconfigserver profiles: active: native my: property: myvalue 

SRC /主/资源/ myapp.yml

 my: otherprop: myotherval 

要获取名为myapp的应用程序的属性,请执行以下操作。

curl http://localhost:8080/myapp/default

 { "name": "default", "label": "master", "propertySources": [ { "name": "applicationConfig: [classpath:/myapp.yml]", "source": { "my.otherprop": "myotherval" } }, { "name": "applicationConfig: [classpath:/application.yml]", "source": { "spring.application.name": "myconfigserver", "spring.profiles.active": "native", "my.property": "myvalue" } } ] } 

我能够使用Spring配置服务器读取apple-service(测试微服务)的配置。

spring config应用程序的application.yml示例

 spring: profiles: active: native cloud: config: server: native: searchLocations: classpath:config/ server: port: 8888 endpoints: restart: enabled: true 

将.properties或.yml文件放在src \ main \ resources \ config文件夹中。 确保此文件的名称应与您的微服务的spring.application.name匹配。

例如,如果spring.application.name = apple-service,那么属性文件应该是src \ main \ resources \ config文件夹中的apple-service.properties

apple-service的示例bootstrap.yml

 spring: application: name: apple-service cloud: config: uri: http://localhost:8888 

在Mac OS环境中运行配置服务器时遇到了同样的问题。 这在Linux或Windows中没有发生。

我在bootstrap.yml文件中设置了本机属性,如下所示:

 spring: profiles: active: native 

最后,它在mac上为我工作的方式是将活动配置文件传递给jar文件,就像这样。

 java -jar config-server.jar --spring.profiles.active=native 

我仍然不知道为什么它在Mac OS中表现不同。

使用spring.profiles.active = native是Spring文档似乎提出的建议,但我也无法让它工作。 我的application.properties文件是

 server.port=8888 spring.cloud.config.profiles=native 

但来自URL的响应

 http://localhost:8888/config-server/env 

 {"name":"env","label":"master","propertySources":[{"name":"https://github.com/spring-cloud-samples/config-repo/application.yml","source":{"info.url":"https://github.com/spring-cloud-samples","info.description":"Spring Cloud Samples"}}]} 

表示原始配置文件被忽略,服务器仍将github视为属性源。

我遇到的一个小问题是配置服务默认端口。 根据Sprin Cloud Config文档,它应该是8888.如果我从我的application.properties中删除server.port = 8888,配置服务器在端口8080上启动,它是默认的Spring Boot端口,但不是一个配置服务器应该使用的。

我能够使用本地repo和bootstrap配置工作:

 java -jar spring-cloud-config-server-1.0.0.M3-exec.jar --spring.config.name=bootstrap 

bootstrap.yml文件放在./config/文件夹中。

 server: port: 8080 spring: config: name: cfg_server cloud: config: server: git: uri: /home/us/config_repo searchPaths: pmsvc,shpsvc 

如果config服务器的application.properties包含以下内容,配置服务器将读取本地属性文件:

 spring.profiles.active=native **spring.cloud.config.server.native.searchLocations=file:/source/tmp** 

/source/tmp目录中,存储客户端的本地属性文件,例如:

 http://localhost:8888/a-bootiful-client/default 

你会得到:

 {"name":"a-bootiful-client","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:/source/tmp/a-bootiful-client.properties","source":{"message":"Kim"}}]} 

我在本地计算机上遇到了同样的问题,但它在我的远程服务器上工作正常。

@Oreste的回答对我有用。 所以我再次检查错误日志,然后我找到了

  2018-06-25 16:09:49.789 INFO 4397 --- [ main] tpasapi.user.UserServiceApplication : The following profiles are active: someProfileISetBefore 

所以,我的情况的根本原因是我之前设置了一个环境变量,但我忘记删除它。 它会覆盖应用程序属性文件配置。

希望你们不要像我一样犯这个愚蠢的错误。 修正:

  unset spring_profiles_active