关于通过ApplicationContext上下文无法获取到feignClient的类
我实在gateway网关服务做的处理
如果上述的方法无法获取到feignClient的类
报以下错:
No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
feign.codec.EncodeException: No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方案:
在对应的服务新增配置
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import java.util.stream.Collectors;
@Configuration
public class GatewayFeignConfig {
@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
}
}
接着会发现又报另一个错误:
block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3
因为gateway网关Feign调用微服务异常,spring boot 2.7.0 WebFlux必须使用异步调用,同步会报错
解决方案:
改为异步
{
//异步调用
CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync(() -> {
return xxxFeign.method();
});
//从completableFuture中取出返回值即可
XXXVo xxxVo = (XXXVo) completableFuture.get();
//业务操作
int xxx = xxxVo.getXXX();
}