feigin使用需要注意的一些问题 |
您所在的位置:网站首页 › excel函数输入时应该注意哪些细节问题和方法 › feigin使用需要注意的一些问题 |
小谈feign
feign和ribbon有什么不同? 首先Ribbon和Feign都是用于调用其他服务的,只不过方式不同而已!ribbon是对resttemplate进行封装,而feign是对ribbon进行了封装,集成了所有ribbon有的功能,所以feign是有负载均衡功能的,它来自ribbon。 具体不一样的地方: 启动类使用的注解不同,Ribbon用的是@RibbonClient,Feign用的是@EnableFeignClients。 服务的指定位置不同,Ribbon是在@RibbonClient注解上声明,Feign则是在定义抽象方法的接口中使用@FeignClient声明。 调用方式不同,Ribbon需要自己构建http请求,模拟http请求然后使用RestTemplate发送给其他服务,步骤相当繁琐,Feign则是在Ribbon的基础上进行了一次改进,采用接口的方式,将需要调用的其他服务的方法定义成抽象方法即可,不需要自己构建http请求。不过要注意的是抽象方法的注解、方法签名要和提供服务的方法完全一致。 有人说feign一定要和euraka一起使用,否则就用不了,这句话只说对了一点,feign不一定需要依赖euraka,它可以单独的进行使用,但是结合euraka 它的强大之处才会体现出来,负载均衡,熔断、等等。 使用feign需要注意问题! 1、feigin客户端无法多继承只允许单继承接口 如需要解决代码冗余只能单继承 所以尽量继承接口多的那个接口 public interface SlideshowtimeInterface { @RequestMapping(value = "/slideshowtime/updateTime", method = RequestMethod.POST) public Map updatetime(@RequestBody(required = false)String json)throws UnsupportedEncodingException; @RequestMapping(value = "/slideshowtime/loadtime", method = RequestMethod.POST) public Map loadtime(@RequestBody(required = false)String json)throws UnsupportedEncodingException; } public interface PictureInterface { @RequestMapping(value = "/picture/initializeAdPosition", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public Map initializeAdPosition(@RequestBody(required = false)String json); @RequestMapping(value = "/picture/saveOrUpdate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public Map save(@RequestBody(required = false)String json); @RequestMapping(value = "/picture/load", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public Map load(@RequestBody( required = false)String json); @RequestMapping(value = "/picture/queryBannerOfproduct", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public Map queryBannerOfproduct(@RequestBody( required = false)String json); @RequestMapping(value = "/picture/queryBannerOfproducts", method = RequestMethod.POST, produces = "application/json;charset=UTF-8") public Map queryBannerOfproducts(@RequestBody( required = false)String json); @RequestMapping(value = "/picture/move", method = {RequestMethod.POST}) public Map move(@RequestBody(required = false)String json); @RequestMapping(value = "/picture/delete", method = RequestMethod.POST) public Map delete(@RequestBody(required = false)String json); @RequestMapping(value = "/picture/isEffect", method = RequestMethod.POST) public Map isEffect(@RequestBody(required = false)String json); }错误写法 调用方书写 @FeignClient(value = "djslideshow-server",path = "/djslideshowserver" ) public interface FeiginClient extends PictureInterface,SlideshowtimeInterface{ }Caused by: java.lang.IllegalStateException: Only single inheritance supported: PayfeiginClient 正确写法 调用方书写 @FeignClient(value = "djslideshow-server",path = "/djslideshowserver" ) public interface FeiginClient extends PictureInterface { @RequestMapping(value = "/slideshowtime/updateTime", method = RequestMethod.POST) public Map updatetime(@RequestBody(required = false)String json)throws UnsupportedEncodingException; @RequestMapping(value = "/slideshowtime/loadtime", method = RequestMethod.POST) public Map loadtime(@RequestBody(required = false)String json)throws UnsupportedEncodingException; } 为什么?原因在于 接口是可以对接口进行继承的 但是spring对于使用@feiginClient 注解修饰的接口会对其进行实现并注入bean 其就相当于是一个类 类是不允许多继承的 而且 不建议继承这种服务器端和客户端之间共享接口方式,因为这种方式会造成服务器端和客户端代码的紧耦合。并且,Feign本身并不使用Spring MVC的工作机制(方法参数映射不被继承) 2、feigin调用的请求类型以及参数。 当参数比较复杂时,feign即使声明为get请求也会强行使用post请求不支持@GetMapping类似注解声明请求,需使用@RequestMapping(value = “url”,method = RequestMethod.GET)使用@RequestParam注解时必须要在后面加上参数名 TbItem对象,使用@RequestBody来声明String字符串,使用@RequestParam(“xxx”)来声明 @PostMapping(value = "bankCard/judgeCardFroPreNumber") public HttpResult judgeCardPreNumber(@RequestParam(value = "faccountNumber") String faccountNumber);如果不加 则报错 feign.FeignException: status 500 reading PayfeiginClient#judgeCardPreNumber(String) 3、feigin调用的方法建议不要用 多实体参数 因为不支持 如果一定需要 你要对方法进行重写编码器修饰包装再使用feignSpringCloud Feign重写编码器支持pojos多实体与文件数组参数传递的方法 请看博客 :https://blog.csdn.net/qq_34523427/article/details/88863800 4、feigin的调用方与被调用方都必须在同一个注册中心 并且被调用方一定要先启动 当然后启动也行 不过需要等待30秒eureka 注册中心对被调用服务进行心跳机制判断毕竟feigin适合euraka注册中心混合使用的,如果单独提出feign那边不是那么很有意义了。但feign依旧集成了ribbon的 底层是rest调用 所以并非feigin离开eureka就无法使用 而是只能充当restHttp使用 也就是说他不高大上了 而且使用时需要注明url 接口名 相关使用 看feign源码:
当实现feigin时会出现以下错误: The bean ‘pay-server.FeignClientSpecification’, defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled. 无法注册以null定义的bean“pay-server / djpay.FeignClientSpecification”。 具有该名称的bean已在null中定义,并且已禁用覆盖。 6、针对服务提供方的入参没有添加@requestParam、@requestBody 等注解时 那么请求方能用什么方式进行feign调用呢?例:加入下面方法是被调用方法 那么这种方法一般只能使用form表单形式进行请求,那么问题来了——如何把入参注入呢? 方法2、使用map传参 方法3、采用form表单 独有的LinkedMultiValueMap 参数进行提交 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |