Spring Boot + Webflux:并行运行某些步骤时上下文丢失

时间:2019-04-10 07:13:50

标签: java spring-boot spring-webflux project-reactor

春季启动:2.1.3。发布

你好,

我正在尝试使用spring webflux的上下文功能来携带一个简单的变量。我有一个WebFilter用这样的变量设置上下文,我尝试在通量/流的不同阶段在控制器中使用它。在某些时候,在调用Flux类的“ parallel()”方法之后,我会丢失它。

  • 过滤器:
public class TestFilter implements WebFilter {

    private Logger LOG = LoggerFactory.getLogger(TestFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        return chain.filter(exchange)
            .doOnEach(voidSignal -> System.out.println("filter:"+voidSignal.getContext().getOrEmpty("blob"))).subscriberContext(Context.of("blob", "kapoue"));
    }

}
  • 控制器:
@RestController
@RequestMapping(TestControllerWebFlux.ROOT)
public class TestControllerWebFlux {

    static final String ROOT = "/flux";
    static final String TEST = "/test";

    private WebClient webClient = WebClient.create();

    @GetMapping(
            value = TEST,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public Mono<String> test() {
        System.out.println("controller1:"+Thread.currentThread());

        Flux<String> call = webClient.get().uri("http://localhost:" + 8080 + ROOT + "/test2").retrieve().bodyToFlux(Result.class).map(Result::getValue);

        return call.map(s -> s+"0")
            .doOnEach(stringSignal -> System.out.println("controller2:"+stringSignal.getContext().getOrEmpty("blob")))
            .parallel()
            .doOnEach(stringSignal -> System.out.println("controller3:"+stringSignal.getContext().getOrEmpty("blob")))
            .map(s -> s+"0")
            .doOnEach(stringSignal -> System.out.println("controller4:"+stringSignal.getContext().getOrEmpty("blob")))
            .reduce((s, s2) -> s+s2)
            .doOnEach(stringSignal -> System.out.println("controller5:"+stringSignal.getContext().getOrEmpty("blob")))
            .map(s -> {
                System.out.println("controller6:"+Thread.currentThread());
                return s;
            });
    }

    @GetMapping(
        value = "test2",
        produces = {MediaType.APPLICATION_JSON_VALUE})
    public Flux<Result> test2() {
        return Flux.just(new Result("0"), new Result("0"), new Result("0"));
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Result {
        private String value;
    }
}

我所做的只是呼叫http://localhost:8080/flux/test/端点,我明白了:

  

controller1:Thread [reactor-http-nio-2,5,main]   controller2:可选[kapoue]   controller3:Optional.empty   controller4:Optional.empty   controller2:可选[kapoue]   controller3:Optional.empty   controller4:Optional.empty   controller2:可选[kapoue]   controller3:Optional.empty   controller4:Optional.empty   controller2:可选[kapoue]   controller3:Optional.empty   controller4:Optional.empty   controller3:Optional.empty   controller4:Optional.empty   controller3:Optional.empty   controller4:Optional.empty   controller3:Optional.empty   controller4:Optional.empty   controller5:可选[kapoue]   controller6:线程[reactor-http-nio-2,5,main]   过滤器:可选[kapoue]

如您所见,上下文在“并行”方法之后立即丢失,而归约后又以某种方式返回。

是bug还是我不应该在这样的调用之后尝试并行运行事物?

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这看起来像是Reactor中的错误。我报告了它:https://github.com/reactor/reactor-core/issues/1656