Spring Cloud FeignClient fallbackFactory配置详解

一般FeignClient需要指定一个fallbackFactory或者fallback,一个一个接口的实现然后返回通用的错误代码有些重复工作。所以下面通过Java代理的方式来创建一个统一的fallbackFactory

首先是@FeignClient接口


/**
 * Spring Cloud API 类
 */
@FeignClient(value = "hws-srv-wrk",fallbackFactory = ServiceWorkApiFallback.class)
public interface IServiceWorkApi {

    /**********<Rzjl>XGeneratorCodeStart**********/
    @PostMapping("/rzjl/add")
    Boolean rzjlAdd(@RequestBody Rzjl entity);
    @PostMapping("/rzjl/addBatch")
    Boolean rzjlAddBatch(@RequestBody List<Rzjl> entityList);
    @PostMapping("/rzjl/deleteById/{id}")
    Boolean rzjlDeleteById(@PathVariable Serializable id);
    @PostMapping("/rzjl/deleteByIds")
    Boolean rzjlDeleteByIds(@RequestBody List<Serializable> idList);
    @PostMapping("/rzjl/updateById")
    Boolean rzjlUpdateById(@RequestBody Rzjl entity);
    @PostMapping("/rzjl/updateBatchById")
    Boolean rzjlUpdateBatchById(@RequestBody List<Rzjl> entityList);
    @PostMapping("/rzjl/findById/{id}")
    Rzjl rzjlFindById(@PathVariable Serializable id);
    @PostMapping("/rzjl/findByIds")
    List<Rzjl> rzjlFindByIds(@RequestBody List<Serializable> idList);
    @PostMapping("/rzjl/findAll")
    List<Rzjl> rzjlFindAll();
	@PostMapping("/rzjl/findList")
    List<Rzjl> rzjlFindList(@RequestBody SelectFilter<Rzjl> selectFilter);
	@PostMapping("/rzjl/findPage")
    Page<Rzjl> rzjlFindPage(@RequestBody Paging<Rzjl> paging);
    /**********<Rzjl>XGeneratorCodeEnd**********/

	/**********<Xlxw>XGeneratorCodeStart**********/
    @PostMapping("/xlxw/add")
    Boolean xlxwAdd(@RequestBody Xlxw entity);
    @PostMapping("/xlxw/addBatch")
    Boolean xlxwAddBatch(@RequestBody List<Xlxw> entityList);
    @PostMapping("/xlxw/deleteById/{id}")
    Boolean xlxwDeleteById(@PathVariable Serializable id);
    @PostMapping("/xlxw/deleteByIds")
    Boolean xlxwDeleteByIds(@RequestBody List<Serializable> idList);
    @PostMapping("/xlxw/updateById")
    Boolean xlxwUpdateById(@RequestBody Xlxw entity);
    @PostMapping("/xlxw/updateBatchById")
    Boolean xlxwUpdateBatchById(@RequestBody List<Xlxw> entityList);
    @PostMapping("/xlxw/findById/{id}")
    Xlxw xlxwFindById(@PathVariable Serializable id);
    @PostMapping("/xlxw/findByIds")
    List<Xlxw> xlxwFindByIds(@RequestBody List<Serializable> idList);
    @PostMapping("/xlxw/findAll")
    List<Xlxw> xlxwFindAll();
	@PostMapping("/xlxw/findList")
    List<Xlxw> xlxwFindList(@RequestBody SelectFilter<Xlxw> selectFilter);
	@PostMapping("/xlxw/findPage")
    Page<Xlxw> xlxwFindPage(@RequestBody Paging<Xlxw> paging);
    /**********<Xlxw>XGeneratorCodeEnd**********/

}

创建一个类实现FallbackFactory接口
@Component
public class ServiceAdminCenterFallback implements FallbackFactory<IServiceAdminCenterAPI>, Serializable {

    private IServiceAdminCenterAPI service;

    public ServiceAdminCenterFallback() {
        this.service = (IServiceAdminCenterAPI) GlobalInvocationHandler.newInstance(new Class[]{ IServiceAdminCenterAPI.class });
    }

    @Override
    public IServiceAdminCenterAPI create(Throwable throwable) {
        return this.service;
    }

}

代理创建接口工具GlobalInvocationHandler
隐藏内容,登录后查阅







 

评论区域