本页介绍了如何将 PGAdapter 连接到 Spanner 模拟器。模拟器在本地运行,您可以使用它来开发和测试应用,而无需创建 Google Cloud 项目或结算帐号。由于模拟器仅将数据存储在内存中,因此所有状态(包括数据、架构和配置)都会在重启时丢失。模拟器提供与 Spanner 生产服务相同的 API,适用于本地开发和测试,而非生产部署。
您可以通过三种不同的方式将 PGAdapter 连接到模拟器:
- 使用 PGAdapter 和模拟器运行预构建的组合 Docker 容器。
- 在本地机器上运行模拟器和 PGAdapter。
- 在 Docker 网络中运行模拟器和 PGAdapter。
组合 Docker 容器
运行包含 PGAdapter 和模拟器的预构建 Docker 映像。Docker 容器中的 PGAdapter 实例会自动配置为连接到该容器中的模拟器。
使用以下命令启动 Docker 容器。
docker pull gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator
docker run -d \
-p 5432:5432 \
-p 9010:9010 \
-p 9020:9020 \
gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator
启动容器时,您无需指定项目、实例或数据库名称。它将默认使用项目名称 emulator-project
和实例名称 test-instance
。您连接的任何数据库都由 PGAdapter 自动创建。
您可以连接到 PGAdapter 并使用以下命令通过 psql
执行语句。
psql -h localhost -p 5432 -d test-database
系统会自动创建数据库 test-database
。执行以下语句以验证您已连接到 PostgreSQL 方言数据库数据库:
create table my_table (
id bigint not null primary key,
value varchar
);
insert into my_table (id, value) values (1, 'One');
select * from my_table;
您还可以直接连接到 Docker 容器内的模拟器,例如使用 gcloud CLI。
gcloud config configurations create emulator
gcloud config set auth/disable_credentials true
gcloud config set project emulator-project
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
gcloud spanner instances list
gcloud spanner databases execute-sql test-database \
--instance=test-instance \
--sql="select * from my_table"
在本地机器上运行模拟器和 PGAdapter
您可以在本地计算机上运行模拟器和 PGAdapter,然后使用以下命令将 PGAdapter 连接到模拟器。
首先启动模拟器。
gcloud emulators spanner start
然后,启动 PGAdapter 并将其连接到模拟器。请注意,您必须在本地机器上将 PGAdapter 作为 Java 应用启动,这样它才能访问模拟器。如果您在 Docker 容器中启动 PGAdapter,它将无法访问在本地主机上运行的模拟器。
wget https://storage.googleapis.com/pgadapter-jar-releases/pgadapter.tar.gz \
&& tar -xzvf pgadapter.tar.gz
java -jar pgadapter.jar -p emulator-project -i test-instance \
-c "" \
-r autoConfigEmulator=true
PGAdapter 用于连接到模拟器的其他命令行参数如下:
-c ""
:此指令指示 PGAdapter 不使用任何凭据。-r autoConfigEmulator=true
:此命令指示 PGAdapter 连接到localhost:9010
,后者是默认的模拟器主机和端口。它还指示 PGAdapter 自动创建用户连接的任何数据库。这意味着您无需在连接到数据库之前创建数据库。
您可以连接到 PGAdapter 并使用以下命令通过 psql
执行语句。
psql -h localhost -p 5432 -d test-database
系统会自动创建数据库 test-database
。执行以下语句以验证您已连接到 PostgreSQL 方言数据库数据库:
create table my_table (
id bigint not null primary key,
value varchar
);
insert into my_table (id, value) values (1, 'One');
select * from my_table;
在 Docker 网络中运行模拟器和 PGAdapter
您可以在 Docker 网络中运行模拟器和 PGAdapter,然后使用以下命令将 PGAdapter 连接到模拟器。
cat <<EOT > docker-compose.yml
version: "3.9"
services:
emulator:
image: "gcr.io/cloud-spanner-emulator/emulator"
pull_policy: always
container_name: spanner-emulator
ports:
- "9010:9010"
- "9020:9020"
pgadapter:
depends_on:
emulator:
condition: service_started
image: "gcr.io/cloud-spanner-pg-adapter/pgadapter"
pull_policy: always
container_name: pgadapter-connected-to-emulator
command:
- "-p emulator-project"
- "-i test-instance"
- "-r autoConfigEmulator=true"
- "-e emulator:9010"
- "-c \"\""
- "-x"
ports:
- "5432:5432"
EOT
docker compose up -d
PGAdapter 和模拟器都在同一个 Docker 网络中启动,并且 PGAdapter 已配置为连接到模拟器。PGAdapter 的其他命令行参数(用于连接到模拟器)包括:
-c ""
:此指令指示 PGAdapter 不使用任何凭据。-r autoConfigEmulator=true
:此属性指示 PGAdapter 自动创建用户连接的任何数据库。这意味着您无需创建数据库即可连接到该数据库。-e emulator:9010
:-e
指定 PGAdapter 应连接的端点。emulator:9010
是同一 Docker 网络中模拟器的名称和端口号。-x
:此选项允许从本地计算机连接到 PGAdapter。
您可以连接到 PGAdapter 并使用以下命令通过 psql
执行语句。
psql -h localhost -p 5432 -d test-database
系统会自动创建数据库 test-database
。执行以下语句以验证您已连接到 PostgreSQL 方言数据库数据库:
create table my_table (
id bigint not null primary key,
value varchar
);
insert into my_table (id, value) values (1, 'One');
select * from my_table;
您也可以直接连接到 Docker 网络中的模拟器,例如使用 gcloud CLI。
gcloud config configurations create emulator
gcloud config set auth/disable_credentials true
gcloud config set project emulator-project
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
gcloud spanner instances list
gcloud spanner databases execute-sql test-database \
--instance=test-instance \
--sql="select * from my_table"