简体   繁体   English

Google Cloud App Engine中的独立Java应用程序已连接到Google Cloud SQL数据库

[英]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 申请详情

  • Spring Boot based Java application is git cloned in to a folder on the Google App Engine Standard Environment. 基于Spring Boot的Java应用程序被git克隆到Google App Engine标准环境中的文件夹中。
  • Google Cloud SQL 2nd generation instance created 创建了Google Cloud SQL第二代实例

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 问题

  1. Is Google App Engine only for Web based applications? Google App Engine是否仅适用于基于Web的应用程序?
  2. Is the Google App Engine Flexible environment suitable for non-web console based Java applications? Google App Engine灵活环境是否适合非基于Web控制台的Java应用程序?
  3. Or should I be using the Google Compute Engine VM platform to run such non-web console applications and similar to running on my local computer? 还是我应该使用Google Compute Engine VM平台来运行此类非Web控制台应用程序,并且类似于在本地计算机上运行的应用程序?
  4. Is there a Maven dependency that can be added for com.mysql.jdbc.GoogleDriver? 是否可以为com.mysql.jdbc.GoogleDriver添加Maven依赖项?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM