未发布到Kafka的Spring Cloud Bus事件(RemoteApplicationEvent)

问题描述 投票:0回答:1

我正在使用Spring Cloud Bus将事件发布到kafka,以便另一个实例可以侦听同一事件。事件正在触发,但尚未发布给kafka。我正在将Spring Cloud Bus与Spring Cloud Stream一起使用。

版本:春季靴子:2.0,春云巴士:2.0.0,Spring Cloud Stream:2.0.1

application.yml:

server:
  port: 7711
spring:
  application:
    index: ${random.uuid}
  cloud:
    bus:
      enabled: true
    stream:
      kafka:
        binder:
          brokers: localhost:9092
      bindings:
        input:
          destination: EMPLOYEE-TOPIC-DEMO-R1-P1
          group: ali

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>

发布事件:

@Autowired
private ApplicationContext context;

@StreamListener(ConsumerStream.INPUT)
public void messageConsumer(@Payload String jsonValue, @Headers MessageHeaders header) {

    try {
        log.info("Enter in Consumer->messageConsumer()");
        final String myUniqueId = context.getId();
        context.publishEvent(new MessagingEventBus(this,myUniqueId,header));
    } catch (Exception e) {
        log.error("Exception caught while processing the request :", e);
    }
}

事件类别:

@Slf4j
public class MessagingEventBus extends RemoteApplicationEvent {


    private MessageHeaders header;

    // Must supply a default constructor and getters/setters for deserialization
    public MessagingEventBus() {
    }

    public MessagingEventBus(Object source, String originService, MessageHeaders header) {
        // source is the object that is publishing the event
        // originService is the unique context ID of the publisher
        super(source, originService);
        this.header = header;
    }


}

事件监听器:

@Component
@Slf4j
public class MessagingEventBusListener implements ApplicationListener<MessagingEventBus> {

    @Override
    public void onApplicationEvent(MessagingEventBus messagingEventBus) {
       log.info("Messaging Event Bus Listener called");
    }
}
apache-kafka spring-cloud-stream spring-cloud-bus
1个回答
0
投票

这是总线发送事件的过程。

1.new RemoteApplicationEvent()//创建事件2.applicationContext#publishEvent //触发本地事件3.BusAutoConfiguration#acceptLocal //总线接受触发了2步的本地事件4.serviceMatcher#isFromSelf //总线会判断事件是自发的吗?如果本身,则发送到出站通道(5步)5.cloudBusOutboundChannel#send

问题是四步判断失败,您可以将BusProperties#getId用于事件originService,它可以正常工作

@Autowired
BusProperties busProperties;

public void fire(){
    new RemoteApplicationEvent(this, busProperties.getId().......
}
© www.soinside.com 2019 - 2024. All rights reserved.