使用application.yml在目录外启动时,未获取SpringBoot外部配置



我有一个服务需要在服务器上作为jar运行。因为jar的组件和依赖项使用spring&spring-boot,这个jar包含spring,并且必须使用application.yml文件进行配置。我正在使用ansible来部署系统,但我无法使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进入机械并运行/启动.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所在的目录不同的目录)工作很重要,因为我坚信让它在那里工作,将允许使用ansible和正确的配置启动jar

渐变文件:

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'
)
}
}

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

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

我发现有效的解决方法是将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 &

最新更新