简体   繁体   English

如何为不同的测试环境配置不同的 Kafka Brokers/endpoints?

[英]How to configure different Kafka Brokers/endpoints for different test environments?

I am using Kafka in my integration tests wherein it publishes to different topics and my tests reads from them and validates.我在我的集​​成测试中使用 Kafka,其中它发布到不同的主题,我的测试从中读取并验证。 Now, I have created a class with different Kafka constants/endpoints, but these constants vary on different environments;现在,我创建了一个具有不同 Kafka 常量/端点的类,但是这些常量在不同的环境中有所不同; say for eg - ST, SIT, etc. How do I configure these constants as per environment so that in pipeline whichever environment am running my tests on it picks up the right constants/endpoints.比如说 - ST、SIT 等。我如何根据环境配置这些常量,以便在管道中运行我的测试的任何环境都会选择正确的常量/端点。

It currently looks like below, please guide how to configure on various environments.目前看起来如下,请指导如何在各种环境中进行配置。

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/";

}

//SIT //坐

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/";

}

I would suggest getting away from hardcoded values and use environment variables.我建议远离硬编码值并使用环境变量。 Most of the CI/CD tools support injecting environment variables in the pipeline runtime, while in your code you take those values not from hardcoded classes but from environment variables.大多数 CI/CD 工具都支持在管道运行时注入环境变量,而在您的代码中,您不是从硬编码类而是从环境变量中获取这些值。

I see that you've added the spring-kafka tag... in a Spring app, it's super easy to get values from environment variables.我看到您已经添加了spring-kafka标签……在 Spring 应用程序中,从环境变量中获取值非常容易。 You can either use the @Value annotation or the @ConfigurationProperties one.您可以使用@Value注释或@ConfigurationProperties注释。 There are tons of examples on the internet for both.互联网上有大量的例子。

Use Spring Profiles to select which properties apply in each environment.使用Spring Profiles选择在每个环境中应用哪些属性。

It is often useful to conditionally enable or disable a complete @Configuration class or even individual @Bean methods , based on some arbitrary system state.基于某些任意系统状态,有条件地启用或禁用完整的@Configuration类甚至单个@Bean methods通常很有用。 One common example of this is to use the @Profile annotation to activate beans only when a specific profile has been enabled in the Spring Environment (see Bean Definition Profiles for details).一个常见的例子是,只有在 Spring 环境中启用了特定配置文件时,才使用@Profile注释激活 bean(有关详细信息,请参阅 Bean 定义配置文件)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置tomcat在单个应用程序中为不同的端点使用不同的端口? - How to configure tomcat to use different ports for different endpoints in a single application? 如何从与不同代理关联的多个 Kafka 主题中消费? - How can I consume from multiple Kafka topics that are associated with different brokers? 如何针对不同的环境进行开发 - How to develop for different environments Maven,Jenkins - 如何将项目构建到不同的测试环境? - Maven, Jenkins - how to build project to different test environments? 如何配置gradle在不同的环境中使用不同的log4j.properties文件? - How to configure gradle to use different log4j.properties files in different environments? 如何测试骆驼路由依次调用了不同的端点? - How can I test that a Camel route calls different endpoints in order? 如何为不同的环境设置java - How to set the java for different environments 我可以使用不同的 kafka 代理集来存储 Kafka 流应用程序的 state 吗? - Can I use different set of kafka brokers for storing the state of Kafka streams application? 如何在不同的环境中处理不同的用户 - How to handle different users in different environments 如何在不同的环境中运行 API 自动化测试脚本,特别是我需要在 QA 环境和 prod 环境中运行 - How to run API automation test scripts in different environments, specifically I need to run in QA environment & prod environment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM