[英]Spring Cloud Configuration Server not working with local properties file - getting need to use a composite configuration
[英]Spring Cloud Configuration Server not working with local properties file
我一直在玩 github 上的 Spring Cloud 项目,位于: https://github.com/spring-cloud/spring-cloud-config
但是,我遇到了一些问题,让它读取本地属性文件而不是从 github 中提取属性。即使我删除了对 github 的所有引用,spring 似乎也忽略了本地文件。这里有一个类似的问题: Spring-Cloud 配置服务器忽略配置属性文件
但我还没有看到任何好的答案。 我想知道是否有人可以指出我的例子? 我想在本地设置我的属性,而不是使用任何类型的 git 存储库。 我假设有人以前遇到过这个,如果某处有这样的例子,我真的很想看看它,这样我就可以朝着正确的方向前进。
我所有的代码都在这里https://github.com/spencergibb/communityanswers/tree/so27131143
src / main / java / Application.java
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
src / main / resources / application.yml
spring:
application:
name: myconfigserver
profiles:
active: native
my:
property: myvalue
src / main / resources / 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(Test Micro Service)的配置。
弹簧配置应用程序的实施例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
使用spring.profiles.active = native是Spring文档所建议的,但是我也无法使它正常工作。 我的application.properties文件是
server.port=8888
spring.cloud.config.profiles=native
但网址的回应
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,则配置服务器将从默认的Spring Boot端口8080启动,但不应使用一个配置服务器。
在Mac OS环境中运行配置服务器时,我遇到了同样的问题。 在Linux或Windows中没有发生这种情况。
我在bootstrap.yml
文件中设置了本机属性,如下所示:
spring:
profiles:
active: native
最终,它在Mac上对我有用的方法是将活动配置文件传递给jar文件,就像这样。
java -jar config-server.jar --spring.profiles.active=native
我仍然不知道为什么它在Mac OS中表现不同。
如果配置服务器的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"}}]}
我能够使其与本地存储库和引导程序配置一起使用:
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
我在本地计算机上遇到了同样的问题,但在远程服务器上工作正常。
@Oreste的答案对我有用。 所以我再次检查了错误日志,发现
2018-06-25 16:09:49.789 INFO 4397 --- [ main] t.p.a.s.api.user.UserServiceApplication : The following profiles are active: someProfileISetBefore
因此,造成这种情况的根本原因是我之前设置了环境变量,但是却忘记删除它。 并且它会覆盖应用程序属性文件config。
希望你们不要像我一样犯傻的错误。 固定于:
unset spring_profiles_active
在Mac上,当文件路径中有空格时,我遇到了这个问题:
spring.cloud.config.server.native.search-locations=file:///Users/.../Development/Folder with a space /prj
另外请注意,在用户之前有3个斜杠:
spring.cloud.config.server.native.search-locations=file:///Users/...
或者我使用:
spring.cloud.config.server.native.search-locations=file://${user.home}/Desktop
本机属性是:
spring.profiles.active=native
这是我所做的:
以下https://medium.com/@danismaz.furkan/spring-cloud-config-with-file-system-backend-c18ae16b7ad5
1)!!不要忘记您的VM选项!!:
-Dspring.profiles.active=native
(在Netbeans中:configserver-> project-> properties> Run-> VM选项
2)在application.properties中
#spring.cloud.config.server.native.searchLocations=file:///C:/tmp/config
spring.cloud.config.server.native.searchLocations=classpath:/config
或Applications.yml
spring:
cloud:
config:
server:
native:
search-locations : file:///C:/tmp/config
#search-locations : classpath:/config
笔记:
n1:search-locations和searchLocations都可以工作
n2:file:/// =>硬文件路径
喜欢
c:/temp/config/
app-admin.yml
app-admin-develop.yml
....
n3:用于您的配置文件配置文件
类路径:/
喜欢
Other sources/src/main/resources/config/app-admin.yml
n4:我无法在不设置vm选项的情况下使文件路径正常工作。 不要忘记! 仅在您的应用程序配置中设置spring.profiles.active = native并不能解决我的问题
n5:http查询示例
http:// localhost:8998 / app-admin / default / yml
提供app-admin.yml
http:// localhost:8998 / app-admin / develop / yml
提供app-admin-develop.yml
我添加了以下两个属性,它开始工作了:
spring.cloud.config.server.git.default-label=main
spring.cloud.config.server.git.try-master-branch=true
我使用的是 spring 引导版本 2.6.0-SNAPSHOT
如果您尝试在 a.properties 文件中使用本地存储库,请将这些包含在服务器的 application.properties 文件中
server.port=8888
spring.profiles.active=git
spring.cloud.config.server.git.uri=file:/link-to-local-git-repo
您可以在 git 存储库目录中通过在终端上键入pwd
(ubuntu) 来获取本地 git 存储库的链接。
强调:确保 spring.profiles.active 的值是git不是原生的
然后输入
http://localhost:8888/<name-of-file>/default
在浏览器选项卡中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.