我正在尝试使用 spring cloud gcp 在 Google App Engine 上部署 spring boot 应用程序。
在本地运行 Mysql 时形成 JDBL Url,成功连接到 Google Cloud Platform 中的 Cloud Mysql 数据库。当应用程序被部署(appengine:deploy)时,当api被命中时,它会尝试连接到mysql,形成的url是我的jdbc驱动程序不可接受的。引发以下异常
java.lang.RuntimeException:驱动程序 com.mysql.cj.jdbc.Driver 声称在 org.eclipse.jetty 不接受 jdbcUrl、jdbc:google:mysql://pocketpharmaapi123:asia-southeast1:pocketpharmapi-db-instance/pocketpharmadb。 annotations.ServletContainerInitializersStarter.doStart (ServletContainerInitializersStarter.java:68) 在 org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68) 在 org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java: 330) 在 org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1406) 在 com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.startWebapp (AppEngineWebAppContext.java:175)
应用部署时如何成功连接数据库?
我试图在 application.properties 中对 jdbcUrl 进行硬编码。它给出了代理例外。
spring.datasource.url=jdbc:mysql://google/pocketpharmadb?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance=pocketpharmaapi123:asia-southeast1:pocketpharmapi-db-instance&useSSL=false
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.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.pocketpharma.application</groupId>
<artifactId>PocketPharmaApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PocketPharmaApi</name>
<description>Api End Points for The Pocket Pharma</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.82.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
</plugin>
</plugins>
</build>
</project>
应用程序属性
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.database=MYSQL
spring.cloud.gcp.sql.database-name=pocketpharmadb
spring.cloud.gcp.sql.instance-connection-name=pocketpharmaapi123:asia-southeast1:pocketpharmapi-db-instance
#spring.datasource.url=jdbc:mysql://google/pocketpharmadb?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance=pocketpharmaapi123:asia-southeast1:pocketpharmapi-db-instance&useSSL=false
#spring.datasource.url=jdbc:mysql://google/pocketpharmadb?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance=pocketpharmaapi123:asia-southeast1:pocketpharmapi-db-instance&useSSL=false
#spring.datasource.url=jdbc:mysql://pocketpharmaapi123:asia-southeast1:pocketpharmapi-db-instance/pocketpharmadb
#spring.datasource.url=jdbc:mysql://localhost/pocketpharmadb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin123
#spring.datasource.testWhileIdle = true
#spring.datasource.validationQuery = SELECT 1
#spring.jpa.show-sql = true
#spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect