Spring Profile值变得混杂

时间:2017-01-14 02:22:48

标签: unix gradle spring-boot deployment spring-profiles

我使用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

当我运行我的战争时,只有一个配置文件正常工作,但是一旦我添加了另一个配置文件,我就会让值混淆,导致如下错误:

    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$$290c5e2d] 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;
}
}

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

我错过了什么?感谢。

3 个答案:

答案 0 :(得分:1)

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

使用bootstrap.yml

更好地Boostrap您的应用程序

并且有两个application.yml如下:

application-dev.yml
application-local.yml

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

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

答案 1 :(得分:0)

与@Barath一样,您无法在单个application.yml文件中分隔配置文件属性。为每个配置文件创建一个yml文件,命名如下

application-{profile}.yml

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

答案 2 :(得分:0)

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

您可以拥有以下文件:

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

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

spring:
    profiles:
       active : dev 

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