【问题标题】:SpringBoot external config not picked up when started outside directory with application.yml使用 application.yml 在目录外部启动时未拾取 Spring Boot 外部配置
【发布时间】:2017-10-25 14:47:01
【问题描述】:

我有一项服务需要在服务器上作为 jar 运行。因为 jar 的组件和依赖项使用 spring & spring boot,所以这个 jar 包含 spring,并且必须使用 application.yml 文件进行配置。我正在使用 ansible 部署系统,但我无法成功启动服务,因为在启动时,服务没有获取 application.yml 文件。经过一些调试,我发现如果我在与 application.yml 不同的目录中启动服务,它也会失败。

有效的设置:

  1. 使用 springBoot 任务通过 gradle 构建 tar。 ./gradlew build
  2. 将生成的 tar 复制到远程服务器并解压缩。让我们调用解压缩目录deploy_dir
  3. 将生产应用程序.yml 复制到 deploy_dir
  4. 将以下脚本 (start.sh) 复制到 deploy_dir:

    #!/bin/bash nohup /home/ubuntu/deploy_dir/bin/project -Dspring.config.location=/home/ubuntu/deploy_dir/application.yml 1>/home/ubuntu/deploy_dir/out.log 2>&1 &

  5. ssh 进入机器并运行 ./start.sh

这些步骤有效,但是如果我将 start.sh 复制到主目录并从那里运行它,则 application.yml 不会被拾取,并且会出现以下错误。

2017-10-25 14:24:58.730 INFO 31582 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]] 2017-10-25 14:24:59.599 INFO 31582 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$c78541e8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

让 start.sh 从主目录(或不同于 application.yml 所在的目录)工作很重要,因为我坚信让它在那里工作,将允许 jar使用正确的配置从 ansible 开始。

Gradle 文件:

apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')

compile 'org.codehaus.groovy:groovy-all:2.4.0'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile('com.github.groovy-wslite:groovy-wslite:1.1.2')
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.0'
compile group: 'com.google.code.gson', name: 'gson', version:'2.8.0'

runtime('org.postgresql:postgresql')
runtime('com.h2database:h2')

}

jar {
baseName = 'project'
version =  '0.0.1'
manifest {
    attributes(
            'Class-Path': configurations.compile,
            'Main-Class': 'com.MainClass'
    )
}
}

【问题讨论】:

    标签: java spring spring-boot ansible


    【解决方案1】:

    请尝试将您的配置位置设置为目录。还要在前面加上“file:”前缀。

    -Dspring.config.location=file:/home/ubuntu/deploy_dir/
    

    【讨论】:

      【解决方案2】:

      我发现的解决方法是在 start.sh 中添加一行到 cd 到脚本工作的目录。这适用于 ansible 并在服务器上手动运行 start.sh 脚本。我真的不知道为什么会这样,而且看起来很可疑,但它确实有效。

      start.sh

      #!/bin/bash
      cd /home/ubuntu/deploy_dir
      nohup /home/ubuntu/deploy_dir/bin/project -Dspring.config.location=/home/ubuntu/deploy_dir/application.yml 1>/home/ubuntu/deploy_dir/out.log 2>&1 &
      

      【讨论】:

        猜你喜欢
        • 2017-11-26
        • 2019-08-17
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 2018-12-13
        • 2021-01-05
        相关资源
        最近更新 更多