如何从 Google 云 运行 SQL 云实例连接 Spring 引导服务?
How to connect a Spring Boot service from Google Cloud Run to SQL cloud instance?
我创建了一个 SQL 实例。这是工作。
然后,我在 Container Registry 上为我的 Spring 启动应用程序上传了 docker 图像。
我的目标是 运行 Google Cloud 运行 上的应用程序,但为了使其正常工作,它必须连接到数据库。因此,在创建服务时,我给它图像并传递环境变量:
- spring.datasource.url(这里我作为值传递 - jdbc:mysql://(public sql 实例的 ip 地址)/(数据库名称)?useSSL=假
- spring.datasource.username ...
- spring.datasource.password ...
此外,我在高级设置中连接到云 SQL 实例。
但是,当我部署它时,它失败了。 spring 应用程序正在启动,但随后出现通信 link 故障。我想它找不到数据库或者它必须对权限做一些事情。
我是 Google Cloud 的新手,我需要一些帮助。我该如何解决这个问题?或者我做错了什么?
默认情况下,无服务器(如 Cloud 运行)无法访问其他资源。
您必须配置 Serverless VPC Access 才能在专用网络中使用数据库、google 服务或其他服务。
然后您必须将无服务器 VPC 访问分配给您的云 运行 服务。
Cloud SQL 的 MySQL 实例可以有一个 Public IP 和一个私有 IP。
要使用 Public IP 从实例连接云 运行 应用程序,需要通过 Unix 套接字使用云 SQL Auth 代理。您可以按照this document了解步骤。
要使用私有 IP 从实例连接云 运行 应用程序,需要使用无服务器 VPC 访问连接器。你跟着this document了解步骤。
您已经为 Spring 引导应用程序创建了一个映像并将其部署在云端 运行 并传递了环境变量。在 spring.datasource.url 中,您直接使用了 Public IP,这将无法通过 Unix 套接字路径作为 the recommended way to connect using the Public IP is using the CloudSQL Auth Proxy 使用。
我建议您使用 here 中提到的 jdbcUrl 作为 spring.datasource.url
,它看起来像这样-
jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>&useSSL=false
其中,
<DATABASE_NAME> = Your database name in the MySQL instance
<INSTANCE_CONNECTION_NAME> = Connection name of the MySQL instance of Cloud SQL (can be found in the instance overview page in the Cloud Console) which follows a certain pattern i.e. <project-id : region : instance-id>.
<MYSQL_USER_NAME> = Username for the MySQL instance
<MYSQL_USER_PASSWORD> = Password for the above username
在使用上述 spring.datasource.url
时,您不必分别指定 spring.database.username
和 spring.datasource.password
属性,因为 URL 中都提到了两者.
我创建了一个 SQL 实例。这是工作。 然后,我在 Container Registry 上为我的 Spring 启动应用程序上传了 docker 图像。
我的目标是 运行 Google Cloud 运行 上的应用程序,但为了使其正常工作,它必须连接到数据库。因此,在创建服务时,我给它图像并传递环境变量:
- spring.datasource.url(这里我作为值传递 - jdbc:mysql://(public sql 实例的 ip 地址)/(数据库名称)?useSSL=假
- spring.datasource.username ...
- spring.datasource.password ...
此外,我在高级设置中连接到云 SQL 实例。
但是,当我部署它时,它失败了。 spring 应用程序正在启动,但随后出现通信 link 故障。我想它找不到数据库或者它必须对权限做一些事情。
我是 Google Cloud 的新手,我需要一些帮助。我该如何解决这个问题?或者我做错了什么?
默认情况下,无服务器(如 Cloud 运行)无法访问其他资源。
您必须配置 Serverless VPC Access 才能在专用网络中使用数据库、google 服务或其他服务。
然后您必须将无服务器 VPC 访问分配给您的云 运行 服务。
Cloud SQL 的 MySQL 实例可以有一个 Public IP 和一个私有 IP。
要使用 Public IP 从实例连接云 运行 应用程序,需要通过 Unix 套接字使用云 SQL Auth 代理。您可以按照this document了解步骤。
要使用私有 IP 从实例连接云 运行 应用程序,需要使用无服务器 VPC 访问连接器。你跟着this document了解步骤。
您已经为 Spring 引导应用程序创建了一个映像并将其部署在云端 运行 并传递了环境变量。在 spring.datasource.url 中,您直接使用了 Public IP,这将无法通过 Unix 套接字路径作为 the recommended way to connect using the Public IP is using the CloudSQL Auth Proxy 使用。
我建议您使用 here 中提到的 jdbcUrl 作为 spring.datasource.url
,它看起来像这样-
jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>&useSSL=false
其中,
<DATABASE_NAME> = Your database name in the MySQL instance
<INSTANCE_CONNECTION_NAME> = Connection name of the MySQL instance of Cloud SQL (can be found in the instance overview page in the Cloud Console) which follows a certain pattern i.e. <project-id : region : instance-id>.
<MYSQL_USER_NAME> = Username for the MySQL instance
<MYSQL_USER_PASSWORD> = Password for the above username
在使用上述 spring.datasource.url
时,您不必分别指定 spring.database.username
和 spring.datasource.password
属性,因为 URL 中都提到了两者.