尝试将Spring Boot应用程序部署到Google Cloud时,必须提供数据库名称错误

时间:2019-04-28 14:03:38

标签: rest spring-boot google-cloud-platform

我正在将Spring Boot Web应用程序部署到Google Cloud。该应用程序在本地运行良好。我能够启动该应用程序,并且一切正常。但是,当我尝试将其部署到Google Cloud时,出现以下错误:

java.lang.IllegalArgumentException: A database name must be provided.

我已按照本教程部署了该应用:https://www.baeldung.com/spring-boot-google-app-engine

有关错误的更多详细信息:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0;

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Unsatisfied dependency expressed through method 'dataSource' parameter 0;

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cloudSqlDataSourceProperties' defined in class path resource [org/springframework/cloud/gcp/autoconfigure/sql/GcpCloudSqlAutoConfiguration$CloudSqlDataSourcePropertiesConfiguration.class]: Unsatisfied dependency expressed through method 'cloudSqlDataSourceProperties' parameter 3;

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultMySqlJdbcInfoProvider' defined in class path resource [org/springframework/cloud/gcp/autoconfigure/sql/GcpCloudSqlAutoConfiguration$MySqlJdbcInfoProviderConfiguration.class]: Bean instantiation via factory method failed;

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.gcp.autoconfigure.sql.CloudSqlJdbcInfoProvider]: Factory method 'defaultMySqlJdbcInfoProvider' threw exception;

Caused by: java.lang.IllegalArgumentException: A database name must be provided.

app.yaml:

runtime: java
env: flex
runtime_config:
  jdk: openjdk8
env_variables:
  SPRING_PROFILES_ACTIVE: "gcp,mysql"
handlers:
  - url: /.*
    script: this field is required, but ignored
manual_scaling:
  instances: 1

application.properties:

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://35.***.***.**:3306/name_db
spring.datasource.username=root
spring.datasource.password=mypass
spring.datasource.initialization-mode=always
server.port = 8080
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

application-gcp.properties:

spring.cloud.gcp.sql.instance-connection-name=name-project:us- 
central1:instance-name
spring.cloud.gcp.sql.database-name=name_db

spring-cloud-bootstrap.properties:

spring.cloud.appId=name-project

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.activekids</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web</name>
<description>Charity spring backend application</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.7.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>cloud-gcp</id>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-starter</artifactId>
                <version>1.0.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
                <version>1.0.0.RELEASE</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>${project.name}-gcp</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>com.google.cloud.tools</groupId>
                    <artifactId>appengine-maven-plugin</artifactId>
                    <version>1.3.2</version>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

1 个答案:

答案 0 :(得分:0)

我通过将application-gcp.properties中的所有内容移到application.properties中来解决了这个问题。 像这样:

(application.properties)

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://35.***.***.**:3306/name_db
spring.datasource.username=root
spring.datasource.password=mypass
spring.datasource.initialization-mode=always
server.port = 8080
spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect

#Here added:
spring.cloud.gcp.sql.instance-connection-name=name-project:us- 
central1:instance-name
spring.cloud.gcp.sql.database-name=name_db