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<Boolean> saveJob(@RequestBody SysJob sysJob);
/**
* 启动定时任务
* @param job 定时任务对象
*/
@PostMapping("/job/run")
void runJob(@RequestBody SysJob job);
/**
* 修改定时任务装态
* @param job 定时任务对象
* @return 结果
*/
@PostMapping("/job/changeStatus")
R<Boolean> changeStatus(@RequestBody SysJob job);
/**
* 修改定时任务表达式
* @param job 定时任务对象
* @return 结果
*/
@PutMapping("/job")
R<Boolean> editCron(@RequestBody SysJob job);
/**
* 删除Job
* @param jobIds 要删除的定时任务ID
* @return 结果
*/
@DeleteMapping("/job/{jobIds}")
R<Boolean> removeJob(@PathVariable("jobIds") Long[] jobIds);
/**
* 查询定时任务
* @param jobId 定时任务Id
* @return SysJob
*/
@GetMapping(value = "/job/{jobId}")
R<SysJob> getInfo(@PathVariable("jobId") Long jobId);
@GetMapping("/job/list")
List<SysJob> list(@RequestBody SysJob sysJob);
}
创建分级处理工厂
/**
* RemoteJobFallbackFactory
*
* @author jgn
* @date 2021/9/16
*/
@Component
public class RemoteJobFallbackFactory implements FallbackFactory<RemoteJobService> {
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<Boolean> saveJob(SysJob sysJob) {
return null;
}
@Override
public void runJob(SysJob job) {
}
@Override
public R<Boolean> changeStatus(SysJob job) {
return null;
}
@Override
public R<Boolean> editCron(SysJob job) {
return null;
}
@Override
public R<Boolean> removeJob(Long[] jobIds) {
return null;
}
@Override
public R<SysJob> getInfo(Long jobId) {
return null;
}
@Override
public List<SysJob> 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<SysJob> list(@RequestBody SysJob sysJob);
参考文章:
http://doc.ruoyi.vip/ruoyi-cloud/cloud/feign.html
https://cloud.tencent.com/developer/article/1497809