2021-09-24 ruoyi框架中使用feign自定义服务,fegin使用遇到的坑

ruoyi框架中使用feign自定义服务

创建RemoteJobService使用fegin对远程服务进行代理

/**
 * RemoteJobService
 * 定时任物服务
 *
 * @author jgn
 * @date 2021/9/16
 */
@FeignClient(contextId = "remoteJobService", value = ServiceNameConstants.JOB_SERVICE,fallbackFactory = RemoteJobFallbackFactory.class)
public interface RemoteJobService {
    /**
     * 添加定时任务
     * @param sysJob 定时任务对象
     * @return 结果
     */
    @PostMapping("/job")
    R saveJob(@RequestBody SysJob sysJob);

    /**
     * 启动定时任务
     * @param job 定时任务对象
     */
    @PostMapping("/job/run")
    void runJob(@RequestBody SysJob job);

    /**
     * 修改定时任务装态
     * @param job 定时任务对象
     * @return 结果
     */
    @PostMapping("/job/changeStatus")
    R changeStatus(@RequestBody SysJob job);

    /**
     * 修改定时任务表达式
     * @param job 定时任务对象
     * @return 结果
     */
    @PutMapping("/job")
    R editCron(@RequestBody SysJob job);

    /**
     * 删除Job
     * @param jobIds 要删除的定时任务ID
     * @return 结果
     */
    @DeleteMapping("/job/{jobIds}")
    R  removeJob(@PathVariable("jobIds") Long[] jobIds);

    /**
     * 查询定时任务
     * @param jobId 定时任务Id
     * @return SysJob
     */
    @GetMapping(value = "/job/{jobId}")
    R getInfo(@PathVariable("jobId") Long jobId);

    @GetMapping("/job/list")
    List list(@RequestBody SysJob sysJob);
}

创建分级处理工厂

/**
 * RemoteJobFallbackFactory
 *
 * @author jgn
 * @date 2021/9/16
 */
@Component
public class RemoteJobFallbackFactory implements FallbackFactory {
    private static final Logger log = LoggerFactory.getLogger(RemoteJobFallbackFactory.class);
    @Override
    public RemoteJobService create(Throwable throwable) {
        log.error("定时器服务调用失败:{}", throwable.getMessage());
        return new RemoteJobService() {
            @Override
            public R saveJob(SysJob sysJob) {
                return null;
            }

            @Override
            public void runJob(SysJob job) {

            }

            @Override
            public R changeStatus(SysJob job) {
                return null;
            }

            @Override
            public R editCron(SysJob job) {
                return null;
            }

            @Override
            public R removeJob(Long[] jobIds) {
                return null;
            }

            @Override
            public R getInfo(Long jobId) {
                return null;
            }

            @Override
            public List list(SysJob sysJob) {
                return null;
            }
        };
    }

}

坑一:spring.factories装配加载,不进行此操作可能会导致降级处理工厂无法使用

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.ruoyi.system.api.factory.RemoteUserFallbackFactory,\
  com.ruoyi.system.api.factory.RemoteLogFallbackFactory,\
  com.ruoyi.system.api.factory.RemoteJobFallbackFactory

坑二: spring cloud feign微服务调用报错Request method 'POST' not supported
调用@GetMapping类的请求要对形参加入@RequestBody

  @GetMapping("/job/list")
    List list(@RequestBody SysJob sysJob);

参考文章:
http://doc.ruoyi.vip/ruoyi-cloud/cloud/feign.html
https://cloud.tencent.com/developer/article/1497809

你可能感兴趣的:(2021-09-24 ruoyi框架中使用feign自定义服务,fegin使用遇到的坑)