Spring 配置文件值混合

Spring Profile values getting mixed

我正在使用 Spring Boot 1.4.3.RELEASE 和 gradle 2.13 来开发 api。在本地环境中,“local”配置文件运行良好,但在 Linux/Unix 服务器中部署代码时遇到问题。

这是我的 application.yml,有两个配置文件:dev 和 local

---

spring:
  profiles:
    active: dev

server:
  context-path: /api/v1
  port: 8080

build:
  version: 1.0.0

cache:
  storage:
    path: '/home/user/content/storage/'


---

spring:
  profiles:
    active: local

server:
  context-path: /content-delivery/api/v1
  port: 8081

build:
  version: 1.0.0

cache:
  storage:
    path: '/Yogen/api/code/cachedData'

我用来部署 war 文件的命令是:

jdk1.8.0_112/bin/java -jar _-Dspring.profiles.active=dev content-delivery.jar

当我 运行 我的 war 它只使用一个配置文件时工作正常,但是一旦我添加了另一个配置文件,我得到的值混淆导致错误如下:

    01:56:16.557 INFO  ContentDeliveryApplication - The following profiles are active: dev
............
02:00:48.182 INFO  PropertyPlaceholderConfigurer - Loading properties file from file [/home/542596/content-api/resources/app-dev.properties]
    02:00:51.939 INFO  PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [class org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$0c5e2d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    02:00:55.736 INFO  AnnotationActionEndpointMapping - Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
    02:01:25.559 INFO  PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a08e9c2b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    02:01:56.522 INFO  TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8081 (http)
    02:01:57.669 INFO  StandardService - Starting service Tomcat
    02:01:57.877 INFO  StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.6
    02:02:11.228 INFO  [/content-delivery/api/v1] - Initializing Spring embedded WebApplicationContext
    02:02:11.228 INFO  ContextLoader - Root WebApplicationContext: initialization completed in 353263 ms
    02:02:19.412 INFO  ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
    02:02:19.517 INFO  ServletRegistrationBean - Mapping servlet: 'messageDispatcherServlet' to [/services/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'metricsFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
    02:02:46.283 ERROR EhcacheManager - Initialize failed.
    02:02:46.284 WARN  AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonObjectCacheManager': Invocation of init method failed; nested exception is org.ehcache.StateTransitionException: Directory couldn't be created: /Yogen/api/code/cachedData/cachedData
    02:02:46.285 INFO  StandardService - Stopping service Tomcat
    02:02:47.528 INFO  AutoConfigurationReportLoggingInitializer -

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    02:02:48.103 ERROR SpringApplication - Application startup failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonObjectCacheManager': Invocation of init method failed; nested exception is org.ehcache.StateTransitionException: Directory couldn't be created: /Yogen/api/code/cachedData/cachedData
            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

PropertyPlaceholderConfigurer 已配置如下:

@Configuration
public class PropertyConfiguration {

    @Bean
    @Profile("dev")
    public static PropertyPlaceholderConfigurer developmentPropertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        configurer.setLocation(new FileSystemResource("/home/542596/content-api/resources/app-dev.properties"));
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
}
}

控制台显示正在读取的属性文件是正确的,配置文件也是 'dev',但是端口已启动,上下文路径和其他值是从“local”配置文件中获取的,而不是“ dev'个人资料。

我错过了什么??谢谢。

您的个人资料正在被覆盖。

使用 bootstrap.yml

更好地引导您的应用程序

并且有两个application.yml如下:

application-dev.yml
application-local.yml

在 bootstrap.yml 中设置配置文件以决定加载哪个 属性 文件:

    spring:
         profiles:
             active : dev # choose which application properties to load dev/local

如@Barath 所述,您不能在单个 application.yml 文件中分隔配置文件属性。为每个配置文件创建一个yml文件,命名如下

application-{profile}.yml

我不会使用 bootstrap.yml 来指定要加载的配置文件,因为如果可能的话,您会希望将其外部化,因此最好通过 -Dspring.profiles.active=dev 将其传递给 VM。

我们还有另一个类似于@Maxwell 答案的选项,而不是通过 VM 属性 传递配置文件:

您可以拥有以下文件:

application.yml 
application-dev.yml
application-local.yml

并在 application.yml 中定义配置文件:

spring:
    profiles:
       active : dev 

这确保了 application.yml 和 application-dev.yml 的加载。