没有设置数据源无法启动springboot应用程序

Unable to launch springboot application without setting datasource

我试图启动我的 SpringBoot 应用程序,但未在编译前配置 DataSource。如果我这样做,指示参数,应用程序正常工作,但我想通过 JAVA_OPTS 参数传递它们,当我 运行 a docker容器。像那样:

docker run --network public --link pg --name tm -e JAVA_OPTS="-Ddatasource.jdbcUrl=jdbc:postgresql://pg:5432/ggal1701 -Ddatasource.user=postgres -Ddatasource.password=postgres" -p 9000:8080 worker 

我已经尝试了很多我在 Whosebug 上看到的选项,但是 none 对我有用。

我的build.gradle:

buildscript {
    repositories {
        mavenLocal()
        jcenter()
        maven { url 'http://repo.spring.io/plugins-release' }
        maven { url 'http://repo.spring.io/milestone' }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath group: 'net.ltgt.gradle', name: 'gradle-apt-plugin', version: '0.6'
        classpath group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: spring_boot_version
        classpath group: 'org.springframework.build.gradle', name: 'propdeps-plugin', version: '0.0.7'
    }
}


plugins {
    id "org.sonarqube" version "2.6.2"
}

apply plugin: "org.sonarqube"

apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'maven'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'propdeps'
defaultTasks 'bootRun'

bootRepackage {
    mainClass = 'es.asd.GGAL1701.Application'
}

war {
    rootSpec.exclude("**/tomcat-*.jar")
    exclude("**/client")
    exclude("**/tmp")
    exclude("**/upload")
    from "dist"
}

springBoot {
    mainClass = 'es.asd.GGAL1701.Application'
    executable = true
}

bootRun {
    addResources = false
}

if (project.hasProperty('prod')) {
    apply from: 'gradle/profile_prod.gradle'
} else {
    apply from: 'gradle/profile_dev.gradle'
}

apply from: 'gradle/process_resources.gradle'

group = 'es.asd.GGAL1701'
version = '0.0.1-SNAPSHOT'

description = ''

configurations {
    providedRuntime
}


repositories {
    maven {
        url "http://asd-nexus.com/repository/maven-central/"
        credentials {
            username "asd"
            password "asd./"
        }
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "${nexusUrl}/repository/maven-releases/") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
            snapshotRepository(url: "${nexusUrl}/repository/maven-snapshots") {
                authentication(userName: nexusUsername, password: nexusPassword)
            }
        }
    }
}


repositories {
    mavenLocal()
    maven { url 'http://repo.spring.io/milestone' }
    maven { url 'http://repo.spring.io/snapshot' }
    maven { url 'https://repository.jboss.org/nexus/content/repositories/releases' }
    maven { url 'https://oss.sonatype.org/content/repositories/releases' }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'http://repo.maven.apache.org/maven2' }
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'

    //
    compile group: 'javax.inject', name: 'javax.inject', version: '1'
    compile group: 'javax.el', name: 'javax.el-api', version: '3.0.0'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    compile group: 'javax.transaction', name: 'javax.transaction-api', version: '1.2'
    compile group: 'javax.annotation', name: 'jsr250-api', version: '1.0'

    compile(group: 'com.zaxxer', name: 'HikariCP', version: '2.6.3') {
        exclude(module: 'tools')
    }
    compile group: 'org.postgresql', name: 'postgresql', version: postgresql_version

    compile group: 'org.springframework', name: 'spring-aspects', version: spring_framework_version
    compile group: 'org.springframework.data', name: 'spring-data-jpa', version: spring_framework_data_version
    compile group: 'org.springframework.security', name: 'spring-security-core', version: spring_security_version
    compile group: 'org.springframework.security', name: 'spring-security-config', version: spring_security_version
    compile group: 'org.springframework.security', name: 'spring-security-data', version: spring_security_version
    compile group: 'org.springframework.security', name: 'spring-security-web', version: spring_security_version

    compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: spring_boot_version

    compile group: 'org.hibernate', name: 'hibernate-core', version: hibernate_version
    compile group: 'org.hibernate', name: 'hibernate-java8', version: hibernate_version
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: hibernate_version

    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.7.0'

    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: jackson_version
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jackson_version
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: jackson_version
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: jackson_version
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: jackson_version

    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'

    compile group: 'net.bull.javamelody', name: 'javamelody-core', version: '1.68.0'

    compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.2'

    // Envío de emails
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'

    // Generación de PDFs
    compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.10'
}

compileJava.dependsOn(processResources)

clean {
    delete "target"
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.12'
}

task stage(dependsOn: 'bootRepackage') {
}

我的gradle.properties:

rootProject.name=application
profile=dev
##
## Versiones de las dependencias comunes del proyecto
postgresql_version=9.4-1203-jdbc42
jackson_version=2.7.9
hibernate_version=5.2.10.Final
spring_boot_version=1.4.7.RELEASE
spring_framework_version=4.3.9.RELEASE
spring_framework_data_version=1.10.11.RELEASE
spring_security_version=4.1.4.RELEASE
systemProp.sonar.host.url=https://asd-sonarqube.ga
systemProp.sonar.login=asd
systemProp.sonar.password=asd./
systemProp.sonar.sources=src/main
nexusUrl=http://asd-nexus.ga
nexusUsername=asd
nexusPassword=asd./
#Para solucionar problema de lanzar sonarqube desde jenkins
org.gradle.daemon=false
#systemProp.sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#systemProp.sonar.jdbc.username=sonar
#systemProp.sonar.jdbc.password=sonar

##
## Configuraciones de GRADLE
##
## Below are some of the gradle performance improvement settings that can be used as required, these are not enabled by default
##
## The Gradle daemon aims to improve the startup and execution time of Gradle.
## When set to true the Gradle daemon is to run the build.
## TODO: disable daemon on CI, since builds should be clean and reliable on servers
## un comment the below line to enable the daemon
#org.gradle.daemon=true
## Specifies the JVM arguments used for the daemon process.
## The setting is particularly useful for tweaking memory settings.
## Default value: -Xmx1024m -XX:MaxPermSize=256m
## un comment the below line to override the daemon defaults
#org.gradle.jvmargs=-Xmx1024m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
## When configured, Gradle will run in incubating parallel mode.
## This option should only be used with decoupled projects. More details, visit
## http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
## un comment the below line to enable parellel mode
#org.gradle.parallel=true
## Enables new incubating mode that makes Gradle selective when configuring projects.
## Only relevant projects are configured which results in faster builds for large multi-projects.
## http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
## un comment the below line to enable the selective mode
#org.gradle.configureondemand=true

我的application.yml:

# ===================================================================
# Spring Boot configuration.
#
# This configuracion will be overriden by the Spring profile you use,
# for example application-dev.yml if you use the "dev" profile.
# ===================================================================

locale:
    default: es

server:
    port: 8080

spring:
    jpa:
        database-platform: org.hibernate.dialect.PostgreSQL94Dialect
        database: POSTGRESQL
        openInView: false
        show_sql: false
        generate-ddl: false
        hibernate:
            ddl-auto: none
            naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
        properties:
            hibernate.cache.use_second_level_cache: false
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.id.new_generator_mappings: true
            hibernate.current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
    http:
      multipart:
        max-file-size: 20Mb
        max-request-size: 20Mb
    messages:
      basename: i18n/messages
    thymeleaf:
      mode: XHTML

properties:
    async:
        corePoolSize: 2
        maxPoolSize: 50
        queueCapacity: 10000
    security:
        authentication:
            jwt:
                secret: c2t3f0c5dXF6bV9tOV45X2RpdmlzYQ==
                # Token is valid 24 hours
                tokenValidityInSeconds: 86400
                tokenValidityInSecondsForRememberMe: 2592000

我的申请-prod.yml:

# ===================================================================
# Spring Boot configuration for the "prod" profile.
#
# This configuracion overrides the application.yml file.
# ===================================================================

spring:
    # Configuración de la BASE DE DATOS
    datasource:
        dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
        url: jdbc:postgresql://pg:5432/ggal1701
        username: asd
        password: asd
        maximum-pool-size: 5

    application:
        url: http://localhost:8080
    profiles:
        active: prod
    devtools:
        restart:
            enabled: false
        livereload:
            enabled: false
server:
    compression:
        enabled: true
        mime-types: text/html,text/xml,text/plain,text/css, application/javascript, application/json

正如我在开头提到的,我已经尝试了 Whosebug 中出现的许多替代方案,但是当我没有在我的应用程序中配置 spring.datasource-prod.yml 时,我总是得到一个类似于以下错误:

mrnobody@mrnobody-UX301LAA:~/Escritorio/tfg/appserver$ sudo docker run --network public --link pg --name tm -e JAVA_OPTS="-Ddatasource.jdbcUrl=jdbc:postgresql://pg:5432/ggal1701 -Ddatasource.user=postgres -Ddatasource.password=postgres" -p 9000:8080 worker
02-May-2018 20:56:17.864 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.51
02-May-2018 20:56:17.867 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Apr 9 2018 21:20:15 UTC
02-May-2018 20:56:17.868 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.51.0
02-May-2018 20:56:17.868 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
02-May-2018 20:56:17.868 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.13.0-39-generic
02-May-2018 20:56:17.868 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
02-May-2018 20:56:17.868 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
02-May-2018 20:56:17.869 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_162-8u162-b12-1~deb9u1-b12
02-May-2018 20:56:17.869 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
02-May-2018 20:56:17.869 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
02-May-2018 20:56:17.869 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
02-May-2018 20:56:17.870 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
02-May-2018 20:56:17.870 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
02-May-2018 20:56:17.870 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Ddatasource.jdbcUrl=jdbc:postgresql://pg:5432/ggal1701
02-May-2018 20:56:17.870 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Ddatasource.user=postgres
02-May-2018 20:56:17.871 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Ddatasource.password=postgres
02-May-2018 20:56:17.871 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
02-May-2018 20:56:17.871 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
02-May-2018 20:56:17.871 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
02-May-2018 20:56:17.871 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
02-May-2018 20:56:17.872 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
02-May-2018 20:56:17.872 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
02-May-2018 20:56:17.872 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.16 using APR version 1.5.2.
02-May-2018 20:56:17.872 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
02-May-2018 20:56:17.882 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f  25 May 2017)
02-May-2018 20:56:18.001 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
02-May-2018 20:56:18.010 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
02-May-2018 20:56:18.011 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 614 ms
02-May-2018 20:56:18.040 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
02-May-2018 20:56:18.041 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.51
02-May-2018 20:56:18.066 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/ROOT.war
02-May-2018 20:56:21.589 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
20:56:21.802 [localhost-startStop-1] WARN es.enxenio.GGAL1701.ApplicationWebXml - No Spring profile configured, running with default configuracion
20:56:22.446 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.exception-conversion-word]
name [LOGGING_EXCEPTIONCONVERSIONWORD]. javax.naming.NameNotFoundException: Name [LOGGING_EXCEPTIONCONVERSIONWORD] is not bound in this Context. Unable to find [LOGGING_EXCEPTIONCONVERSIONWORD].
20:56:22.456 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [LOGGING_EXCEPTIONCONVERSIONWORD]
20:56:22.456 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [LOGGING_EXCEPTIONCONVERSIONWORD] threw NamingException with message: Name [LOGGING_EXCEPTIONCONVERSIONWORD] is not bound in this Context. Unable to find [LOGGING_EXCEPTIONCONVERSIONWORD].. Returning null.

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.7.RELEASE)

2018-05-02 20:56:22.956  INFO 1 --- [ost-startStop-1] es.enxenio.GGAL1701.ApplicationWebXml    : Starting ApplicationWebXml on c72e1d8684c7 with PID 1 (/usr/local/tomcat/webapps/ROOT/WEB-INF/classes started by root in /usr/local/tomcat)
2018-05-02 20:56:22.958 DEBUG 1 --- [ost-startStop-1] es.enxenio.GGAL1701.ApplicationWebXml    : Running with Spring Boot v1.4.7.RELEASE, Spring v4.3.9.RELEASE
2018-05-02 20:56:22.959  INFO 1 --- [ost-startStop-1] es.enxenio.GGAL1701.ApplicationWebXml    : The following profiles are active: prod
2018-05-02 20:56:23.096 DEBUG 1 --- [kground-preinit] org.jboss.logging                        : Logging Provider: org.jboss.logging.Slf4jLoggerProvider found via system property
2018-05-02 20:56:25.745 DEBUG 1 --- [ost-startStop-1] net.bull.javamelody                      : spring interceptor initialized
2018-05-02 20:56:25.757 DEBUG 1 --- [ost-startStop-1] net.bull.javamelody                      : spring interceptor initialized
2018-05-02 20:56:25.758 DEBUG 1 --- [ost-startStop-1] net.bull.javamelody                      : spring interceptor initialized
2018-05-02 20:56:27.019  INFO 1 --- [ost-startStop-1] e.enxenio.GGAL1701.config.WebConfigurer  : Web application configuracion, using profiles: [prod]
2018-05-02 20:56:27.020 DEBUG 1 --- [ost-startStop-1] e.enxenio.GGAL1701.config.WebConfigurer  : Registering Caching HTTP Headers Filter
2018-05-02 20:56:27.021  INFO 1 --- [ost-startStop-1] e.enxenio.GGAL1701.config.WebConfigurer  : Web application fully configured
2018-05-02 20:56:27.056  WARN 1 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [es/enxenio/GGAL1701/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2018-05-02 20:56:27.062  WARN 1 --- [ost-startStop-1] o.s.boot.SpringApplication               : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2018-05-02 20:56:27.198 ERROR 1 --- [ost-startStop-1] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method entityManagerFactory in es.enxenio.GGAL1701.config.DatabaseConfiguration required a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' in your configuration.

02-May-2018 20:56:27.199 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:755)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [es/enxenio/GGAL1701/config/DatabaseConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)


02-May-2018 20:56:27.202 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive /usr/local/tomcat/webapps/ROOT.war
 java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)


02-May-2018 20:56:27.204 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/ROOT.war has finished in 9,137 ms
02-May-2018 20:56:27.212 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
02-May-2018 20:56:27.267 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 55 ms
02-May-2018 20:56:27.270 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
02-May-2018 20:56:27.535 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 265 ms
02-May-2018 20:56:27.535 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
02-May-2018 20:56:27.566 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 31 ms
02-May-2018 20:56:27.566 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
02-May-2018 20:56:27.588 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 22 ms
02-May-2018 20:56:27.593 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
02-May-2018 20:56:27.631 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
02-May-2018 20:56:27.651 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 9639 ms

如果您的任务是在容器启动期间将 jdbc url 作为参数传递,则无需通过 DatabaseConfiguration 来完成。在 docker-运行 命令中使用配置中的占位符和环境变量可以相当简单地完成。

在配置中定义占位符:

spring:
  datasource:
    url: ${JDBC_URL}
    username: ${DATABASE_USERNAME}
    password: ${DATABASE_PASSWORD}

和运行这样的容器:

docker run \
    --network public \
    --link pg \
    --name tm \
    -e JDBC_URL="jdbc:postgresql://pg:5432/ggal1701" \
    -e DATABASE_USERNAME="postgres" \
    -e DATABASE_PASSWORD="postgres" \
    -p 9000:8080 worker 

注意:docker link 是已弃用的功能,您可能需要替换它。

Java 属性应根据 Spring Boot Externalized Configuration Docs 工作。可能是因为您没有为 java 属性使用正确的名称?

例如,根据您的生产属性,我看到数据库 URL 的 属性 是 spring.datasource.url:

spring:
    # Configuración de la BASE DE DATOS
    datasource:
        url: jdbc:postgresql://pg:5432/ggal1701

但是你通过以下 Java 属性:

-Ddatasource.jdbcUrl=jdbc:postgresql://pg:5432/ggal1701

我认为你应该使用:

-Dspring.datasource.url=jdbc:postgresql://pg:5432/ggal1701

另一个对我一直有效的建议是使用环境变量而不是 java 属性。您可以用与 Docker:

同样简单的方式来使用它们
-e SPRING_DATASOURCE_URL=jdbc:postgresql://pg:5432/ggal1701

你有两个选择,都正确:

首先是传值JAVA_OPTS如下(你传错参数了):

docker run \
    --network public \
    --link pg \
    --name tm \
    -e JAVA_OPTS= \
        "-Dspring.datasource.url=jdbc:postgresql://pg:5432/ggal1701
         -Dspring.datasource.username=postgres \
         -Dspring.datasource.password=postgres" \
    -p 9000:8080 worker 

第二个是将值作为环境变量传递如下(你的错误是没有引号):

docker run \
    --network public \
    --link pg \
    --name tm \
    -e DATASOURCE_JDBC_URL="jdbc:postgresql://pg:5432/ggal1701" \
    -e DB_USERNAME="postgres" \
    -e DB_PASSWORD="postgres" \
    -p 9000:8080 worker 

请记住在您的配置中指定这些参数以使用第二个选项。在第一个中,您无需直接配置任何内容。

作为 devOps 文化的一部分,我从不建议对设置进行硬编码并选择第二个选项,因为它是为此目的而设计的。他们都必须为你工作,如果你不这样做,你就会犯其他错误。

您所说的 hikariPool 错误只是数据源故障的结果。尝试这两种方式中的一种,在我的专业意见中,选择第二种。

我留给你一个很好的教程:Spring-boot tutorial