Java Spring云配置服务器无法使用本地属性文件
我一直在github上玩Spring云项目,该项目位于: 但是,我在让它读取本地属性文件而不是从github中提取属性时遇到了一些问题。spring似乎忽略了本地文件,即使我删除了对github的所有引用。这里发布了一个类似的问题:Java Spring云配置服务器无法使用本地属性文件,java,spring,spring-cloud,Java,Spring,Spring Cloud,我一直在github上玩Spring云项目,该项目位于: 但是,我在让它读取本地属性文件而不是从github中提取属性时遇到了一些问题。spring似乎忽略了本地文件,即使我删除了对github的所有引用。这里发布了一个类似的问题: 但我还没有看到任何好的答案。我想知道是否有人能给我举个例子?我想在本地设置我的属性,而不是使用任何类型的git回购。我假设以前有人遇到过这种情况,如果在某个地方有这样的例子,我真的很想看看它,这样我就可以朝着正确的方向前进。使用spring.profiles.act
但我还没有看到任何好的答案。我想知道是否有人能给我举个例子?我想在本地设置我的属性,而不是使用任何类型的git回购。我假设以前有人遇到过这种情况,如果在某个地方有这样的例子,我真的很想看看它,这样我就可以朝着正确的方向前进。使用spring.profiles.active=native是spring文档似乎建议的,但我也无法让它工作。 我的application.properties文件是
server.port=8888
spring.cloud.config.profiles=native
但是来自URL的响应
http://localhost:8888/config-server/env
是
这表明本机配置文件已被忽略,服务器仍将github视为属性源
我遇到的另一个小问题是配置服务默认端口。根据Sprin云配置文档,它应该是8888。如果我从application.properties中删除server.port=8888,配置服务器将从端口8080启动,这是默认的Spring启动端口,但不是配置服务器应该使用的端口。我的所有代码都在这里
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
的应用程序的属性,请执行以下操作
curlhttp://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"
}
}
]
}
我能够通过本地回购和引导配置使其工作:
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
在Mac OS环境中运行配置服务器时,我遇到了同样的问题。这在Linux或Windows中没有发生 我在
bootstrap.yml
文件中设置了本机属性,如下所示:
spring:
profiles:
active: native
最后,我在mac上的工作方式是将活动配置文件传递给jar文件,如下所示
java -jar config-server.jar --spring.profiles.active=native
我仍然不知道为什么它在Mac OS中的行为会有所不同。我能够使用Spring配置服务器读取apple服务(Test Micro service)的配置 spring配置应用程序的示例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文件夹中。确保此文件的名称应与micro服务的spring.application.name匹配
例如,如果spring.application.name=apple service,则属性文件应该是src\main\resources\config文件夹中的apple service.properties
苹果服务的bootstrap.yml示例:
spring:
application:
name: apple-service
cloud:
config:
uri: http://localhost:8888
如果配置服务器的
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"}}]}
我在本地机器上也遇到了同样的问题,但在远程服务器上可以正常工作 @俄瑞斯特的回答对我来说确实有用。所以我再次检查了错误日志,发现
2018-06-25 16:09:49.789 INFO 4397 --- [ main] t.p.a.s.api.user.UserServiceApplication : The following profiles are active: someProfileISetBefore
所以,我的案例的根本原因是我之前设置了一个环境变量,但我忘记了删除它。它会覆盖应用程序属性文件配置
希望你们不要像我一样犯愚蠢的错误。固定人:
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://${user.home}/Desktop
本机属性为:
spring.profiles.active=native
以下是我所做的:
跟随
1) !!别忘了你的虚拟机选项!!:
-Dspring.profiles.active=native
(在Netbeans中:配置服务器->项目->属性->运行->虚拟机选项
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:搜索位置和搜索位置都有效
n2:文件://=>硬文件路径
像
n3:用于您的配置文件配置文件
类路径:/
像
n4:如果不设置vm选项,我无法使文件路径正常工作。别忘了!仅在应用程序配置中设置spring.profiles.active=native并不适合我
n5:http查询示例
给出app-admin.yml
给app admin develope.yml你是否尝试使用
spring.profiles.active=native
(应该与1.0.0.M3或快照一起使用)?是的,我尝试过添加它,但运气不好。在将spring.profiles.active=native添加到application.properties文件后,它对我有效。这真的很酷。它工作得很好!非常感谢。我的下一个问题是:要获得一个特定的值,您可以在application.yml或一些类似这样的属性文件中指定它:foo=bar
,然后使用一些curl命令获得foo
的值吗?比如curlhttp://localhost:8080/myapp/foo
?目前无法通过http api获取单个值。如果您正在spring应用程序环境中使用spring云配置客户端
。这些值可以通过YAML或Java属性文档获得http://localhost:8080/myapp-default.yml
和http://localhost:8080/myapp-分别为default.properties
。目前,使用Spring Boot 2.x,您将无法在响应中获得application.properties。根据Spring Cloud Config的文档:“searchLocations的默认值与本地Spring引导应用程序相同(…),这不会将服务器上的application.properties公开给所有客户端,因为服务器中存在的任何属性源在发送给客户端之前都会被删除。”您找到解决方案了吗?您可以将spring.profiles.active=native放在bootstrap.properties和put中
#spring.cloud.config.server.native.searchLocations=file:///C:/tmp/config
spring.cloud.config.server.native.searchLocations=classpath:/config
spring:
cloud:
config:
server:
native:
search-locations : file:///C:/tmp/config
#search-locations : classpath:/config
c:/temp/config/
app-admin.yml
app-admin-develop.yml
....
Other sources/src/main/resources/config/app-admin.yml