美文网首页
使用 SpringCloud OpenFeign 注意事项

使用 SpringCloud OpenFeign 注意事项

作者: 吉他手_c156 | 来源:发表于2020-09-20 17:30 被阅读0次
  • 引入 openFeign 坐标
  • springboot 引导类加上 @EnableFeignClients 开启 Feign 客户端
  • Feign 接口上加上 @FeignClient("服务名称") 注解,且不要使用 @ReqeustMapping("/xxx") 注解定义 URL 前缀
    例如:
@FeignClient("user-service")
@RequestMapping("/xxx")
public interface userFeignClient{
}

如果要写,写在接口方法前面,例如: @GetMapping("/xxx/user"),@PostMapping("/xxx/order")

  • 对象参数必须使用 @RequestBody 注解接受
  • Rest URL 必须使用 @PathVariable("xxx") 注解,且参数名 xxx 不能省略
  • URL 参数 必须使用 @RequestParam("xxx")注解,且参数名 xxx 不能省略

gateway openFeign 调用服务报错:

image.png

No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessage’

  • HttpMessageConvertersAutoConfiguration 类注入有一个条件,@Conditional(NotReactiveWebApplicationCondition.class),意思就是说只有不是基于 Reactive 实现的才会去注入, gateway 是基于 WebFlux 实现的,也就是基于 Reactive 实现的所以不会注入,它不自动注入那么我们就去手动注入
  • 注入解码器:
@SpringBootConfiguration
public class FeignConfig {

    @Bean
    public Decoder feignDecoder() {
        return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
    }

    public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
        HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new MappingJackson2HttpMessageConverter());
        return () -> httpMessageConverters;
    }
}

相关文章

网友评论

      本文标题:使用 SpringCloud OpenFeign 注意事项

      本文链接:https://www.haomeiwen.com/subject/mocpyktx.html