feigin使用需要注意的一些问题

1、feigin无法多继承只允许单继承接口 如需要解决代码冗余只能单继承 所以尽量继承接口多的那个接口

public interface SlideshowtimeInterface {

    @RequestMapping(value = "/slideshowtime/updateTime", method = RequestMethod.POST)
    public Map<String, Object> updatetime(@RequestBody(required = false)String json)throws UnsupportedEncodingException;

    @RequestMapping(value = "/slideshowtime/loadtime", method = RequestMethod.POST)
    public Map<String, Object> 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<String,Object> initializeAdPosition(@RequestBody(required = false)String json);

    @RequestMapping(value = "/picture/saveOrUpdate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String,Object> save(@RequestBody(required = false)String json);

    @RequestMapping(value = "/picture/load", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String, Object> load(@RequestBody( required = false)String json);

    @RequestMapping(value = "/picture/queryBannerOfproduct", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String, Object> queryBannerOfproduct(@RequestBody( required = false)String json);

    @RequestMapping(value = "/picture/queryBannerOfproducts", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String, Object> queryBannerOfproducts(@RequestBody( required = false)String json);

    @RequestMapping(value = "/picture/move", method = {RequestMethod.POST})
    public Map<String, Object> move(@RequestBody(required = false)String json);

    @RequestMapping(value = "/picture/delete", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestBody(required = false)String json);

    @RequestMapping(value = "/picture/isEffect", method = RequestMethod.POST)
    public Map<String, Object> 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<String, Object> updatetime(@RequestBody(required = false)String json)throws UnsupportedEncodingException;

    @RequestMapping(value = "/slideshowtime/loadtime", method = RequestMethod.POST)
    public Map<String, Object> loadtime(@RequestBody(required = false)String json)throws UnsupportedEncodingException;

}
为什么?

原因在于 接口是可以对接口进行继承的 但是spring对于使用@feiginClient 注解修饰的接口会对其进行实现并注入bean 其就相当于是一个类 类是不允许多继承的

而且 不建议继承这种服务器端和客户端之间共享接口方式,因为这种方式会造成服务器端和客户端代码的紧耦合。并且,Feign本身并不使用Spring MVC的工作机制(方法参数映射不被继承)


2、feigin调用的请求类型以及参数。

  1. 当参数比较复杂时,feign即使声明为get请求也会强行使用post请求
  2. 不支持@GetMapping类似注解声明请求,需使用@RequestMapping(value = “url”,method = RequestMethod.GET)
  3. 使用@RequestParam注解时必须要在后面加上参数名
    1. TbItem对象,使用@RequestBody来声明
    2. 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调用的方法最好不要有 多实体参数 因为不支持 如果一定需要 你要对方法进行重写编码器修饰包装

SpringCloud Feign重写编码器支持pojos多实体与文件数组参数传递的方法 请看博客 :https://blog.csdn.net/qq_34523427/article/details/88863800

4、feigin的调用方与被调用方都必须在同一个注册中心 并且被调用方一定要先启动 当然后启动也行 不过需要等待30秒eureka 注册中心对被调用服务进行心跳机制判断

feigin的实现就是依靠eureka的 所以不用说了

5、一个项目里面准确的说时一个服务里面 不允许两个feigin客户端代理同一个服务即使feigin名称不一致

@FeignClient(value = "pay-server/djpay")
public interface PaybankFeiginClient extends BalanceModuleInterface {
}
@FeignClient(name = "pay-server/djpay")
public interface PayfeiginClient extends BankCardModuleInterface {

当实现feigin时会出现以下错误:
The bean ‘pay-server/djpay.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中定义,并且已禁用覆盖。

猜你喜欢

转载自blog.csdn.net/weixin_42083036/article/details/91982930
今日推荐