google-app-engine - 部署在 App Engine 上的带有 Hibernate 的 SpringBoot App 无法连接到 Cloud SQL
问题描述
我有一个部署在 App Engine 上的 SpringBoot 应用程序,我的数据库是 MySQL 8。对于我的本地测试,我可以使用公共 IP 连接到 Cloud DB,并且连接工作正常。
但是,当此应用程序部署在 App Engine 上时,我收到错误消息:
2021-11-09 01:44:50 default[1] 2021-11-09 01:44:50.874 INFO 10 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.25.Final
2021-11-09 01:44:51 default[1] 2021-11-09 01:44:51.092 INFO 10 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-09 01:46:58 default[1] 2021-11-09 01:46:58.343 WARN 10 --- [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata
2021-11-09 01:46:58 default[1]
2021-11-09 01:46:58 default[1] com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
2021-11-09 01:46:58 default[1]
2021-11-09 01:46:58 default[1] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2021-11-09 01:46:58 default[1] at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
2021-11-09 01:46:58 default[1] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.26.jar!/:8.0.26]
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection timed out (Connection timed out)
我已按照文档中提到的步骤进行操作: https ://cloud.google.com/sql/docs/mysql/connect-app-engine-standard
应用程序.yaml
runtime: java11
instance_class: F2
entrypoint: java -noverify -jar strategy-0.0.1-SNAPSHOT.war
应用程序属性
#spring.datasource.url=jdbc:mysql://35.345.11.132:3306/fin_strat
spring.datasource.url=jdbc:mysql://localhost:3306/fin_strat
spring.datasource.username=<database-user-name>
spring.datasource.password=<database-password>
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
我不确定从 App Engine 连接时错过了什么。请协助。
解决方案
当您使用 APP ENgine(或 Cloud Functions 或 Cloud Run)中的 Cloud SQL 功能连接您的 Cloud SQL 数据库时,该服务会打开一个套接字来连接数据库。您不必使用公共 IP,而是使用打开的套接字。
它类似于套接字模式下的 Cloud SQL 代理。您在这里有一个 Java 示例的文档
推荐阅读
- angular - ng 生成:发生未处理的异常:“路径”参数必须是字符串类型。收到未定义
- java - java EE如何并行处理DB数据集?
- json - 从 SQL Server 2104 中的 JSON 访问数据
- c# - 切换到另一种形式时,将一种形式的切换按钮保持打开状态
- react-native - 任务 ':app:compileDebugJavaWithJavac' 执行失败。> java.lang.NullPointerException(没有错误信息)
- jenkins - Jenkins 在 Openshift 上的 pod 中运行
- jenkins - Git 插件显示 Jenkinsfile 相关存储库的 git 数据修订
- python - 用于将 1d 数组重塑为 x、y 和 z 方向的 3d 的参数序列
- wordpress - 为什么定制器中不显示菜单?
- ios - 如何在opentok中显示所有加入视频通话的参与者列表