我正在使用来自GitHub(https://github.com/marinkobabic/axon-kafka-example)的Kafka示例,并且在连接到本地部署的Kafka集群时可以正常工作。正如Axon的docs所建议的那样,我试图通过暴露 KafkaPublisher bean并覆盖 withProducerFactory (),来使用自定义的kafka ProducerFactory )。我的bean已创建,但是我的自定义kafka ProducerFactory 的 createProducer ()从未被调用。实际上,仍使用Axon的 DefaultProducerFactory 。有什么建议吗?
@Bean
KafkaPublisher<byte[], byte[]> kafkaPublisher(ProducerFactory factory) {
KafkaPublisherConfiguration configuration = KafkaPublisherConfiguration.<String, byte[]>builder()
.withMessageSource(new SimpleEventBus())
.withProducerFactory(new org.axonframework.kafka.eventhandling.producer.ProducerFactory<String, byte[]>() {
@Override
public Producer<String, byte[]> createProducer() {
return factory.createProducer();
}
@Override
public void shutDown() {
}
})
.withTopic(topic)
.build();
KafkaPublisher<byte[], byte[]> publisher = new KafkaPublisher<>(configuration);
publisher.start();
return publisher;
}
答案 0 :(得分:2)
我可以通过保留 KafkaPublisher 来解决问题,但是我需要公开一个 EventBus bean(我在 Sender.java ),然后将发布者连接到该发布者。我还需要公开包装自己的kafka ProducerFactory 的轴突 ProducerFactory 。我的最终配置如下:
@Configuration
@AutoConfigureBefore(KafkaAutoConfiguration.class)
class AxonConfig {
@Value("${axon.kafka.default-topic}")
private String topic;
@Bean
public org.axonframework.kafka.eventhandling.producer.ProducerFactory<byte[], byte[]> producerFactory(ProducerFactory factory) {
return new org.axonframework.kafka.eventhandling.producer.ProducerFactory<byte[], byte[]>() {
@Override
public Producer<byte[], byte[]> createProducer() {
return factory.createProducer();
}
@Override
public void shutDown() {
}
};
}
@Bean("event-bus")
EventBus eventBus() {
return new SimpleEventBus();
}
@Bean
KafkaPublisher<byte[], byte[]> kafkaPublisher(org.axonframework.kafka.eventhandling.producer.ProducerFactory factory, EventBus eventBus) {
KafkaPublisherConfiguration configuration = KafkaPublisherConfiguration.<String, byte[]>builder()
.withMessageSource(eventBus)
.withProducerFactory(factory)
.withTopic(topic)
.build();
PhotonKafkaPublisher<byte[], byte[]> publisher = new PhotonKafkaPublisher<>(configuration);
eventBus.subscribe((events) -> publisher.sendEvents(events));
publisher.start();
return publisher;
}
}