繁体   English   中英

由于云 sql 实例配置,部署到云运行的 spring 启动应用无法运行

[英]A spring boot app deployed to cloud run won't run due to cloud sql instance configuration

我有一个用Springboot写的小应用,目的是学习Cloud SQL和Cloud run。 我遵循了连接到云运行指南 我设置了一个云 SQL 实例,使用私有 IP 地址配置它,并创建了一个无服务器 VPC 访问连接器,我在我的云运行实例中使用了它。 在我的应用程序属性文件中,我添加了带有必要字段的JDBC URL路径,但是当我提交构建时,应用程序中断了。 我做错了什么?

日志并没有说明太多,但这是错误消息的快照

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]

我的 application.properties 文件

spring.datasource.url=jdbc:mysql:///<db-name>?\
cloudSqlInstance=<connection name>&\
socketFactory=com.google.cloud.sql.mysql.SocketFactory&\
user=<username>&\
password=<password>&\
ipTypes=PRIVATE

我的 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 https://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.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tbm5k</groupId>
    <artifactId>cloud-sql-connection-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-sql-connection-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.google.cloud.sql</groupId>
            <artifactId>mysql-socket-factory-connector-j-8</artifactId>
            <version>1.4.4</version>
        </dependency>

        <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>com.h2database</groupId>-->
<!--            <artifactId>h2</artifactId>-->
<!--            <scope>runtime</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

我的cloudbuild.yaml文件

steps:
  # Build the container image
  - name: 'gcr.io/cloud-builders/docker'
    args: ['build', '-t', 'gcr.io/project-id/image-name', '.']

  # Push the container image to Container Registry
  - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/project-id/image-name']

  # Deploy container image to Cloud Run
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args: ['run', 'deploy', 'database-connection-test', '--image', 'gcr.io/project-id/image-name', '--region', 'europe-west4', '--vpc-connector', 'vpc-connector']

images:
  - gcr.io/project-id/image-name

在 Cloud Run 实例上,仅当您在服务配置中配置数据库连接时,才会打开 Cloud SQL 套接字

在您的 Cloud Run 部署中添加这些参数

'--add-cloudsql-instances', '<INSTANCE_CONNECTION_NAME>'

此外,根据文档,您无法使用私有 IP 访问 Cloud SQL 实例,只能通过公共 IP; 因此您不必使用 VPC 连接器。

或者,使用 VPC 连接器并直接使用私有数据 IP,而无需 sockets 和 Cloud Run 数据库连接。 (也记录在案)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM