在google云中将spring-boot应用程序连接到mysql数据库

rryofs0p  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(459)

我正在尝试将一个使用jdbc和mysql数据库的springbootmaven应用程序部署到一个具有第二代db的灵活googleappengine项目中。当app和db都在本地运行时(spring-boot:run & xampp)。当应用程序在本地运行并且数据库在googlecloudsql上运行时,所有的功能仍然正常。但当这两个实体都部署到google云上时,我的应用程序在尝试访问数据库时会出现“连接链接故障”。应用程序部署良好,并提供硬编码响应。应用程序和数据库位于同一个google云项目中,cloudsql bucket被设置为允许访问所有项目文件。不过,我也尝试过将它们分成不同的项目,并在db的列表中授权应用程序的项目。没有变化。它的行为与我试图从未经授权的ip地址访问数据库时完全一样。
我已经按照指示向app.yaml文件添加元素。

runtime: java
env: flex

runtime_config:  # Optional
jdk: openjdk8

handlers:
   - url: /.*
   script: this field is required, but ignored

# I've tried implementing env_variables and beta setting

# both separately and in tandem.

env_variables:
   MYSQL_DSN: mysql:unix_socket=/cloudsql/jdbc:mysql://12.345.67.890;dbname=exam
   MYSQL_USER: username
   MYSQL_PASSWORD: password

beta_settings:
   cloud_sql_instances: mytestproject1-123456:europe-west1:mydb  

manual_scaling:
   instances: 1

我已经按照指示向application.properties文件添加元素。

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://12.345.67.890/exam
spring.datasource.username=username
spring.datasource.password=password

# I've also tried this url formulation:

# spring.datasource.url=jdbc:mysql://google/exam?cloudSqlInstance=mytestproject1-123456:europe-west1:mydb&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=username&password=password&useSSL=false

我按照指示向pom.xml文件中添加元素。

http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.my</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myproject</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud-gcp.version>1.0.0.M3</spring-cloud-gcp.version>
    <spring-cloud.version>Finchley.M9</spring-cloud.version>
    <appengine.maven.plugin>1.3.2</appengine.maven.plugin>

    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <INSTANCE_CONNECTION_NAME>mytestproject1-123456:europe-west1:mydb</INSTANCE_CONNECTION_NAME>
    <user>username</user>
    <password>password</password>
    <database>exam</database>
    <sqlURL>jdbc:mysql://google/${database}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=${user}&amp;password=${password}&amp;useSSL=false</sqlURL>

</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.google.cloud.sql</groupId>
        <artifactId>mysql-socket-factory-connector-j-6</artifactId>
        <version>1.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-dependencies</artifactId>
            <version>${spring-cloud-gcp.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.9.63</version>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>${appengine.maven.plugin}</version>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

再一次,这一切在本地部署中运行良好,而云mysql bucket将其数据提供给本地部署的应用程序。我的问题是,当部署到googleappengine时,如何让spring引导应用程序连接到云mysql示例?

o2gm4chl

o2gm4chl1#

您可能有兴趣尝试针对cloudsqlmysql的新spring云gcp集成。它会自动为您选择jdbc url。你应该能够让你的应用程序以比你现在更少的配置运行。

相关问题