我试图让两个消费者订阅一个EmbeddedKafkaBroker
。第一个成功了,第二个失败了。@EmbeddedKafka
和经纪人都@ClassRule
失败了。
@RunWith(SpringRunner.class)
@SpringBootTest
@EmbeddedKafka(topics = { "topic" })
public class AnnotationEmbeddedKafkaTest {
@Autowired
private EmbeddedKafkaBroker broker;
@Test
public void annotationEmbeddedKafkaTest() {
Map<String, Object> consumerProps1 = KafkaTestUtils.consumerProps("testEmbedded", "false", broker);
Consumer<String, String> consumer1 = new KafkaConsumer<>(consumerProps1);
broker.consumeFromAnEmbeddedTopic(consumer1, "topic");
System.out.println("consumer1 assignments=" + consumer1.assignment());
Map<String, Object> consumerProps2 = KafkaTestUtils.consumerProps("testEmbedded", "false", broker);
Consumer<String, String> consumer2 = new KafkaConsumer<>(consumerProps2);
broker.consumeFromAnEmbeddedTopic(consumer2, "topic");
System.out.println("consumer2 assignments=" + consumer2.assignment());
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class ClassRuleEmbeddedKafkaTest {
@ClassRule
public static EmbeddedKafkaRule embeddedKafkaRule = new EmbeddedKafkaRule(1, false, "topic");
private EmbeddedKafkaBroker broker = embeddedKafkaRule.getEmbeddedKafka();
@Test
public void classRuleEmbeddedKafkaTest() {
Map<String, Object> consumerProps1 = KafkaTestUtils.consumerProps("testEmbedded", "false", broker);
Consumer<String, String> consumer1 = new KafkaConsumer<>(consumerProps1);
broker.consumeFromAnEmbeddedTopic(consumer1, "topic");
System.out.println("consumer1 assignments=" + consumer1.assignment());
Map<String, Object> consumerProps2 = KafkaTestUtils.consumerProps("testEmbedded", "false", broker);
Consumer<String, String> consumer2 = new KafkaConsumer<>(consumerProps2);
broker.consumeFromAnEmbeddedTopic(consumer2, "topic");
System.out.println("consumer2 assignments=" + consumer2.assignment());
}
}
我期待两个消费者可以订阅一个EmbeddedKafkaBroker
。春季卡夫卡测试有可能吗?
我在这里复制了这个:https ://github.com/yraydhitya/spring-kafka-test-multiple-consumers