通信链接失败:Google Cloud Function 应用无法连接到 Google Cloud SQL 实例。

huangapple go评论93阅读模式
英文:

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服务:

  1. gcloud alpha functions deploy function-sample-gcp-background \
  2. --entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
  3. --runtime java17 \
  4. --trigger-topic example-functions-app \
  5. --source target/deploy \
  6. --memory 512MB \
  7. --region europe-central2

在云函数实例日志中返回错误:

  1. Exception in thread "main" java.lang.RuntimeException: Could not construct an instance of org.springframework.cloud.function.adapter.gcp.GcfJarLauncher: java.lang.reflect.InvocationTargetException
  2. ...
  3. 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]
  4. ...

一些更详细的运行时Spring Boot日志:

  1. [function-sample-gcp-background] com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  2. ...

如何解决无法连接Google Cloud SQL实例上设置的数据库与Cloud Function实例的问题?

相关数据:

上面的Google Cloud服务基础设施图显示了Spring Cloud Functions应用程序的Google Cloud设置基础设施。
该应用程序具有使用JPA实现的SQL数据库设置,配置存储在application.yml中,如下所示:

  1. spring:
  2. datasource:
  3. username: username
  4. password: password
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://99.999.999.9/db-name
  7. jpa:
  8. hibernate:
  9. ddl-auto: update
  10. default:
  11. properties:
  12. hibernate:
  13. bytecode:
  14. provider: none
  15. cloud:
  16. gcp:
  17. sql:
  18. instance-connection-name: instance-connection-name
  19. database-name: db-name
  20. config:
  21. credentials:
  22. 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:

  1. gcloud alpha functions deploy function-sample-gcp-background \
  2. --entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
  3. --runtime java17 \
  4. --trigger-topic example-functions-app \
  5. --source target/deploy \
  6. --memory 512MB \
  7. --region europe-central2

returns error in cloud function instance log:

  1. Exception in thread "main" java.lang.RuntimeException: Could not construct an instance of org.springframework.cloud.function.adapter.gcp.GcfJarLauncher: java.lang.reflect.InvocationTargetException
  2. at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:125)
  3. at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:115)
  4. at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:304)
  5. at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:241)
  6. at com.google.cloud.functions.invoker.runner.Invoker.main(Invoker.java:124)
  7. Caused by: java.lang.reflect.InvocationTargetException
  8. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  9. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
  10. at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  11. at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
  12. at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
  13. at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:122)
  14. ... 4 more
  15. Caused by: java.lang.reflect.InvocationTargetException
  16. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  17. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
  18. at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  19. at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
  20. at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
  21. at org.springframework.cloud.function.adapter.gcp.GcfJarLauncher.<init>(GcfJarLauncher.java:48)
  22. ... 10 more
  23. 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]
  24. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804)
  25. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
  26. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
  27. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
  28. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
  29. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
  30. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
  31. at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156)
  32. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:910)
  33. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
  34. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
  35. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
  36. at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
  37. at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.init(FunctionInvoker.java:97)
  38. at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.<init>(FunctionInvoker.java:84)
  39. at org.springframework.cloud.function.adapter.gcp.FunctionInvoker.<init>(FunctionInvoker.java:80)
  40. ... 16 more
  41. Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  42. at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:284)
  43. at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:246)
  44. at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:223)
  45. at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:175)
  46. at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:295)
  47. at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:252)
  48. at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:223)
  49. at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:173)
  50. at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127)
  51. at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460)
  52. at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494)
  53. at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
  54. at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
  55. at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
  56. at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
  57. at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
  58. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
  59. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)
  60. ... 31 more
  61. Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
  62. at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
  63. at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
  64. at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:138)
  65. at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
  66. at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101)
  67. at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:272)

some further runtime spring boot logs:

  1. [function-sample-gcp-background] com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
  2. [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.
  3. [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]
  4. [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]
  5. [function-sample-gcp-background] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:824) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
  6. [function-sample-gcp-background] at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
  7. [function-sample-gcp-background] at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
  8. [function-sample-gcp-background] at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
  9. [function-sample-gcp-background] at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
  10. [function-sample-gcp-background] at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
  11. [function-sample-gcp-background] at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
  12. [function-sample-gcp-background] at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
  13. [function-sample-gcp-background] at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
  14. [function-sample-gcp-background] at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
  15. [function-sample-gcp-background] at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
  16. [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]
  17. [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]
  18. [function-sample-gcp-background] at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-
  19. at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]
  20. [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]
  21. [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]
  22. [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]
  23. [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]
  24. [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]
  25. [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]
  26. [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]
  27. [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]
  28. [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]
  29. [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]
  30. [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]
  31. [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]
  32. [function-sample-gcp-background] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.26.jar!/:5.3.26]
  33. [function-sample-gcp-background] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.26.jar!/:5.3.26]
  34. [function-sample-gcp-background] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.26.jar!/:5.3.26]
  35. [function-sample-gcp-background] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.26.jar!/:5.3.26]
  36. [function-sample-gcp-background] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.26.jar!/:5.3.26]
  37. [function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.26.jar!/:5.3.26]
  38. [function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.26.jar!/:5.3.26]
  39. [function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:
  40. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.26.jar!/:5.3.26]
  41. [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]
  42. [function-sample-gcp-background] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.26.jar!/:5.3.26]
  43. [function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.26.jar!/:5.3.26]
  44. [function-sample-gcp-background] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.26.jar!/:5.3.26]
  45. [function-sample-gcp-background] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156) ~[spring-context-5.3.26.jar!/:5.3.26]
  46. [function-sample-gcp-background] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:910) ~[spring-context-5.3.26.jar!/:5.3.26]
  47. [function-sample-gcp-background] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.26.jar!/:5.3.26]
  48. [function-sample-gcp-background] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.10.jar!/:2.7.10]
  49. [function-sample-gcp-background] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.10.jar!/:2.7.10]
  50. [function-sample-gcp-background] at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.10.jar!/:2.7.10]
  51. [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]
  52. [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]
  53. [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]
  54. [function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
  55. [function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
  56. [function-sample-gcp-background] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
  57. [function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
  58. [function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
  59. [function-sample-gcp-background] at org.springframework.cloud.function.adapter.gcp.GcfJarLauncher.<init>(GcfJarLauncher.java:48) ~[video-stream-function-0.0.1-SNAPSHOT
  60. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
  61. [function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
  62. [function-sample-gcp-background] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
  63. [function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
  64. [function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
  65. [function-sample-gcp-background] at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:122) ~[na:na]
  66. [function-sample-gcp-background] at com.google.cloud.functions.invoker.BackgroundFunctionExecutor.forClass(BackgroundFunctionExecutor.java:115) ~[na:na]
  67. [function-sample-gcp-background] at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:304) ~[na:na]
  68. [function-sample-gcp-background] at com.google.cloud.functions.invoker.runner.Invoker.startServer(Invoker.java:241) ~[na:na]
  69. [function-sample-gcp-background] at com.google.cloud.functions.invoker.runner.Invoker.main(Invoker.java:124) ~[na:na]
  70. [function-sample-gcp-background] Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
  71. [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.
  72. [function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
  73. [function-sample-gcp-background] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
  74. [function-sample-gcp-background] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
  75. [function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
  76. [function-sample-gcp-background] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
  77. [function-sample-gcp-background] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
  78. [function-sample-gcp-background] at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-j-8.0.32.jar!/:8.0.32]
  79. [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

通信链接失败:Google Cloud Function 应用无法连接到 Google Cloud SQL 实例。
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:

  1. spring:
  2. datasource:
  3. username: username
  4. password: password
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://99.999.999.9/db-name
  7. jpa:
  8. hibernate:
  9. ddl-auto: update
  10. default:
  11. properties:
  12. hibernate:
  13. bytecode:
  14. provider: none
  15. cloud:
  16. gcp:
  17. sql:
  18. instance-connection-name: instance-connection-name
  19. database-name: db-name
  20. config:
  21. credentials:
  22. 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:

  1. Cloud Functions Developer
  2. 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.

huangapple
  • 本文由 发表于 2023年6月16日 04:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定