简体   繁体   English

Kafka 和 ActiveMQ 的通用消息代理接口

[英]Common message broker interface for Kafka and ActiveMQ

I want to design an interface which would be used for simple sending and receiving messages between spring boot micro services.我想设计一个接口,用于在 spring 启动微服务之间简单地发送和接收消息。 Any service should be able to autowire the interface and use send/receive methods.任何服务都应该能够自动连接接口并使用发送/接收方法。 The implementation of those methods should depend upon, say application.props file whether it wants to use kafka or activemq.这些方法的实现应该取决于 application.props 文件是要使用 kafka 还是 activemq。 I am not able to come up with a clean design.我无法想出一个干净的设计。 I was thinking if I could create a custom annotation for it.我在想是否可以为它创建一个自定义注释。 With whatever spring boot experience I have I am not able to imagine such a design.无论 spring 启动体验如何,我都无法想象这样的设计。 The skeleton looks like something below but I am not able to see how will I be able to implement this as proper code: beans, switch between kafka/activemq specific annotations and methods, etc. things like that.骨架看起来像下面的东西,但我无法看到如何将其实现为正确的代码:bean,在 kafka/activemq 特定注释和方法之间切换,等等。

Service:服务:

@Autowired MessageProducer messageProducer @Autowired MessageProducer messageProducer

@Autowired MessageReceiver messageReceiver @Autowired MessageReceiver messageReceiver

MessageProducer [interface] public void sendMessage(destination, payload) MessageProducer [接口] public void sendMessage(destination, payload)

MessageReceiver [interface] public void receiveMessage(source, payload) MessageReceiver [接口] public void receiveMessage(source, payload)

you could have two implementation classes like KafkaMessageProducer and ActvieMQMessageProducer .你可以有两个实现类,比如KafkaMessageProducerActvieMQMessageProducer inside application.properties you could have config like messaging.type=kafka or messaging.type=activemq that will specify which messaging communication should application select.application.properties中,你可以有像 messinging.type messaging.type=kafka或 messinging.type messaging.type=activemq这样的配置,它将指定应该应用 select 的消息通信。

Configuration class:配置 class:

@Configuration
public class MessagingConfiguration {

    @Bean
    @ConditionalOnProperty(name = "messaging.type", havingValue = "kafka")
    public MessageProducer messageProducer() {
        return new KafkaMessageProducer();
    }
    
    @Bean
    @ConditionalOnProperty(name = "messaging.type", havingValue = "activemq")
    public MessageProducer messageProducer() {
        return new ActvieMQMessageProducer();
    }
}

if you want to use both messaging types in the same application (eg one service class will use Kafka meanwhile another class will use ActiveMQ), then create two beans (as mentioned above, but with @Qualifier and without @ConditionalOnProperty ).如果您想在同一个应用程序中使用这两种消息类型(例如,一个服务 class 将使用 Kafka,而另一个 class 将使用 ActiveMQ),然后创建两个 bean(如上所述,但使用@Qualifier和不@ConditionalOnProperty )。 And on each service, that require message producer, specify with @Qualifier which implementation you need在每个需要消息生产者的服务上,使用@Qualifier指定您需要的实现

@Configuration
public class MessagingConfiguration {

    @Bean
    @Qualifier("kafkaMessageProducer")
    public MessageProducer messageProducer() {
        return new KafkaMessageProducer();
    }
    
    @Bean
    @Qualifier("actvieMQMessageProducer")
    public MessageProducer messageProducer() {
        return new ActvieMQMessageProducer();
    }

    @Bean
    public YourService messageProducer(@Qualifier("kafkaMessageProducer") MessageProducer messageProducer) {
        return new YourServiceImpl(messageProducer);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM