【问题标题】:Spring Stream - Send event to multiple destinationsSpring Stream - 将事件发送到多个目的地
【发布时间】:2020-06-04 07:46:00
【问题描述】:

我正在将 Spring Cloud Stream 与 RabbitMQ 一起使用,我需要发送一个需要恰好 2 个消费者消费的事件。

在生产者中我添加了多个目的地:

标签微服务:

public interface OutgoingEventChannels {

    @Output("updateTagNameChannel")
    MessageChannel updateTagName();
}

@Component
@EnableBinding(OutgoingEventChannels.class)
public class EventProducer {

    @Autowired
    private OutgoingEventChannels outgoingEventChannels;

    public void sendUpdateTagNameEvent(UpdateTagNameEvent updateTagNameEvent) {
        outgoingEventChannels.updateTagName().send(new GenericMessage<>(updateTagNameEvent));
    }
}

spring.cloud.stream.bindings.updateTagNameChannel.destination=updateCustomerTagName,updateSectionTagName
spring.cloud.stream.bindings.updateTagNameChannel.group=tags-group

每个消费者都绑定到不同的目的地:

客户微服务:

public interface IncomingEventChannels {

    @Input("updateTagNameChannel")
    MessageChannel updateTagName();
}

@Component
@EnableBinding(IncomingEventChannels.class)
public class EventListener {

    private static final Logger LOG = LogManager.getLogger(EventListener.class);

    @Autowired
    private CustomerService customerService;

    @StreamListener("updateTagNameChannel")
    public void handleUpdateTagNameEvent(UpdateTagNameEvent updateTagNameEvent) {
        LOG.info("Received update tag event: " + updateTagNameEvent);
        customerService.updateTagName(updateTagNameEvent);
    }
}

spring.cloud.stream.bindings.updateTagNameChannel.destination=updateCustomerTagName
spring.cloud.stream.bindings.updateTagNameChannel.group=tags-group

两个消费者中的任何一个都不会收到该事件。有谁知道我做错了什么?

提前谢谢你!

【问题讨论】:

    标签: spring-boot stream rabbitmq spring-cloud


    【解决方案1】:

    如果我理解正确,您希望两个消费者都获得数据的副本。在这种情况下,您希望您的两个消费者位于两个不同的消费者组中。如果两个消费者都在同一个消费者组中,那么他们中只有一个会收到事件。

    您可以在此处找到有关消费者群体的更多详细信息:Spring Docs

    【讨论】:

    • 谢谢!就是这样! =)
    • 如果对您有帮助,请考虑接受答案!
    猜你喜欢
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2020-04-08
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多