我正在使用spring cloud bus将事件发布到kafka,以便其他实例可以监听相同的事件。事件正在被触发,但未发布到kafka。我正在使用spring cloud bus和spring cloud stream。
版本: 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: alipom.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");
}
}发布于 2020-03-06 20:29:25
这是总线发送事件的过程。
//create event
问题是4步判断失败,你可以为事件originService使用BusProperties#getId,它是有效的
@Autowired
BusProperties busProperties;
public void fire(){
new RemoteApplicationEvent(this, busProperties.getId().......
}https://stackoverflow.com/questions/57179696
复制相似问题