用于与Google云模拟器交互的工具集
emulator-tools的Python项目详细描述
模拟器工具
googlecloud提供emulators来本地测试PubSub和BigTable等云组件,而不必连接到远程实例。在这里,您将找到与模拟器轻松交互的工具,用于设置本地开发环境。在
使用pip(Python 3)安装:
pip install emulator-tools
仿真器
基本设置
模拟器随^{
启动
启动仿真器(根据代码使用的内容设置项目)
^{pr2}$设置环境
然后在运行程序之前设置程序的环境变量。您还需要设置使用模拟器工具的环境。在
$(gcloud beta emulators pubsub env-init)
echo $PUBSUB_EMULATOR_HOST # verify the environment variable is set
$(gcloud beta emulators bigtable env-init)
echo $BIGTABLE_EMULATOR_HOST
工具
要使用这些工具,您需要安装python和需求,如果使用pyenv
:
pyenv install $(cat .python-version)
pyenv local
pip install -r requirements.txt
PubSub公司
在starting the emulator,set the environment之后,开始设置pubsub。在
首先,检查以确保您连接到的是仿真器,而不是生产实例。在
emulator-tools pubsub <project-id> list-topics
结果应该是空的,否则如果您看到一个主题列表,您就连接到生产!确保环境变量设置正确。 检查连接后,可以根据组件的需要创建主题(通常可以在应用程序的配置文件中找到这些主题名称):
emulator-tools pubsub <project-id> create-topic <topic-name>
然后创建订阅:
emulator-tools pubsub <project-id> create-subscription <topic-name> <subscription-name>
您可以检查以验证您的主题和订阅:
emulator-tools pubsub <project-id> list-topics
emulator-tools pubsub <project-id> list-subscriptions-in-topic <topic-name>
emulator-tools pubsub <project-id> list-subscriptions-in-project
然后,您可以测试发布和接收消息:
cat sample-message.json | emulator-tools pubsub <project-id> publish <topic-name>
emulator-tools pubsub <project-id> receive-messages <subscription-name>
如果您看到“Received message:message{”包括文件sample.json
的内容,那么您就一切就绪了!在
现在可以将项目连接到此模拟器,并使用这些脚本向其发送信封。在
如果你想为你的项目提供一个样本信封,你可以通过这个应用的产品级pubsub订阅,使用“查看消息”然后“拉”来获取一个示例消息。有时你可能需要点击“拉”多次来获取一些信息。注意不要“回覆”信息。在
{1}$
大表
在starting the emulator,set the environment之后,开始设置BigTable。在
首先,检查以确保您连接到的是仿真器,而不是生产实例。在
emulator-tools bigtable <project-id> <instance-id> list-tables
结果应该是空的,否则如果您看到一个表列表,您就连接到生产了!确保环境变量设置正确。 检查连接后,可以根据组件的需要创建表(通常可以在应用程序的配置文件中找到这些表名)。要创建表,您需要以JSON格式提供列族和列类型的列表:
{
"column_families": [{
"name": "i",
"columns": [{
"key": "id",
"type": "long"
}, {
"key": "score",
"type": "double"
}, {
"key": "title",
"type": "string"
}]
}, {
"name": "m",
"columns": [{
"key": "namespace",
"type": "string"
}]
}]
}
cat sample-table.json | emulator-tools bigtable <project-id> <instance-id> create-table <table-name>
{cd4>可以使用新的文件格式插入一行:
{
"rows": [
{
"rowkey": "testing#12345",
"columns": [
{
"key": "i:id",
"value": 1234,
"timestamp": 1579607960.80492
},
{
"key": "i:title",
"value": "Here's a test title for you"
},
{
"key": "i:score",
"value": 1.5
},
{
"key": "m:namespace",
"value": "core"
}
]
}
]
}
cat sample-row.json | emulator-tools bigtable <project-id> <instance-id> write <table-name> sample-table.json
要读取行:
emulator-tools bigtable <project-id> <instance-id> read <table-name> sample-table.json
# for specific rowkeys
emulator-tools bigtable <project-id> <instance-id> read <table-name> sample-table.json --rowkey testing:.*
您还可以使用与BigTable交互的官方工具cbt与仿真环境进行交互。只需注意正确设置环境,不要与生产实例交互。在
# list tables
cbt -project <project-id> -instance <instance-id> ls
# list column families in table
cbt -project <project-id> -instance <instance-id> ls <table-name>
# read rows
cbt -project <project-id> -instance <instance-id> read <table-name>
如果需要项目的一些示例数据,可以将这些数据从生产实例导入到本地模拟器。为此,请从远程实例中读取并将其写入一个文件,然后在设置了正确的环境变量的另一个终端中将其写入本地仿真器。在
# in a terminal with no emulator env variables
emulator-tools bigtable <project-id> <instance-id> read <table-name> sample-table.json --limit 5 --json > data.json
# in a terminal with emulator environment set
cat data.json | emulator-tools bigtable <project-id> <instance-id> write <table-name> sample-table.json
然后您应该能够在模拟环境中检查此数据:
emulator-tools bigtable <project-id> <instance-id> read <table-name> sample-table.json
服务模式
我们在AYLIEN使用这些脚本的方式是,我们为每个serviceint都有一个setup-emulators.sh
脚本使用BigTable和PubSub进行交互,这些脚本负责设置必要的表、主题、订阅,并在运行时发送示例消息。这意味着入门就像用模拟器环境变量运行应用程序并运行setup-emulators.sh
一样简单。在
示例脚本可能如下所示:
$(gcloud beta emulators bigtable env-init)
$(gcloud beta emulators pubsub env-init)
project="awesome-project-staging"
emulator-tools pubsub $project create-topic test
emulator-tools pubsub $project create-subscription test test
cat ./messages/love-letter.json | emulator-tools pubsub $project publish test
已知问题
Alpakka不能连接到我的PubSub模拟器!在
Alpakka PubSub GRPC connector似乎还不支持模拟器环境变量(但是http version does)。如果要在使用GRPC时连接到模拟器,则可能需要手动配置alpakka以指向中的仿真器。为此,请在application.conf
文件中添加以下配置:
// Overriding configuration for PubSub Emulator
alpakka.google.cloud.pubsub.grpc {
host = "localhost"
port = "8085"
rootCa = "none"
callCredentials = "none"
}
- 项目
标签: