【问题标题】:How to connect to CosmosDB emulator running locally on docker-compose如何连接到在 docker-compose 上本地运行的 CosmosDB 模拟器
【发布时间】:2022-12-15 21:12:08
【问题描述】:

我正在尝试通过 docker-compose 为 CosmosDB 模拟器运行一个 linux 容器,但我无法连接到它。

我有以下 docker-compose 文件:

version: '3.4'
services:
  local-cosmosdb:
    image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:mongodb"
    container_name: local.cosmosdb
    tty: true
    restart: always
    environment:
      - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
      - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
    ports:
      - 8081:8081
      - 10250:10250
      - 10251:10251
      - 10252:10252
      - 10253:10253
      - 10254:10254
      - 10255:10255

当我启动容器时,控制台显示所有分区都已成功启动并且已启动并正在运行。通过使用telnet,我也确认了那里一些进程监听本地主机上的端口,例如 10250 和 8081。但是,我无法使用连接字符串 mongodb://localhost:8081 连接到数据库(MongoDB 使用相同的字符串但在端口 27017 上工作,我假设在 cosmos 中有一个等效字符串)。

我也试过:

  1. 使用任何其他暴露的端口(包括 10250 和 10255)
  2. 为模拟器尝试不同的标签
  3. 结合各种端口使用容器名称而不是本地主机(例如mongodb://local.cosmosdb:8081

    有关于通过连接字符串直接连接到 Azure Cosmos DB 的文档,但我真的找不到任何关于连接到 Cosmos DB 模拟器的资源在 docker compose 上运行特别是...我能找到的最好的是:CosmosDb Emulator with docker-compose 这并没有真正回答我的问题。我也遇到过这个:How to start CosmosDB emulator with docker-compose? 但它不包括获取实际的连接字符串。

    对此的任何帮助将不胜感激!

【问题讨论】:

  • 微软在修补他们的许可证时所做的任何更改 whoopsie 也搞砸了 MongoDB API 模拟器并使其无法使用......

标签: mongodb docker docker-compose azure-cosmosdb azure-cosmosdb-emulator


【解决方案1】:

就像我在评论中提到的那样,Microsoft 在急于修复 the license issue 时似乎已经做了一些破坏 MongoDB API 模拟器的事情。

在更改之前,要通过 MongoDB 客户端连接到模拟器,您需要做的就是fetch the emulator certificate,然后用它初始化客户端,例如在爪哇中:

var storePassword = // whatever you used to create the keystore

var sslContext = new SSLContextBuilder()
        .loadKeyMaterial(keyStoreFile, storePassword, storePassword)
        .loadTrustMaterial(keyStoreFile, storePassword, TrustSelfSignedStrategy.INSTANCE)
        .setKeyStoreType("PKCS12")
        .build();

 var connectionUrl = "mongodb://localhost:" + URLEncoder.encode(EMULATOR_KEY, UTF_8) + "@localhost:10255/admin?ssl=true&retrywrites=false"

var clientSettings = MongoClientSettings.builder()
        .applyConnectionString(new ConnectionString(connectionUrl()))
        .applyToSslSettings(builder -> builder.enabled(true)
                .invalidHostNameAllowed(true)
                .context(sslContext))
        .build();

MongoClients.create(clientSettings);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 2019-08-29
    • 1970-01-01
    • 2020-11-17
    • 2021-10-28
    • 1970-01-01
    • 2020-09-22
    • 2018-03-15
    相关资源
    最近更新 更多