【问题标题】:A database name must be provided error when trying to deploy a spring boot app to google cloud尝试将 Spring Boot 应用程序部署到谷歌云时,必须提供数据库名称错误
【发布时间】:2019-09-17 08:24:26
【问题描述】:

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

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

应用程序-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>

【问题讨论】:

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


    【解决方案1】:

    我通过将 application-gcp.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
    

    【讨论】:

    【解决方案2】:

    看起来发生这种情况是因为默认情况下启用了 GCP SQL 并且需要正确的连接。 但是如果你不需要它,例如在本地开发期间 - 只需在 application.properties 中禁用 gsp sql:

    spring.cloud.gcp.sql.enabled=false
    

    在添加 spring-cloud-gcp-starter-secretmanager 工件后出现了对我来说有趣的相同问题。

    【讨论】:

    • 当 postgresql & spring-cloud-gcp-starter 在依赖项中时,spring cloud 会尝试配置数据库。这看起来像一个错误,因为它在处理配置文件之前就这样做了。因此,您应该在默认属性文件中添加上述行,而不是自动配置数据库。
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 2019-05-24
    • 2020-02-18
    • 2021-07-30
    • 2021-12-20
    • 2019-01-11
    • 2017-09-04
    • 2015-12-24
    相关资源
    最近更新 更多