【问题标题】:Connecting Spring Boot app to MySQL db in Google Cloud将 Spring Boot 应用程序连接到 Google Cloud 中的 MySQL 数据库
【发布时间】:2018-10-12 11:40:27
【问题描述】:

我正在尝试将使用 jdbc 和 MySQL 数据库的 Spring Boot Maven 应用程序部署到具有第二代数据库的灵活 Google App Engine 项目。 当 app 和 db 都在本地运行时,所有功能都符合预期(spring-boot:run & XAMPP)。 当应用程序在本地运行并且 db 在 Google CloudSQL 上时,所有功能仍然正常。 但是,当这两个实体都部署到 Google Cloud 时,我的应用在尝试访问数据库时会出现“连接链接失败”。 该应用程序部署良好并提供硬编码响应。 app 和 db 位于同一个 Google Cloud Project 中,并且 CloudSQL 存储桶设置为允许所有项目文件访问。 但是,我也尝试将它们分成单独的项目并在数据库列表中授权应用程序的项目。不用找了。 它的行为与我尝试从未经授权的 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>

再一次,这一切在本地部署中都可以正常工作,并且 Cloud MySQL 存储桶正在将其数据提供给本地部署的应用程序。 我的问题是,如何让 Spring Boot 应用程序在部署到 Google App Engine 时连接到 Cloud MySQL 实例?

【问题讨论】:

  • application.proprieties 文件在哪个目录下?看看这个来源[medium.com/@DazWilkin/…]和这个类似的案例[stackoverflow.com/questions/48848198/…]它应该在src/main/resources上。
  • 我的文件结构已经按照您的建议,但您的链接向我展示了一些新的配方可供尝试。谢谢,如果他们中的任何人修复它,我会告诉你。

标签: mysql maven spring-boot google-cloud-sql app-engine-flexible


【解决方案1】:

您可能有兴趣尝试新的Spring Cloud GCP integration for Cloud SQL MySQL。 它会自动为您选择 JDBC URL。您应该能够以比当前更少的配置运行您的应用程序。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2019-10-19
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
  • 2017-02-01
相关资源
最近更新 更多