示例:Atlas 集群的专用连接
本页面介绍如何启用从 Integration Connectors 连接到 Google Cloud 专用网络上运行的 MongoDB Atlas 集群。
为 Atlas 集群配置专用连接
如需为 Atlas 集群配置专用连接,请执行以下操作:
- 登录您的 Atlas 管理控制台。
- 打开需要建立网络连接的 Atlas 项目。
- 打开项目的网络访问权限页面,然后选择专用端点。
- 选择专用集群,然后点击添加专用端点。
系统会打开一个对话框,供您选择云服务提供商。
- 选择 Google Cloud,然后点击下一步。
- 选择一个 Atlas 区域,然后点击下一步。
这是创建 Atlas 集群的 Google Cloud 区域,它应该与您创建连接的 Google Cloud 项目所在的区域相同。如需查看所有受支持的集成连接器区域的列表,请参阅位置。
- 输入 Google Cloud 项目 ID。这是 Integration Connectors 运行时中服务目录的项目 ID。如需了解详情,请参阅获取服务目录的项目 ID。
- 在 VPC 名称字段中,输入
default
。 - 在子网名称字段中,输入
default
。 - 在 Private Service Connect 端点前缀字段中,输入一个前缀。
此文本的前缀是所有端点名称,可用于识别您创建的专用端点。
- 安装
gcloud
CLI(命令行界面)。如需了解详情,请参阅安装 gcloud CLI。 - 在本地系统上复制以下 Shell 脚本并将其保存为
setup_psc.sh
。为脚本中的以下变量设置适当的值:
- PROJECT_ID
- 地区
- PREFIX
- SERVICE_ATTACHMENT_ID_PREFIX
#!/bin/bash alias gcurl='curl -H "Authorization: Bearer $(gcloud auth print-access-token)"' # Set PROJECT_ID to the ID of the Google Cloud project where you create your connections. export PROJECT_ID=PROJECT_ID # Set REGION to the Google Cloud region where Atlas cluster is installed. export REGION=REGION export HOSTNAME=https://connectors.googleapis.com/v1 # Set ENDPOINT_PREFIX to the prefix value that you entered for the # "Private service connect endpoint prefix" field (in step 10). export PREFIX=ENDPOINT_PREFIX # Set SERVICE_ATTACHMENT_ID_PREFIX to the prefix of the service attachments # created by Atlas. The service attachments are present in the script # displayed on the Atlas UI and have the following naming format: # projects/{google-cloud-project-id-of-atlas-cluster}/regions/{region}/serviceAttachment/{prefix}-{index} # For example, if the service attachment is # "projects/p-ditnerelrvndieqhe3kh9pop/regions/us-west1/serviceAttachments/sa-us-west1-37204mg5wr0j6-$i", # you must set the value of SERVICE_ATTACHMENT_ID_PREFIX to # "projects/p-ditnerelrvndieqhe3kh9pop/regions/us-west1/serviceAttachments/sa-us-west1-37204mg5wr0j6" export SERVICE_ATTACHMENT_ID_PREFIX=SERVICE_ATTACHMENT_ID_PREFIX function poll_operation() { OPERATION_ID="$1" INDEX="$2" RESULT='' while [[ "$RESULT" != *"true"* ]]; do RESULT=$(gcurl -s -X GET "${HOSTNAME}/${OPERATION_ID}" | grep '"done"') sleep 5 done echo "${PREFIX}-${INDEX} created" } echo "Creating Endpoint Attachments..." for i in {0..49} do export ENDPOINT=${HOSTNAME}/projects/${PROJECT_ID}/locations/${REGION}/endpointAttachments?endpointAttachmentId=${PREFIX}-$i OPERATION_ID=$(gcurl -s -X POST -H "Content-Type: application/json" -d '{"name": "'"projects/${PROJECT_ID}/locations/${REGION}/endpointAttachments/${PREFIX}-$i"'","serviceAttachment": "'"${SERVICE_ATTACHMENT_ID_PREFIX}-$i"'"}' "${ENDPOINT}" | sed 's/ //' | grep -E '"name"' | sed -E 's/"name": "//' | sed 's/",//') poll_operation "$OPERATION_ID" "$i" & done wait echo "All endpoint attachments created." echo "[" > atlasEndpoints-${PREFIX}.json echo "Fetching IP Addresses..." for i in {0..49} do ENDPOINT=${HOSTNAME}/projects/${PROJECT_ID}/locations/${REGION}/endpointAttachments/${PREFIX}-$i gcurl -s -X GET "${ENDPOINT}" | sed 's/ //g' | grep -E 'name|endpointIp' | sed 's/"endpointIp"/"IPAddress"/g' | sed -E 's/"name": "projects\/.+\/locations\/.+\/endpointAttachments\//"name": "/g' | sed '1 s/"/{"/1' | sed '2 s/"/"},/4' >> atlasEndpoints-${PREFIX}.json done sed -i '$s/,/]/' atlasEndpoints-${PREFIX}.json echo "Created atlasEndpoints-${PREFIX}.json"
- 运行 Shell 脚本。如需运行 Shell 脚本,请前往保存脚本的目录,然后运行以下命令:
sh setup_psc.sh
此脚本会生成一个 JSON 文件,其中包含 IP 地址及其对应的 PSC 端点名称列表。JSON 文件保存在运行 Shell 脚本的目录中。
- 在 Atlas 管理控制台中点击下一步。
- 点击上传 JSON 文件,然后上传您在第 13 步中创建的 JSON 文件。
- 点击创建。
端点设置过程最多可能需要 25 分钟才能完成,如果设置成功,端点将处于
Available
状态。 - 验证您的专用连接。如需了解详情,请参阅验证专用连接。
获取服务目录的项目 ID
如需获取服务目录的项目 ID,您可以使用 List Connections API,如以下示例所示。
语法
curl -X GET \ -H "authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://connectors.googleapis.com/v1/projects/CONNECTORS_PROJECT_ID/locations/-/connections"
请替换以下内容:
- CONNECTORS_PROJECT_ID:您在其中创建了连接的 Google Cloud 项目的 ID。
示例
此示例会获取 connectors-test
Google Cloud 项目的服务目录的项目 ID。
curl -X GET \ -H "authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ "https://connectors.googleapis.com/v1/projects/connectors-test/locations/-/connections"
在终端上运行此命令会显示类似于以下内容的输出:
..... { "connections": [ { "name": "projects/connectors-test/locations/asia-northeast1/connections/big-query-iam-invalid-sa", "createTime": "2022-10-07T09:02:31.905048520Z", "updateTime": "2022-10-07T09:22:39.993778690Z", "connectorVersion": "projects/connectors-test/locations/global/providers/gcp/connectors/bigquery/versions/1", "status": { "state": "ACTIVE" }, "configVariables": [ { "key": "project_id", "stringValue": "connectors-test" }, { "key": "dataset_id", "stringValue": "testDataset" } ], "authConfig": {}, "serviceAccount": "564332356444-compute@developer.gserviceaccount.com", "serviceDirectory": "projects/abcdefghijk-tp/locations/asia-northeast1/namespaces/connectors/services/runtime", "nodeConfig": { "minNodeCount": 2, "maxNodeCount": 50 } }, ....
在示例输出中,对于 connectors-test
Google Cloud 项目,服务目录的项目 ID 为 abcdefghijk-tp
。
验证专用连接
如需验证 Atlas 集群的专用连接,请执行以下操作:
- 在 Atlas 管理控制台中,打开 Database Deployments 页面。
- 滚动到所需的集群,然后点击连接。此时将打开连接到 <CLUSTER_NAME> 对话框。
- 点击专用端点以选择连接类型。
- 从选择专用端点下拉列表中选择您在 Atlas 界面中输入的专用端点前缀。
- 点击选择连接方法。
- 在 Connect to your application 部分中,点击 Drivers。此步骤将获取
srv
连接字符串。 - 复制将您的连接字符串添加到您的应用代码中部分中的
srv
连接字符串。srv
字符串的格式:mongodb+srv://{cluster-name}-pl-{index}.{unique-pin}.mongodb.net
。 - 使用此
srv
字符串作为主机名来创建 MongoDB 连接。如需了解详情,请参阅配置 MongoDB 连接器。连接应已成功创建。