我有2个微服务(图像和注释),它们通过Spring Cloud Stream与代理Rabbit MQ相互通信(由eureka发现服务发现)。我想从图像微服务发送一条消息以评论微服务。 问题在于未调用CommentController StreamListener方法“保存”。
图像微服务-> CommentController.java:
public CommentController(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
this.flux = Flux.<Message<Comment>>create(
emitter -> this.commentSink = emitter,
FluxSink.OverflowStrategy.IGNORE)
.publish()
.autoConnect();
}
@PostMapping("/comments")
public Mono<String> addComment(Mono<Comment> newComment) {
if (commentSink != null) {
return newComment
.map(comment -> {
commentSink.next(MessageBuilder
.withPayload(comment)
.setHeader(MessageHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.build());
return comment;
})
.flatMap(comment -> {
meterRegistry
.counter("comments.produced", "imageId", comment.getImageId())
.increment();
return Mono.just("redirect:/");
});
} else {
return Mono.just("redirect:/");
}
}
@StreamEmitter
@Output(Source.OUTPUT)
public void emit(FluxSender output) {
output.send(this.flux);
}
评论微服务-> CommentService.java
@StreamListener
@Output(Processor.OUTPUT)
public Flux<Void> save(@Input(Processor.INPUT) Flux<Comment> newComment) {
return repository
.saveAll(newComment)
.flatMap(comment -> {
meterRegistry
.counter("comments.consumed", "imageId", comment.getImageId())
.increment();
return Mono.empty();
});
}
CommentService.java-> @Service
@EnableBinding(Processor.class)
public class CommentService { ....
Repository which I cloned Chapter7/part 1