gateway 调用 feign

2022-09-04  本文已影响0人  川流不息attitude

gateway 使用 webflux 异步编程,所以不能直接 feign

CompletableFuture<R> completableFuture = CompletableFuture.supplyAsync(()->{
            // 这里调用 就行
        },gateWayTaskExecutor);

R result = completableFuture.get();

添加 消息转换 配置

    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }
拦截符合条件 响应消息
private Mono<Void> response(String msg ,ServerWebExchange exchange) {
        R<Object> r = R.failed(msg);
        exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(JSONObject.toJSONString(r).getBytes(StandardCharsets.UTF_8));
        return exchange.getResponse().writeWith(Flux.just(buffer));
    }
上一篇下一篇

猜你喜欢

热点阅读