[英]Standalone Java application in Google Cloud App Engine connecting to Google Cloud SQL database
Goal: Run a standalone Java & Database application in the cloud for performance, scalability and high availability 目标:在云中运行独立的Java和数据库应用程序以提高性能,可伸缩性和高可用性
Application Details 申请详情
Note: The Cloud Shell is in the same Google Cloud project as the Cloud SQL 注意: Cloud Shell与Cloud SQL位于同一Google Cloud项目中
Google Cloud Shell Google Cloud Shell
user@${PROJECT_ID}:~$ cd git
user@${PROJECT_ID}:~$ cd myapplication
Running application using Maven throws exception not being able to connect to the Cloud SQL database 使用Maven运行应用程序会引发无法连接到Cloud SQL数据库的异常
user@${PROJECT_ID}:~$ mvn exec:java
Using different JDBC connection strings and JDBC drivers did not help. 使用不同的JDBC连接字符串和JDBC驱动程序无济于事。
application.properties application.properties
# Use MySQL database
#etl.datasource.url=jdbc:mysql://${IP:Instance_name}/${database}?autoReconnect=true&useSSL=false
#etl.datasource.url=jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}
etl.datasource.url=jdbc:mysql://google/${database}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false
#etl.datasource.driver-class-name=com.mysql.jdbc.Driver
etl.datasource.driver-class-name=com.mysql.jdbc.GoogleDriver
etl.datasource.username=${user}
etl.datasource.password=${password}
2017-08-03 10:40:59.477 ERROR 381 --- [MyApplication.main()] oatomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
2017-08-03 10:40:59.477错误381 --- [MyApplication.main()] oatomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接。 Attempted reconnect 3 times.
尝试重新连接3次。 Giving up.
放弃。
2017-08-03 12:16:23.525 ERROR 1048 --- [MyApplication.main()] oatomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
2017-08-03 12:16:23.525错误1048 --- [MyApplication.main()] oatomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。 java.sql.SQLException: Unable to load class: com.mysql.jdbc.GoogleDriver from ClassLoader:java.net.URLClassLoader@13b428de;ClassLoader:java.net.URLClassLoader@13b428de
java.sql.SQLException:无法从ClassLoader:java.net.URLClassLoader@13b428de;加载Class:com.mysql.jdbc.GoogleDriver; ClassLoader:java.net.URLClassLoader@13b428de
Even adding a src/webapp/WEB-INF/appengine-web.xml did not help. 即使添加src / webapp / WEB-INF / appengine-web.xml也无济于事。
appengine-web.xml AppEngine上-web.xml中
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<use-google-connector-j>true</use-google-connector-j>
</appengine-web-app>
Questions 问题
What you're trying to do should be absolutely possible on App Engine. 您尝试做的事情绝对应该可以在App Engine上实现。 There is indeed a Maven dependency you can pull for the Google Driver:
实际上,您可以为Google驱动程序获取Maven依赖项:
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
</dependency>
Also, since you're using Spring Boot, I would suggest you give Spring Cloud GCP a try. 另外,由于您使用的是Spring Boot,因此建议您尝试一下Spring Cloud GCP 。 There is a simple Cloud SQL sample there that can get you started quickly.
那里有一个简单的Cloud SQL示例 ,可以帮助您快速入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.