【问题标题】:Standalone Java application in Google Cloud App Engine connecting to Google Cloud SQL databaseGoogle Cloud App Engine 中的独立 Java 应用程序连接到 Google Cloud SQL 数据库
【发布时间】:2018-01-11 09:48:06
【问题描述】:

目标:在云中运行独立的 Java 和数据库应用程序以获得性能、可扩展性和高可用性

申请详情

  • 基于 Spring Boot 的 Java 应用程序被 git 克隆到一个文件夹中 Google App Engine 标准环境。
  • 已创建 Google Cloud SQL 第二代实例

注意: Cloud Shell 与 Cloud SQL 位于同一 Google Cloud 项目中

Google Cloud Shell

user@${PROJECT_ID}:~$ cd git

user@${PROJECT_ID}:~$ cd myapplication

使用 Maven 运行应用程序抛出异常,无法连接到 Cloud SQL 数据库

user@${PROJECT_ID}:~$ mvn exec:java

使用不同的 JDBC 连接字符串和 JDBC 驱动程序没有帮助。

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 错误 381 --- [MyApplication.main()] o.a.tomcat.jdbc.pool.ConnectionPool:无法创建初始 池的连接。 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: 无法创建与数据库服务器的连接。尝试重新连接 3 次。放弃。

2017-08-03 12:16:23.525 错误 1048 --- [MyApplication.main()] o.a.tomcat.jdbc.pool.ConnectionPool:无法创建初始 池的连接。 java.sql.SQLException:无法加载类: com.mysql.jdbc.GoogleDriver 来自 ClassLoader:java.net.URLClassLoader@13b428de;ClassLoader:java.net.URLClassLoader@13b428de

即使添加 src/webapp/WEB-INF/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>

问题

  1. Google App Engine 是否仅适用于基于 Web 的应用程序?
  2. Google App Engine Flexible 环境是否适合基于非 Web 控制台的 Java 应用程序?
  3. 或者我应该使用 Google Compute Engine VM 平台来运行此类非 Web 控制台应用程序,并且类似于在我的本地计算机上运行?
  4. 是否可以为 com.mysql.jdbc.GoogleDriver 添加 Maven 依赖项?

【问题讨论】:

  • 您的 CloudSQL 实例和 App 在同一个 GCP 项目中吗?
  • application.properties 文件是否粘贴在已部署的文件之上?还是只是作为示例粘贴的代表性示例?

标签: java spring maven google-app-engine google-cloud-platform


【解决方案1】:

您尝试做的事情在 App Engine 上应该是绝对可行的。 您确实可以为 Google 驱动程序拉取 Maven 依赖项:

    <dependency>
        <groupId>com.google.cloud.sql</groupId>
        <artifactId>mysql-socket-factory</artifactId>
    </dependency>

另外,由于您使用的是 Spring Boot,我建议您尝试一下Spring Cloud GCP。有一个简单的Cloud SQL sample 可以帮助您快速入门。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多