Spring 除非文件名为应用程序,否则云配置无法加载本机配置文件
Spring cloud config cannot load native config file unless the filename is application
我发生了一些奇怪的事情。我不知道如何解决 spring 云配置无法加载本机或云配置文件,除非它的文件名是 'application.yml/application.properties'.
下面的代码是我的配置:
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>micro-certification-config-center</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置服务器:
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
application.yml:
server:
port: 8000
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
共享文件夹结构:
资源
-- 共享
-- -- 应用-dev.yml
-- -- 短信-dev.yml
它看起来不错 运行 没有错误,但是当我访问 http://127.0.0.1:8000/configCenter/dev/master 时,它只显示应用程序 属性 源。
响应:
{
"name": "configCenter",
"profiles": [
"dev"
],
"label": "master",
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/shared/application-dev.yml",
"source": {
"logging.level.org.springframework.security": "INFO",
"eureka.instance.prefer-ip-address": true,
"eureka.client.serviceUrl.defaultZone": "http://registry:8761/eureka/",
"security.oauth2.resource.user-info-uri": "http://auth-service:5000/uaa/users/current",
"spring.rabbitmq.host": "rabbitmq"
}
}
]
}
服务器控制台:
2017-08-24 22:34:08.055 INFO 30010 --- [nio-8000-exec-4] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4a931797: startup date [Thu Aug 24 22:34:08 CST 2017]; root of context hierarchy
2017-08-24 22:34:08.062 INFO 30010 --- [nio-8000-exec-4] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-08-24 22:34:08.075 INFO 30010 --- [nio-8000-exec-4] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: classpath:/shared/application-dev.yml
2017-08-24 22:34:08.075 INFO 30010 --- [nio-8000-exec-4] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4a931797: startup date [Thu Aug 24 22:34:08 CST 2017]; root of context hierarchy
以上显示 'application.yml' 文件仅有效。
有人可以帮助我吗?非常感谢!
外部配置可以帮助您。详情在这里:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
您可以按照指南中的命令将此默认应用程序配置更改为外部配置:
If you don’t like application.properties as the configuration file
name you can switch to another by specifying a spring.config.name
environment property. You can also refer to an explicit location using
the spring.config.location environment property (comma-separated list
of directory locations, or file paths).
$ java -jar myproject.jar --spring.config.name=myproject or
$ java -jar myproject.jar
--spring.config.location=classpath:/default.properties,classpath:/override.properties
或者在你的代码中你可以这样写:
new SpringApplicationBuilder(Application.class)
.properties("spring.config.name:YOUR_EXTERNAL_CONFIG_FILE")
.build()
.run(args);
}
希望对您有所帮助。
正如@spencergibb 所说,如果我想加载 sms-dev.yml,我应该将 application.name 重命名为 sms.
我发生了一些奇怪的事情。我不知道如何解决 spring 云配置无法加载本机或云配置文件,除非它的文件名是 'application.yml/application.properties'.
下面的代码是我的配置:
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>micro-certification-config-center</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置服务器:
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
application.yml:
server:
port: 8000
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
共享文件夹结构:
资源
-- 共享
-- -- 应用-dev.yml
-- -- 短信-dev.yml
它看起来不错 运行 没有错误,但是当我访问 http://127.0.0.1:8000/configCenter/dev/master 时,它只显示应用程序 属性 源。
响应:
{
"name": "configCenter",
"profiles": [
"dev"
],
"label": "master",
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/shared/application-dev.yml",
"source": {
"logging.level.org.springframework.security": "INFO",
"eureka.instance.prefer-ip-address": true,
"eureka.client.serviceUrl.defaultZone": "http://registry:8761/eureka/",
"security.oauth2.resource.user-info-uri": "http://auth-service:5000/uaa/users/current",
"spring.rabbitmq.host": "rabbitmq"
}
}
]
}
服务器控制台:
2017-08-24 22:34:08.055 INFO 30010 --- [nio-8000-exec-4] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4a931797: startup date [Thu Aug 24 22:34:08 CST 2017]; root of context hierarchy
2017-08-24 22:34:08.062 INFO 30010 --- [nio-8000-exec-4] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-08-24 22:34:08.075 INFO 30010 --- [nio-8000-exec-4] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: classpath:/shared/application-dev.yml
2017-08-24 22:34:08.075 INFO 30010 --- [nio-8000-exec-4] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4a931797: startup date [Thu Aug 24 22:34:08 CST 2017]; root of context hierarchy
以上显示 'application.yml' 文件仅有效。
有人可以帮助我吗?非常感谢!
外部配置可以帮助您。详情在这里:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
您可以按照指南中的命令将此默认应用程序配置更改为外部配置:
If you don’t like application.properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).
$ java -jar myproject.jar --spring.config.name=myproject or
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
或者在你的代码中你可以这样写:
new SpringApplicationBuilder(Application.class)
.properties("spring.config.name:YOUR_EXTERNAL_CONFIG_FILE")
.build()
.run(args);
}
希望对您有所帮助。
正如@spencergibb 所说,如果我想加载 sms-dev.yml,我应该将 application.name 重命名为 sms.