简体   繁体   English

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

[英]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: 有效的设置:

  1. Build the tar with gradle using springBoot task. 使用springBoot任务用gradle构建tar。 ./gradlew build
  2. Copy the resulting tar to the remote server and uncompress. 将生成的tar复制到远程服务器并解压缩。 Lets call the uncompresses directory deploy_dir 让我们调用解压目录deploy_dir
  3. Copy a production application.yml to deploy_dir 将生产application.yml复制到deploy_dir
  4. 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 &

  5. ssh into machince and run ./start.sh ssh进入机密并运行./start.sh

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.

相关问题 使用application.yml配置SpringBoot - SpringBoot Configuration with application.yml 获取应用程序名称Springboot application.yml时获得NullPointerException - Got NullPointerException when getting application name Springboot application.yml docker 环境中的 Spring Boot 外部配置:application.yml 不存在 - Spring boot external config in a docker environment: application.yml does not exist 在启动时如何在Spring Boot中将一些变量注入到application.yml中 - How are some variables injected to application.yml in spring boot when it is started 如何在springboot中从application.yml中读取带有特殊字符的属性 - How to read properties with special characters from application.yml in springboot 无法访问 springboot 组件中的 application.yml 属性 - Not able to access application.yml properties in springboot component Springboot-从application.yml注入取决于方法名称 - Springboot - injection from application.yml depending method name 当应用程序执行时,不会使用application.yml中的spring.cloud.config设置 - Settings in application.yml for spring.cloud.config aren't used when app is executing 使用外部 tomcat 时如何覆盖 application.yml 中的属性? - How to override properties from application.yml when using external tomcat? 从application.yml读取配置到POJO列表列表 - Read config from application.yml to Map of List of POJO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM