M2 Mac中Docker运行Pub/Sub模拟器失败问题咨询
M2 Mac上Pub/Sub模拟器卡顿无响应问题解决
问题描述
在M2 Mac上运行Pub/Sub模拟器时,执行创建Topic的代码卡在publisher.create_topic步骤,仅打印到"I am here 7.3"后无报错、无响应。此前在Intel Mac上可正常运行,Docker容器显示AMD64架构警告:
若通过模拟运行,镜像可能性能不佳或运行失败。
原代码如下:
def create_topic(project_id, publisher, topic): print("I am here 7.1") publisher = PublisherClient() print("I am here 7.2") topic_path = publisher.topic_path(project_id, topic) print("I am here 7.3") new_topic = publisher.create_topic(request={"name": topic_path}) print(f"Created topic: {new_topic.name}") return topic_path
问题根源
官方Pub/Sub模拟器默认提供AMD64架构镜像,M2 Mac为ARM64架构,Docker通过Rosetta模拟运行AMD64镜像时会出现兼容性问题,导致网络请求卡顿或无响应。
解决步骤
1. 使用ARM64架构的Pub/Sub模拟器镜像
拉取Google官方提供的ARM64版本镜像,替换原AMD64镜像启动容器:
docker run -d -p 8085:8085 gcr.io/google.com/cloudsdktool/cloud-sdk-emulators:latest-arm64 gcloud beta emulators pubsub start --host-port=0.0.0.0:8085
2. 配置客户端连接到本地模拟器
修改Python代码,明确指定Pub/Sub客户端连接到本地模拟器的地址,同时移除冗余的publisher参数:
from google.cloud import pubsub_v1 def create_topic(project_id, topic): print("I am here 7.1") # 指定连接到本地模拟器 publisher = pubsub_v1.PublisherClient( client_options={"api_endpoint": "localhost:8085"} ) print("I am here 7.2") topic_path = publisher.topic_path(project_id, topic) print("I am here 7.3") new_topic = publisher.create_topic(request={"name": topic_path}) print(f"Created topic: {new_topic.name}") return topic_path
3. 检查Docker模拟器设置(可选)
打开Docker Desktop,进入Settings > Features in development,确保**"Use Rosetta for x86/amd64 emulation on Apple Silicon"**选项已启用,提升跨架构镜像的兼容性。
内容的提问来源于stack exchange,提问作者Asad Amir Khwaja
相关产品推荐
相关产品推荐

