Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot外部配置在使用application.yml在目录外启动时未拾取_Java_Spring_Spring Boot_Ansible - Fatal编程技术网

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

Java SpringBoot外部配置在使用application.yml在目录外启动时未拾取,java,spring,spring-boot,ansible,Java,Spring,Spring Boot,Ansible,我有一个需要在服务器上作为jar运行的服务。因为jar的组件和依赖项使用spring&spring引导,所以这个jar包括spring,并且必须配置application.yml文件。我正在使用ansible部署系统,但我无法让ansible成功启动服务,因为在启动时,服务没有拾取application.yml文件。经过一些调试,我发现如果我在application.yml之外的另一个目录中启动服务,那么它也会失败 有效的设置: 使用springBoot任务用gradle构建tar/gradle

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

有效的设置:

  • 使用springBoot任务用gradle构建tar<代码>/gradlew构建
  • 将生成的tar复制到远程服务器并解压缩。让我们调用解压缩目录
    deploy\u dir
  • 将生产应用程序.yml复制到部署目录
  • 复制以下脚本(start.sh)以部署\u 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&

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

    
    2017-10-25 14:24:58.730信息31582---[main]o.s.b.f.s.DefaultListableBeanFactory:用不同的定义覆盖bean“dataSource”的bean定义:替换[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=(推断);在类路径资源中定义[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]]和[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=(推断);在类路径资源中定义[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]]
    2017-10-25 14:24:59.599信息31582---[main]trationlegate$BeanPostProcessorChecker:Bean'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'类型为[class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerbyspringC78541E8]不符合由所有BeanPostProcessor处理的条件(例如:不符合自动代理的条件)
    

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

    渐变文件:

    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 to 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 &