0

我正在使用 FluentDocker 进行一些集成测试。容器在 docker-compose 中定义:

version: '3.4'

services:

  zookeeper:
    image: confluentinc/cp-zookeeper:5.5.6
    container_name: "zookeeper"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181

  kafka:
    image: confluentinc/cp-kafka:5.5.6
    container_name: "kafka"
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_HOST://localhost:29092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    ports:
      - 29092:29092

  mongodb:
    image: mongo:5.0
    container_name: "mongodb"
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: MyPassw0rd_123
      MONGO_INITDB_DATABASE: broker-messages
    volumes:
      - mongodata:/data/db
      - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
    ports:
      - "27017:27017"
 
volumes:
  mongodata:

然后,我使用以下代码来启动服务:

 public class DockerComposeFixture : IDisposable
    {
        private readonly ICompositeService compositeServices;
        public DockerComposeFixture()
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), (TemplateString)"Resources", "docker-compose.yml");
            compositeServices = Fd.UseContainer()
                .UseCompose()
                .ForceRecreate()
                .ServiceName("test-services")
                .FromFile(file)
                .RemoveOrphans()
                .ForceRecreate()
                .Build()
                .Start();
        }

        public ICompositeService CompositeServices => compositeServices; 

        public void Dispose()
        {
            compositeServices?.Dispose();
            GC.SuppressFinalize(this);
        }
    }

这是我的测试课:

public class BrokerFacadeApiIntegrationTests : IClassFixture<DockerComposeFixture>, IClassFixture<CustomWebApplicationFactory<Startup>>
{
    private readonly DockerComposeFixture dockerCompose;
    private readonly CustomWebApplicationFactory<Startup> factory;

    public BrokerFacadeApiIntegrationTests(DockerComposeFixture dockerCompose, CustomWebApplicationFactory<Startup> factory)
    {
        this.dockerCompose = dockerCompose;
        this.factory = factory;
    }

    
    [Fact]
    public async Task ItPublishMessageToBrokerIfItsUp()
    {
        var bytes = Encoding.UTF8.GetBytes("Hello word!");
        var encodedString = Convert.ToBase64String(bytes);
        var message = new { contentAsBase64 = encodedString};
        var jsonMessage = JsonSerializer.Serialize(message);
        var stringContent = new StringContent(jsonMessage, Encoding.UTF8, "application/json");
        var client = factory.CreateClient();
        var response = await client.PostAsync("/api/v1/messagebroker/topics/test-topic/messages", stringContent);
        Assert.True(response.IsSuccessStatusCode);
    }
}

在 appsetings.json 中,我指向 localhost:

  "KafkaSeting": {
    "BootstrapServers": "localhost:29092",
    "GroupId": "foo",
    "Topic": "dcb",
    "NumPartitions": 1,
    "ReplicationFactor": 1,
    "MessageTimeoutMs": 3000,
    "MessageSendMaxRetries": 1
  },
  "MongoDbConfig": {
    "MongoDbConnection": "mongodb://localhost:MyPassw0rd_@mongodb:27017/?authSource=broker-messages",
    "Database": "broker-messages"
  }

但是,我无法连接到 mongo 或 kafka 服务。我应该怎么做才能找到这些服务?

4

0 回答 0