我正在尝试将带有MySQL数据库的jdbc的Spring Boot Maven应用程序部署到具有第二代数据库的灵活Google App Engine项目中。 当应用程序和数据库都在本地运行时,所有功能都符合预期(spring-boot:run & XAMPP(。 当应用程序在本地运行并且数据库位于Google CloudSQL上时,所有功能仍然正常运行。 但是,当这两个实体都部署到 Google Cloud 时,我的应用在尝试访问数据库时出现"连接链接失败"。 应用程序部署良好,并提供硬编码响应。应用和数据库位于同一个 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}&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=${user}&password=${password}&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 存储桶将其数据提供给本地部署的应用程序。 我的问题是,当部署到Google App Engine时,如何让Spring Boot应用程序连接到Cloud MySQL实例?
您可能有兴趣尝试用于Cloud SQL MySQL的新Spring Cloud GCP集成。 它会自动为您选择 JDBC URL。你应该能够以比当前更少的配置运行应用。