【发布时间】:2018-05-05 10:25:58
【问题描述】:
在 How to create a Spring Reactor Flux from Http integration flow?artem-bilan mentioned in a comment 中表示将来可以使用 webflux 集成。
自从写评论以来,WebFlux 集成一直是factored out to spring-integration-webflux。我尝试了以下方法,通过将 MVC 版本的 Http.inboundChannelAdapter 和 @GetRequest 处理程序替换为 WebFlux.inboundChannelAdapter 和 WebFlux.inboundGateway 来复制基于 MVC 的 http->flux 集成与基于 WebFlux 的集成:
@SpringBootApplication
public class WebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(WebfluxApplication.class, args);
}
@Bean
public Publisher<Message<String>> reactiveSource() {
return IntegrationFlows.
from(WebFlux.inboundChannelAdapter("/message/{id}")
.requestMapping(r -> r
.methods(HttpMethod.POST)
)
.payloadExpression("#pathVariables.id")
)
.log()
.channel(MessageChannels.flux())
.toReactivePublisher();
}
@Bean
public IntegrationFlow eventMessages() {
return IntegrationFlows
.from(WebFlux.inboundGateway("/events")
.requestMapping(m -> m.produces(MediaType.TEXT_EVENT_STREAM_VALUE)))
.handle((p, h) -> reactiveSource())
.get();
}
}
reactiveSource() 发布者中的流似乎没有收到任何消息,至少我的.log() 语句没有记录任何消息。
当我替换 eventMessages 流中的 reactiveSource() 发布者时
.handle((p, h) -> reactiveSource())
假出版商
.handle((p, h) -> Flux.just("foo", "bar"))
我收到了来自
的 SSE 回复curl localhost:8080/events
跟踪日志显示reactiveSource() POST 处理程序已映射并且正在调用WebFluxInboundEndpoint.handle 方法:
2018-05-05 16:50:58.788 INFO 6552 --- [ main] xIntegrationRequestMappingHandlerMapping : Mapped "{[/message/{id}],methods=[POST]}" onto public abstract reactor.core.publisher.Mono<java.lang.Void> org.springframework.web.server.WebHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-05-05 16:50:58.789 INFO 6552 --- [ main] xIntegrationRequestMappingHandlerMapping : Mapped "{[/events],methods=[GET || POST],produces=[text/event-stream]}" onto public abstract reactor.core.publisher.Mono<java.lang.Void> org.springframework.web.server.WebHandler.handle(org.springframework.web.server.ServerWebExchange)
2018-05-05 16:50:59.191 INFO 6552 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2018-05-05 16:50:59.192 INFO 6552 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2018-05-05 16:50:59.196 INFO 6552 --- [ main] d.e.sample.webflux.WebfluxApplication : Started WebfluxApplication in 2.608 seconds (JVM running for 3.419)
2018-05-05 16:51:06.918 DEBUG 6552 --- [ctor-http-nio-2] o.s.web.reactive.DispatcherHandler : Processing POST request for [http://localhost:8080/message/4]
2018-05-05 16:51:06.932 DEBUG 6552 --- [ctor-http-nio-2] s.w.r.r.m.a.RequestMappingHandlerMapping : Looking up handler method for path /message/4
2018-05-05 16:51:06.933 DEBUG 6552 --- [ctor-http-nio-2] s.w.r.r.m.a.RequestMappingHandlerMapping : Did not find handler method for [/message/4]
2018-05-05 16:51:06.967 TRACE 6552 --- [ctor-http-nio-2] o.s.w.r.r.method.InvocableHandlerMethod : Invoking 'org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint.handle' with arguments [org.springframework.web.server.adapter.DefaultServerWebExchange@775cdb20]
2018-05-05 16:51:06.967 TRACE 6552 --- [ctor-http-nio-2] o.s.w.r.r.method.InvocableHandlerMethod : Method [org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint.handle] returned [MonoDefer]
2018-05-05 16:51:06.967 DEBUG 6552 --- [ctor-http-nio-2] o.s.w.r.r.method.InvocableHandlerMethod : Response fully handled in controller method
2018-05-05 16:51:11.363 DEBUG 6552 --- [ctor-http-nio-3] o.s.web.reactive.DispatcherHandler : Processing POST request for [http://localhost:8080/message/4]
2018-05-05 16:51:11.363 DEBUG 6552 --- [ctor-http-nio-3] s.w.r.r.m.a.RequestMappingHandlerMapping : Looking up handler method for path /message/4
2018-05-05 16:51:11.363 DEBUG 6552 --- [ctor-http-nio-3] s.w.r.r.m.a.RequestMappingHandlerMapping : Did not find handler method for [/message/4]
2018-05-05 16:51:11.364 TRACE 6552 --- [ctor-http-nio-3] o.s.w.r.r.method.InvocableHandlerMethod : Invoking 'org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint.handle' with arguments [org.springframework.web.server.adapter.DefaultServerWebExchange@71f648a3]
2018-05-05 16:51:11.364 TRACE 6552 --- [ctor-http-nio-3] o.s.w.r.r.method.InvocableHandlerMethod : Method [org.springframework.integration.webflux.inbound.WebFluxInboundEndpoint.handle] returned [MonoDefer]
2018-05-05 16:51:11.364 DEBUG 6552 --- [ctor-http-nio-3] o.s.w.r.r.method.InvocableHandlerMethod : Response fully handled in controller method
这是为什么呢?
【问题讨论】:
-
在编辑时,我尝试分配一个 stackoverflow spring-integration-webflux 标签,但它还不存在。也许有足够声誉的人可以创造它。
-
如果你这样做,它是如何工作的:
.habdle((p, h) -> Flux.from(reactiveSource()))? -
不幸的是,我得到了相同的结果:reactiveSource() 流中没有日志输出,/events 资源中没有输出。我添加了显示调试日志具有误导性的跟踪日志:Webflux 入站端点实际上确实被调用了。
-
当 POST 没有正文时,WebFluxInboundEndpoint 中的处理似乎停止了。
标签: spring-integration spring-webflux