报错信息

java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3
	at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.4.15.jar:3.4.15]
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
	*__checkpoint ⇢ HTTP GET "/get" [ExceptionHandlingWebHandler]

版本:
springcloud alibaba 2021.0.1.0
springboot 2.7.0
gateway 3.1.1
openfeign 3.1.1
还原场景
网关服务通过openfeign调用授权服务

授权服务

    @GetMapping("/get")
    public String openFeignApi(){
        return "asdgwe";
    }

网关服务

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServerApplication.class, args);
    }

}
@Component
@FeignClient(value = "oauth")
public interface OauthFeign {
    @GetMapping("/get")
    String openFeignApi();
}

controller

 @Resource
    OauthFeign oauthFeign;
    
    @GetMapping("/get")
    public Object get() {
  
        return oauthFeign.openFeignApi();
               
    }

此时请求网关的/get就会报上面的错误。
需要添加一些配置

  1. 添加HttpMessageConverters的bean
@Configuration
public class HttpMsgConverConfig {

    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }
}
  1. 异步调用openfeign接口,修改controller
 @Resource
    OauthFeign oauthFeign;


    @GetMapping("/get")
    public Object get() {

        CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync(() -> {
                    return oauthFeign.openFeignApi();
                });
        return completableFuture;
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐