英文:
Communication link failure: Google Cloud Function app can't connect to Google Cloud SQL instance
问题
问题描述:
部署到Google Cloud Function实例的Java Spring Cloud Functions应用程序无法识别Google Cloud SQL实例上的数据库设置。使用以下命令将应用程序部署到Cloud Function服务:
gcloud alpha functions deploy function-sample-gcp-background \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java17 \
--trigger-topic example-functions-app \
--source target/deploy \
--memory 512MB \
--region europe-central2
在云函数实例日志中返回错误:
Exception in thread "main" java.lang.RuntimeException: Could not construct an instance of org.springframework.cloud.function.adapter.gcp.GcfJarLauncher: java.lang.reflect.InvocationTargetException
...
Caused by: 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 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
...
一些更详细的运行时Spring Boot日志:
[function-sample-gcp-background] com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
...
如何解决无法连接Google Cloud SQL实例上设置的数据库与Cloud Function实例的问题?
相关数据:
上面的Google Cloud服务基础设施图显示了Spring Cloud Functions应用程序的Google Cloud设置基础设施。
该应用程序具有使用JPA实现的SQL数据库设置,配置存储在application.yml中,如下所示:
spring:
datasource:
username: username
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://99.999.999.9/db-name
jpa:
hibernate:
ddl-auto: update
default:
properties:
hibernate:
bytecode:
provider: none
cloud:
gcp:
sql:
instance-connection-name: instance-connection-name
database-name: db-name
config:
credentials:
location: service-account-ceredntials.json
为了匿名性,上述配置的值已重命名。
service-account-credentials.json 包含服务帐户密钥。该服务帐户具有以下角色:
- Cloud Functions Developer
- Cloud SQL Editor
重要提示:
当Spring Cloud Function应用程序在本地主机上运行时,该应用程序能够使用Cloud SQL实例的公共IP地址连接到Cloud SQL数据库。问题发生在将Spring Cloud Function应用程序部署到Google Cloud时。
英文:
Problem statement
A Java Spring Cloud Functions application deployed to Google Cloud Function instance doesn't recognize a database setup on Google Cloud SQL instance. Deploying the application to Cloud Function service using:
gcloud alpha functions deploy function-sample-gcp-background \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java17 \
--trigger-topic example-functions-app \
--source target/deploy \
--memory 512MB \
--region europe-central2
returns error in cloud function instance log:
Exception in thread "main" java.lang.RuntimeException: Could not construct an instance of org.springframework.cloud.function.adapter.gcp.GcfJarLauncher: java.lang.reflect.InvocationTargetException
at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:125)
at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:115)
at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:304)
at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:241)
at com.google.cloud.functions.invoker.runner.Invoker.main(Invoker.java:124)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:122)
... 4 more
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.springframework.cloud.function.adapter.gcp.GcfJarLauncher.<init>(GcfJarLauncher.java:48)
... 10 more
Caused by: 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 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:910)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.init(FunctionInvoker.java:97)
at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.<init>(FunctionInvoker.java:84)
at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.<init>(FunctionInvoker.java:80)
... 16 more
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:284)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:246)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:223)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:175)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:295)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:252)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:223)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:173)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
... 31 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:138)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:272)
some further runtime spring boot logs:
[function-sample-gcp-background] com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
[function-sample-gcp-background] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[function-sample-gcp-background] at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:824) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
[function-sample-gcp-background] at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:272) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:246) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:223) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:175) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:295) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:252) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:223) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:173) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
[function-sample-gcp-background] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156) ~[spring-context-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:910) ~[spring-context-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.26.jar!/:5.3.26]
[function-sample-gcp-background] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.10.jar!/:2.7.10]
[function-sample-gcp-background] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.10.jar!/:2.7.10]
[function-sample-gcp-background] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.10.jar!/:2.7.10]
[function-sample-gcp-background] at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.init(FunctionInvoker.java:97) ~[spring-cloud-function-adapter-gcp-4.0.1.jar!/:na]
[function-sample-gcp-background] at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.<init>(FunctionInvoker.java:84) ~[spring-cloud-function-adapter-gcp-4.0.1.jar!/:na]
[function-sample-gcp-background] at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.<init>(FunctionInvoker.java:80) ~[spring-cloud-function-adapter-gcp-4.0.1.jar!/:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
[function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
[function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
[function-sample-gcp-background] at org.springframework.cloud.function.adapter.gcp.GcfJarLauncher.<init>(GcfJarLauncher.java:48) ~[video-stream-function-0.0.1-SNAPSHOT
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
[function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
[function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
[function-sample-gcp-background] at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:122) ~[na:na]
[function-sample-gcp-background] at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:115) ~[na:na]
[function-sample-gcp-background] at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:304) ~[na:na]
[function-sample-gcp-background] at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:241) ~[na:na]
[function-sample-gcp-background] at com.google.cloud.functions.invoker.runner.Invoker.main(Invoker.java:124) ~[na:na]
[function-sample-gcp-background] Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
[function-sample-gcp-background] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
[function-sample-gcp-background] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
[function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
[function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
[function-sample-gcp-background] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
[function-sample-gcp-background] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
How do I manage this issue where I can't connect the database that is setup on Google Cloud SQL instance with Cloud Function instance?
Relevant Data
The Diagram above shows infrastracture of a google cloud setup of a Spring Cloud Functions application.
The application has an sql database setup with jpa implementation, the configuration is stored in application.yml as followed:
spring:
datasource:
username: username
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://99.999.999.9/db-name
jpa:
hibernate:
ddl-auto: update
default:
properties:
hibernate:
bytecode:
provider: none
cloud:
gcp:
sql:
instance-connection-name: instance-connection-name
database-name: db-name
config:
credentials:
location: service-account-ceredntials.json
For the purpose of anonymity I renamed values of above configurations.
service-account-credentials.json contain service-account key. That service account has roles of:
Cloud Functions Developer
Cloud SQL Editor
Important Note
When the Spring Cloud Function application is run on localhost, the application is able to connect to the database on Cloud SQL using SQL instance's public IP address. The issue occurs when the Spring Cloud Function application is being deployed to Google Cloud.
答案1
得分: 2
发布这篇文章作为一个社区维基,供大家查看。
如楼主所提到的,Spring Boot Hibernate可以自动确定要使用的正确方言,但它需要与数据库的实时连接。由于在本地运行时不存在问题。您可以尝试使用此文档来调试您的连接。
英文:
Posting this as a community wiki for everyone's visibility.
As mentioned by the OP, Spring Boot Hibernate can determine the correct dialect to use automatically, but it needs a live connection to the database. Since the issue is non-existent when running locally. You can try to debug your connectivity with this documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论