[英]SpringBoot external config not picked up when started outside directory with application.yml
I have a service that needs to run as a jar on a server. 我有一项服务,需要以jar形式在服务器上运行。 Because the components and dependencies to the jar use spring & spring boot, this jar includes spring and has to be configured with an application.yml file.
由于jar的组件和依赖项使用spring&spring boot,因此该jar包含spring,必须使用application.yml文件进行配置。 I am using ansible to deploy the system, but I cannot get ansible to successful start the service because on startup, the service is not picking up the application.yml file.
我正在使用ansible来部署系统,但是我无法成功启动该服务,因为在启动时,该服务未拾取application.yml文件。 After some debugging, I figured out that if I start the service in a different directory than the application.yml, it fails then too.
经过一些调试后,我发现如果在不同于application.yml的目录中启动该服务,它也会失败。
Setup that works: 有效的设置:
./gradlew build
deploy_dir
deploy_dir
Copy the following script (start.sh) to deploy_dir: 将以下脚本(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 &
Those steps work, but if I copy start.sh to the home directory, and run it from there, the application.yml does not get picked up and I get the following errors. 这些步骤有效,但是如果我将start.sh复制到主目录并从该目录运行,则application.yml不会被拾取,并且出现以下错误。
2017-10-25 14:24:58.730 INFO 31582 --- [ main] osbfsDefaultListableBeanFactory : 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)
It is important that I get start.sh working from the home directory (or a directory different from the one application.yml is in) because I strongly believe that getting it to work there, will allow the jar to be started with ansible with the correct config. 从主目录(或不同于一个application.yml的目录)中启动start.sh是很重要的,因为我坚信让它在该目录下运行将使jar与ansible一起启动。正确的配置。
Gradle file: 摇篮文件:
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'
)
}
}
Please try to set your config location as a directory. 请尝试将您的配置位置设置为目录。 Also put the "file:" prefix before.
还要在前面加上“ file:”前缀。
-Dspring.config.location=file:/home/ubuntu/deploy_dir/
The workaround I have found works is to add a line to start.sh to cd to the directory where the script works. 我发现有效的解决方法是将start.sh的一行添加到cd到脚本工作的目录。 This works for ansible and running start.sh script manually on the server.
这适用于ansible并在服务器上手动运行start.sh脚本。 I don't really know why this works and it seems shady, but it is what works.
我真的不知道为什么这行得通,而且似乎很阴暗,但这是行得通的。
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 &
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.