无法将Spring引导Mysql连接到GCP云



我在GCP云帐户中创建了SQL实例,如果我在本地运行,它可以正常工作。但在将Spring应用程序部署到GCP之后,它抛出了一个错误。

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:1796)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:595)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0 (AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean (AbstractApplicationContext.java:1108)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization (AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh (ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh (SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext (SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1215)
at inner.stories.StoriesApplication.main (StoriesApplication.java:10)

application.properties:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql: **private-IP-of-sql-instance**
spring.datasource.username=*****
spring.datasource.password=*****
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update

pom.xml:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>

更新

我可以在gcloud命令日志中看到不同的错误。我的页面正在加载一半,然后在gcloud命令日志中显示SocketTimeoutException。

后的主页

Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.

Gcloud命令日志:

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.14.Final.jar!/:5.4.14.Final]
2020-05-04 21:34:35 default[20200504t172700]    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.14.Final.jar!/:5.4.14.Final]
2020-05-04 21:34:35 default[20200504t172700]    at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.4.14.Final.jar!/:5.4.14.Final]
2020-05-04 21:34:35 default[20200504t172700]    ... 43 common frames omitted
2020-05-04 21:34:35 default[20200504t172700]  Caused by: java.net.SocketTimeoutException: connect timed out
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    ... 65 common frames omitted

感谢您的帮助

您的问题可以在属性文件中找到:

spring.datasource.url=jdbc:mysql://google/myBlogcloudSqlInstance=reviewcontainerblog&socketFactory=com.google.cloud.sql.mysql.SocketFactory

您可以查看谷歌的社区教程和谷歌云平台的Github页面上的"创建JDBC URL":

spring.datasource.url=jdbc:mysql://google/petclinic?cloudSqlInstance=INSTANCE_CONNECTION_NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory
jdbc:mysql://google/<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>

因此,它是实例连接名称,而不是实例名称。你也可以查看谷歌社区教程中的以下文档,这些文档可能会帮助你快速学习。

您还可以阅读这篇外部文章,其中解释了如何以不同的方式从Spring应用程序连接到CloudSQL。

请告诉我它是否适合你。

根据您的应用程序属性,您正试图通过其Pivate IP访问实例。根据文件:

如果要使用专用IP地址,请参阅配置无服务器VPC访问并使用私有IP直接连接。

请按照文档中描述的步骤设置无服务器VPC访问,然后按照配置私有IP中提到的步骤,通过其私有IP连接到您的实例。

希望能有所帮助。

相关内容

  • 没有找到相关文章

最新更新