1
votes

I am using spring cloud bus to publish event to kafka so that another instance can listen the same event . Event is being triggered but not being published to kafka . I am using spring cloud bus with spring cloud stream .

version : Spring Boot : 2.0, Spring cloud Bus : 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>

Publishing Event :

@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);
    }
}

Event Class :

@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;
    }


}

Event Listener :

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

    @Override
    public void onApplicationEvent(MessagingEventBus messagingEventBus) {
       log.info("Messaging Event Bus Listener called");
    }
}
1
Can you add the code sample that is publishing the event.asolanki
i have added code snippet . Plz see my editMd Shadab Ali

1 Answers

1
votes

This is the process of bus sending event.

  1. new RemoteApplicationEvent() //create event
  2. applicationContext#publishEvent //publish local event
  3. BusAutoConfiguration#acceptLocal //bus accept local event which 2 step send
  4. serviceMatcher#isFromSelf //bus will judge the event is self send? if itself, sent to out bound channel(5 step)
  5. cloudBusOutboundChannel#send

the problem is 4 step judge fail, You can use BusProperties#getId for event originService, it's work

@Autowired
BusProperties busProperties;

public void fire(){
    new RemoteApplicationEvent(this, busProperties.getId().......
}