Spring 面对io.netty.handler.TIMEOUTE.ReadTimeoutException:使用服务器发送的事件时为null
我是SpringWebFlux的新手,我有一个客户端应用程序,它使用服务器发送的事件,这些事件由服务器随机发布,没有固定的延迟。但是如果没有事件,消费者在60秒后抛出Spring 面对io.netty.handler.TIMEOUTE.ReadTimeoutException:使用服务器发送的事件时为null,spring,spring-boot,spring-webflux,project-reactor,reactor-netty,Spring,Spring Boot,Spring Webflux,Project Reactor,Reactor Netty,我是SpringWebFlux的新手,我有一个客户端应用程序,它使用服务器发送的事件,这些事件由服务器随机发布,没有固定的延迟。但是如果没有事件,消费者在60秒后抛出io.netty.handler.timeout.ReadTimeoutException:null 服务器端事件使用者代码 webClient.get() .uri("http://localhost:8080/events") .accept(MediaType.TEXT_EV
io.netty.handler.timeout.ReadTimeoutException:null
服务器端事件使用者代码
webClient.get()
.uri("http://localhost:8080/events")
.accept(MediaType.TEXT_EVENT_STREAM)
.retrieve()
.bodyToFlux(type)
.subscribe(event -> process(event));
我需要连接客户端,即使长时间没有事件
完全例外
[36mr.netty.http.client.HttpClientConnect [...] The connection observed an error
io.netty.handler.timeout.ReadTimeoutException: null
reactor.Flux.MonoFlatMapMany.1 onError(org.springframework.web.reactive.function.client.WebClientRequestException: nested exception is io.netty.handler.timeout.ReadTimeoutException)
reactor.Flux.MonoFlatMapMany.1
org.springframework.web.reactive.function.client.WebClientRequestException: nested exception is io.netty.handler.timeout.ReadTimeoutException
at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141) ~[spring-webflux-5.3.5.jar:5.3.5]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
报告中有一个注释:
冒号作为行的第一个字符本质上是注释,并且
被忽略了注意:注释行可用于防止连接
从超时;服务器可以定期发送注释以保持
连接处于活动状态
因此,定期发送注释可以使连接保持活动状态。那么我们如何发送评论呢
嗯,spring有一个具有函数的类。因此,如果我们结合使用这个类,例如,我们可以合并只包含注释的事件keep-alive
下面是我不久前创建的一个项目的示例
@Bean
public RouterFunction<ServerResponse> foobars() {
return route()
.path("/api", builder -> builder
.GET("/foobar/{id}", accept(TEXT_EVENT_STREAM), request -> ok()
.contentType(MediaType.TEXT_EVENT_STREAM)
.header("Cache-Control", "no-transform")
.body(Flux.merge(foobarHandler.stream(request.pathVariable("id")),
Flux.interval(Duration.ofSeconds(15)).map(aLong -> ServerSentEvent.<List<FoobarResponse>>builder()
.comment("keep alive").build())), new ParameterizedTypeReference<ServerSentEvent<List<FoobarResponse>>>(){}))
.build();
}
@Bean
公共路由器功能foobars(){
返回路线()
.path(“/api”,生成器->生成器
.GET(“/foobar/{id}”,接受(文本事件流),请求->确定()
.contentType(MediaType.TEXT\u事件\u流)
.header(“缓存控制”、“无转换”)
.body(Flux.merge(foobarHandler.stream(request.pathVariable(“id”)),
Flux.interval(Duration.ofSeconds(15)).map(沿着->ServerSentEvent.builder()运行)
.comment(“保持活动”).build()),新的参数化类型引用(){})
.build();
}