EmbeddedKafkaRule 没有 brokerListProperty 方法

EmbeddedKafkaRule have no brokerListProperty method

我想弄清楚 Spring 卡夫卡。在遵循参考时,我在 this example 中发现了错误。 EmbeddedKafkaRuleclass中没有brokerListProperty()方法。我应该如何重构代码以使其工作?

上面 link 中的代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTests {

    @ClassRule
    public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1,
        false, "someTopic")
            .brokerListProperty("spring.kafka.bootstrap-servers");
    }

    @Autowired
    private KafkaTemplate<String, String> template;

    @Test
    public void test() {
        ...
    }

}

您可以像这样使用 KafkaTestUtils

添加 brokerListProperty
    @Before
  public void setUp() throws Exception {
    // set up the Kafka producer properties
    Map<String, Object> senderProperties =
        KafkaTestUtils.senderProps(
            broker.getEmbeddedKafka().getBrokersAsString());
  }

你可以按照这个 tutorial 看起来很正确。

@SpringBootTest 一起考虑改用 @EmbeddedKafka。 那个有一个 属性 像:

/**
 * The property name to set with the bootstrap server addresses instead of the default
 * {@value org.springframework.kafka.test.EmbeddedKafkaBroker#SPRING_EMBEDDED_KAFKA_BROKERS}.
 * @return the property name.
 * @since 2.3
 * @see org.springframework.kafka.test.EmbeddedKafkaBroker#brokerListProperty(String)
 */
String bootstrapServersProperty() default "";

EmbeddedKafkaRule 的目标,当我们根本不在测试中使用 Spring 时。 我同意我们缺少从 EmbeddedKafkaRulebrokerListProperty() 的传播 属性。请随时提出 GH 问题并解决这个问题。

同时,您可以像这样解决它:

@ClassRule
public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1, false, "someTopic");

@BeforeAll
pubic static void setup() {
    broker.getEmbeddedKafka().brokerListProperty("spring.kafka.bootstrap-servers");
}