从Google Cloud Run with Cloud SQL:MMySQL部署时发生通信链接故障错误



点击此链接,在春季启动应用程序中使用Cloud Build和GitHub操作进行连续部署。

petomalina.medium.com/

除了当服务尝试使用以下命令访问数据库进行部署时,一切都很正常。

gcloud run deploy my-service 
--region europe-west1 
--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/my-service 
--platform managed 
--allow-unauthenticated 
--project ${{ secrets.GCP_PROJECT_ID }} 
--add-cloudsql-instances [instance-name] 
--set-env-vars DATASOURCE_URL=jdbc:mysql://google/[dbname]?cloudSqlInstance=[instance-name]&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false

我还为云SQL客户端添加了针对服务帐户的角色,尝试使用公共ip、私有ip和这种格式jdbc:mysql://google/[dbname]?cloudSqlInstance=[instance-name]&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false

这是我的app.prop文件:

spring.datasource.url=${DATASOURCE_URL}
spring.datasource.username=sa
spring.datasource.password=pswd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

但它仍然给出了这个错误:

com.mysql.cj.jdbc.exceptions.CommunicationsException: 
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. 
The driver has not received any packets from the server.
com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
[mysql-connector-java-8.0.22.jar!/:8.0.22] com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
[mysql-connector-java-8.0.22.jar!/:8.0.22] com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
[mysql-connector-java-8.0.22.jar!/:8.0.22] com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
[mysql-connector-java-8.0.22.jar!/:8.0.22] com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
[mysql-connector-java-8.0.22.jar!/:8.0.22] com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) [mysql-connector-java-8.0.22.jar!/:8.0.22]

有人能给我指正确的方向吗?

这些是添加的依赖项:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

我还添加了以下内容:

<dependency>
<!-- MySQL Socket Factory for Cloud SQL -->
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId> 
<version>1.0.3</version>
</dependency>

现在错误变为:

[nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: 
Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: 
Unable to acquire JDBC Connection] with root cause
java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method)
[na:1.8.0_202] at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
[na:1.8.0_202] at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
[na:1.8.0_202] at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
[na:1.8.0_202] at java.net.SocksSocketImpl.connect(Unknown Source)
[na:1.8.0_202] at java.net.Socket.connect(Unknown Source)
[na:1.8.0_202] at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) 
[mysql-connector-java-8.0.22.jar!/:8.0.22] at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
[HikariCP-3.4.5.jar!/:na] at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
[HikariCP-3.4.5.jar!/:na] at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
[HikariCP-3.4.5.jar!/:na] at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
[HikariCP-3.4.5.jar!/:na] at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
[HikariCP-3.4.5.jar!/:na] at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
[HikariCP-3.4.5.jar!/:na] at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
[HikariCP-3.4.5.jar!/:na] at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:38)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:108)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:138)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:50)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:151)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2104)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2041)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2019)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.doQuery(Loader.java:948)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.doList(Loader.java:2850)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.doList(Loader.java:2832)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2664)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.Loader.list(Loader.java:2659)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.hibernate.query.Query.getResultList(Query.java:165)
[hibernate-core-5.4.23.Final.jar!/:5.4.23.Final] at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:177)
[spring-data-jpa-2.4.1.jar!/:2.4.1] at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88)
[spring-data-jpa-2.4.1.jar!/:2.4.1] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155)
[spring-data-jpa-2.4.1.jar!/:2.4.1] at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143)
[spring-data-jpa-2.4.1.jar!/:2.4.1] at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
[spring-data-commons-2.4.1.jar!/:2.4.1] at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
[spring-data-commons-2.4.1.jar!/:2.4.1] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)
[spring-data-commons-2.4.1.jar!/:2.4.1] at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131)
[spring-data-commons-2.4.1.jar!/:2.4.1] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
[spring-data-commons-2.4.1.jar!/:2.4.1] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:371)
[spring-tx-5.3.1.jar!/:5.3.1] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:134)
[spring-tx-5.3.1.jar!/:5.3.1] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
[spring-tx-5.3.1.jar!/:5.3.1] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:145)
[spring-data-jpa-2.4.1.jar!/:2.4.1] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
[spring-aop-5.3.1.jar!/:5.3.1] at com.sun.proxy.$Proxy121.fetchRestaurants(Unknown Source)
[na:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[na:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
[na:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[na:1.8.0_202] at java.lang.reflect.Method.invoke(Unknown Source)
[na:1.8.0_202] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
[spring-aop-5.3.1.jar!/:5.3.1] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
[spring-aop-5.3.1.jar!/:5.3.1] at com.sun.proxy.$Proxy62.fetchRestaurants(Unknown Source)
[na:na] at october.foods.services.restaurant.RestaurantService.fetchRestaurants(RestaurantService.java:100)
[classes!/:0.0.1-SNAPSHOT] at october.foods.controller.restaurant.RestaurantController.fetchRestaurants(RestaurantController.java:50)
[classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[na:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
[na:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[na:1.8.0_202] at java.lang.reflect.Method.invoke(Unknown Source)
[na:1.8.0_202] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
[spring-web-5.3.1.jar!/:5.3.1] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
[spring-web-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:807)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
[spring-webmvc-5.3.1.jar!/:5.3.1] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
[spring-webmvc-5.3.1.jar!/:5.3.1] at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
[tomcat-embed-core-9.0.39.jar!/:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
[spring-webmvc-5.3.1.jar!/:5.3.1] at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
[tomcat-embed-core-9.0.39.jar!/:4.0.FR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 
[tomcat-embed-websocket-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
[spring-web-5.3.1.jar!/:5.3.1] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
[spring-web-5.3.1.jar!/:5.3.1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
[spring-web-5.3.1.jar!/:5.3.1] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
[spring-web-5.3.1.jar!/:5.3.1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
[spring-web-5.3.1.jar!/:5.3.1] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
[spring-web-5.3.1.jar!/:5.3.1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
[na:1.8.0_202] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
[na:1.8.0_202] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
[tomcat-embed-core-9.0.39.jar!/:9.0.39] at java.lang.Thread.run(Unknown Source) [na:1.8.0_202]

根据Andrew的建议,添加了ConnectionPoolContextListener、

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@WebListener("Creates a connection pool that is stored in the Servlet's context for later use.")
public class ConnectionPoolContextListener implements ServletContextListener {

private static final String CLOUD_SQL_CONNECTION_NAME = "CLOUD_SQL_CONNECTION_NAME";
private static final String DB_USER = "DB_USER";
private static final String DB_PASS = "DB_PASS";
private static final String DB_NAME = "DB_NAME";
private DataSource createConnectionPool() {
// [START cloud_sql_mysql_servlet_create]
// The configuration object specifies behaviors for the connection pool.
HikariConfig config = new HikariConfig();
// The following URL is equivalent to setting the config options below:
// jdbc:mysql:///<DB_NAME>?cloudSqlInstance=<CLOUD_SQL_CONNECTION_NAME>&
// socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<DB_USER>&password=<DB_PASS>
// See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
// https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url
// Configure which instance and what database user to connect with.
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
config.setUsername(DB_USER); // e.g. "root", "mysql"
config.setPassword(DB_PASS); // e.g. "my-password"
// For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections.
// See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
// The ipTypes argument can be used to specify a comma delimited list of preferred IP types 
// for connecting to a Cloud SQL instance. The argument ipTypes=PRIVATE will force the 
// SocketFactory to connect with an instance's associated private IP. 
config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE");
// ... Specify additional connection properties here.
// [START_EXCLUDE]
// [START cloud_sql_mysql_servlet_limit]
// maximumPoolSize limits the total number of concurrent connections this pool will keep. Ideal
// values for this setting are highly variable on app design, infrastructure, and database.
config.setMaximumPoolSize(5);
// minimumIdle is the minimum number of idle connections Hikari maintains in the pool.
// Additional connections will be established to meet this value unless the pool is full.
config.setMinimumIdle(5);
// [END cloud_sql_mysql_servlet_limit]
// [START cloud_sql_mysql_servlet_timeout]
// setConnectionTimeout is the maximum number of milliseconds to wait for a connection checkout.
// Any attempt to retrieve a connection from this pool that exceeds the set limit will throw an
// SQLException.
config.setConnectionTimeout(10000); // 10 seconds
// idleTimeout is the maximum amount of time a connection can sit in the pool. Connections that
// sit idle for this many milliseconds are retried if minimumIdle is exceeded.
config.setIdleTimeout(600000); // 10 minutes
// [END cloud_sql_mysql_servlet_timeout]
// [START cloud_sql_mysql_servlet_backoff]
// Hikari automatically delays between failed connection attempts, eventually reaching a
// maximum delay of `connectionTimeout / 2` between attempts.
// [END cloud_sql_mysql_servlet_backoff]
// [START cloud_sql_mysql_servlet_lifetime]
// maxLifetime is the maximum possible lifetime of a connection in the pool. Connections that
// live longer than this many milliseconds will be closed and reestablished between uses. This
// value should be several minutes shorter than the database's timeout value to avoid unexpected
// terminations.
config.setMaxLifetime(1800000); // 30 minutes
// [END cloud_sql_mysql_servlet_lifetime]
// [END_EXCLUDE]
// Initialize the connection pool using the configuration object.
DataSource pool = new HikariDataSource(config);
// [END cloud_sql_mysql_servlet_create]
return pool;
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// This function is called when the Servlet is destroyed.
HikariDataSource pool = (HikariDataSource) event.getServletContext().getAttribute("my-pool");
if (pool != null) {
pool.close();
}
}
@Override
public void contextInitialized(ServletContextEvent event) {
// This function is called when the application starts and will safely create a connection pool
// that can be used to connect to.
ServletContext servletContext = event.getServletContext();
DataSource pool = (DataSource) servletContext.getAttribute("my-pool");
if (pool == null) {
pool = createConnectionPool();
servletContext.setAttribute("my-pool", pool);
}
}
}

现在错误变为:

com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
... 33 common frames omitted
Caused by: java.net.SocketTimeoutException: connect timed out
java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_202]
java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_202]
java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_202]
java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_202]
java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_202]
java.net.Socket.connect(Unknown Source) ~[na:1.8.0_202]
com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.22.jar!/:8.0.22]

它在本地使用ConnectionPoolContextListener

java.net.SocketTimeoutException是指在接收到远程主机的响应之前的连接超时。原因可能是本地网络问题、代理或防火墙配置不正确。如果您使用的是专用IP,则需要检查docker网桥网络的IP范围。以下是文档中的内容:

如果客户端无法使用专用IP连接到云SQL实例,请检查客户端是否使用172.17.0.0/16范围内的任何IP。从172.17.0.0/16范围内的任何IP到使用专用IP的云SQL实例的连接都会失败。类似地,使用该范围内的IP创建的云SQL实例是不可访问的。此范围是为docker网桥网络保留的。

要解决您遇到的一些问题,请遵循此处的文档并发布您收到的任何错误消息,例如,您可以尝试:

尝试使用gcloud-sql-connect命令连接到您的实例。此命令在短时间内授权您的IP地址。您可以在安装了Cloud SDK和mysql客户端的环境中运行此命令。您也可以在Cloud Shell中运行此命令,该命令可在Google Cloud控制台中获得,并预装了Cloud SDK和mysql客户端。

暂时允许所有IP地址连接到一个实例。对于IPv4,授权0.0.0.0/0(对于IPv6,授权::/0测试完后,请确保在它向世界开放时再次将其删除

您正在使用连接池吗?

如果没有,我会创建一个连接缓存,这样当您的应用程序需要链接到数据库时,它可以从池中获得临时连接。一旦应用程序完成其操作,连接将再次返回到池中供以后使用。为了正确工作,连接需要有效地打开和关闭,而不浪费任何资源。

相关内容

  • 没有找到相关文章

最新更新