Spring 流 - 将事件发送到多个目的地
Spring Stream - Send event to multiple destinations
我正在使用 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 Docs
我正在使用 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 Docs