【问题标题】:How to configure different Kafka Brokers/endpoints for different test environments?如何为不同的测试环境配置不同的 Kafka Brokers/endpoints?
【发布时间】:2020-01-15 22:07:00
【问题描述】:

我在我的集​​成测试中使用 Kafka,它发布到不同的主题,我的测试从它们中读取并验证。现在,我创建了一个具有不同 Kafka 常量/端点的类,但是这些常量在不同的环境中有所不同;比如说 - ST、SIT 等。 如何根据环境配置这些常量,以便在管道中运行我的测试的任何环境都可以选择正确的常量/端点。

目前如下图,请指导如何在各种环境下进行配置。

package Kafka;

//ST

public interface KafkaConst {

public static String KAFKA_BROKERS = "https://10.156.192.120:1211";

public static Integer MESSAGE_COUNT=10;

public static String INBOUND_TOPIC_NAME="publish.st"

public static String GROUP_ID_CONFIG="consumerGroup1";

public static String SCHEMA_REGISTRY = "http://10.156.192.71:1212";

public static Integer MAX_NO_MESSAGE_FOUND_COUNT=10;

public static String OFFSET_RESET_LATEST="latest";

public static String OFFSET_RESET_EARLIER="earliest";

public static Integer MAX_POLL_RECORDS=1000; 

public static String KAFKA_File="src/test/resources/TransformedXML/";

}

//坐下

public interface KafkaConst {

public static String KAFKA_BROKERS = "https://10.156.165.120:1211";

public static Integer MESSAGE_COUNT=10;

public static String INBOUND_TOPIC_NAME="publish.sit"

public static String GROUP_ID_CONFIG="consumerGroup1";

public static String SCHEMA_REGISTRY = "http://10.156.165.71:1212";

public static Integer MAX_NO_MESSAGE_FOUND_COUNT=10;

public static String OFFSET_RESET_LATEST="latest";

public static String OFFSET_RESET_EARLIER="earliest";

public static Integer MAX_POLL_RECORDS=1000; 

public static String KAFKA_File="src/test/resources/TransformedXML/";

}

【问题讨论】:

    标签: java apache-kafka automated-tests spring-kafka


    【解决方案1】:

    我建议远离硬编码值并使用环境变量。大多数 CI/CD 工具都支持在管道运行时注入环境变量,而在您的代码中,这些值不是来自硬编码的类,而是来自环境变量。

    我看到您已经在 Spring 应用程序中添加了 spring-kafka 标记...从环境变量中获取值非常容易。您可以使用@Value 注释或@ConfigurationProperties 注释。互联网上有大量的例子。

    【讨论】:

    • 或使用TestContainers / EmbeddedKafka 运行“集成测试”
    【解决方案2】:

    使用Spring Profiles 选择适用于每个环境的属性。

    根据一些任意系统状态,有条件地启用或禁用完整的@Configuration 类甚至单个@Bean methods 通常很有用。一个常见的例子是使用@Profile 注解仅在 Spring 环境中启用特定配置文件时才激活 bean(有关详细信息,请参阅 Bean 定义配置文件)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2020-02-22
      • 2012-03-22
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多