我在使用 Linux Cosmos DB Docker 容器(通过 Linux Mint 上的 Rider 运行)时遇到很多麻烦 - mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator [最新]。
我已成功安装并运行它,并且可以通过 https://localhost:8081/_explorer/index.html 访问本地模拟器 UI。从这里我可以创建一个数据库和一些容器,甚至添加一些随机数据。
当我尝试使用代码连接时,问题就出现了。我正在使用标准连接字符串进行连接...
AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
我最初遇到了一些 SSL 问题,这些问题是通过导入 SSL 证书解决的 - 这对于重新生成 docker 容器来说有点痛苦,但没关系。主要问题是,一旦我过去了,任何尝试的操作都会挂起。
我开始尝试创建数据库和容器,然后尝试手动创建它们并尝试添加数据,然后加载一些手动创建的项目。
在所有情况下,只要代码调用
CosmosClient
方法,例如...
全部失败。我什至没有收到错误消息,代码只是挂起。它甚至不会超时 - 它会静置并旋转一个多小时,什么也不做。
经过一番挖掘,我发现“调试输出”窗口只是循环显示以下内容...
DocDBTrace Information: 0 : TimerPool Created with minSupportedTimerDelayInSeconds = 1
DocDBTrace Information: 0 : TimerPool Created with minSupportedTimerDelayInSeconds = 1
DocDBTrace Information: 0 : Resolving Master service address, forceMasterRefresh: False, currentMaster:
DocDBTrace Warning: 0 : ClientRetryPolicy: Gateway HttpRequestException Endpoint not reachable. Failed Location: https://172.17.0.4:8081/; ResourceAddress: dbs/brain-store
DocDBTrace Information: 0 : GlobalEndpointManager: Marking endpoint https://172.17.0.4:8081/ unavailable for read
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Endpoint https://172.17.0.4:8081/ unavailable for Read added/updated to unavailableEndpoints with timestamp 04/22/2024 15:17:17
DocDBTrace Information: 0 : GlobalEndpointManager: Marking endpoint https://172.17.0.4:8081/ unavailable for Write
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Endpoint https://172.17.0.4:8081/ unavailable for Write added/updated to unavailableEndpoints with timestamp 04/22/2024 15:17:17
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Resolving Master service address, forceMasterRefresh: False, currentMaster:
Exited Thread 94863
Exited Thread 95169
Exited Thread 95168
DocDBTrace Warning: 0 : ClientRetryPolicy: Gateway HttpRequestException Endpoint not reachable. Failed Location: https://172.17.0.4:8081/; ResourceAddress: dbs/brain-store
DocDBTrace Information: 0 : GlobalEndpointManager: Marking endpoint https://172.17.0.4:8081/ unavailable for read
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Endpoint https://172.17.0.4:8081/ unavailable for Read added/updated to unavailableEndpoints with timestamp 04/22/2024 15:17:28
DocDBTrace Information: 0 : GlobalEndpointManager: Marking endpoint https://172.17.0.4:8081/ unavailable for Write
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Endpoint https://172.17.0.4:8081/ unavailable for Write added/updated to unavailableEndpoints with timestamp 04/22/2024 15:17:28
DocDBTrace Information: 0 : Resolving Master service address, forceMasterRefresh: False, currentMaster:
Exited Thread 95167
Exited Thread 94872
DocDBTrace Warning: 0 : ClientRetryPolicy: Gateway HttpRequestException Endpoint not reachable. Failed Location: https://172.17.0.4:8081/; ResourceAddress: dbs/brain-store
DocDBTrace Information: 0 : GlobalEndpointManager: Marking endpoint https://172.17.0.4:8081/ unavailable for read
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Endpoint https://172.17.0.4:8081/ unavailable for Read added/updated to unavailableEndpoints with timestamp 04/22/2024 15:17:38
DocDBTrace Information: 0 : GlobalEndpointManager: Marking endpoint https://172.17.0.4:8081/ unavailable for Write
DocDBTrace Information: 0 : Current WriteEndpoints = (https://172.17.0.4:8081/) ReadEndpoints = (https://172.17.0.4:8081/)
DocDBTrace Information: 0 : Endpoint https://172.17.0.4:8081/ unavailable for Write added/updated to unavailableEndpoints with timestamp 04/22/2024 15:17:38
(为简洁起见已编辑)
我尝试 ping 172.17.0.4,它在我的网络上不存在,所以我假设它只对 docker 本身有效 - 问题是,如果是这样的话,我的代码应该如何连接到它?这些错误表明模拟器告诉我的代码连接到不存在的端点。
我的 docker 容器有以下设置:
端口:
我错过了什么吗?
此问题的解决方案是确保 Docker 容器指定一个覆盖的 IP 地址 - 如果您不这样做,则容器默认值似乎是使用随机地址。
这可以通过设置以下参数来解决...
AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=127.0.0.1
这使得我的完整 docker run 命令成为...
docker run -p 8081:8081 -p 10250:10250 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254 -p 10255:10255 --env AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=1 --env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=127.0.0.1 --name cosmos-emulator --pull missing -t -i mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest