【问题标题】:Fallback service spring Boot后备服务 spring Boot
【发布时间】:2019-03-18 12:59:16
【问题描述】:

我们使用下面的 api 执行运行 API 来下载组文件: curl -X POST localhost:port/manager/createTask 休息 API:(经理)

@RestController
@RequestMapping("/manager")
public class ClusterResource {
    @Autowired
    private MyService myService;
    @PostMapping("/createTask")
    @ResponseBody
    public ResponseEntity doTasks() throws Exception {
        myService.doTask();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

MyService 类:

@Service
public class MyService
    private String templateFileName = "example";
    @Autowired
    private DownloadService downloadService;
    @Async
    public ResponseTaskDto doTasks() throws Exception {
         ResponseTaskDto result = new ResponseTaskDto();
         int downloadFlag;
         int taskFlag;
         for (int i = 0; i < 10; i ++) {
             if (doTask(i)) {
                 downloadFlag |= i; // downloaded file
                 result.setDownloadFlag(downloadFlag);
                 // execute some code to move file downloaded to correct location
                 // Execute setup proxy network, rollout services use resource file
                 taskFlag |= i; // setup done task
                 result.setTaskFlag(taskFlag);
             }
         }
         return clusterResult;
    }
    private boolen doTask(int number) throws Exception {
        String file = new String(templateFileName + number + ".mp4");
        try {
            downloadService.downloadFile(file);
        } catch (Exception ex) {
            // download service work well. can not here
            //throw ex;
            return false; // download filed: invalid url, ...
        }
        return true;
    }
   public class ResponseTaskDto {
        int downloadFlag;
        int taskFlag;
        public void setDownloadFlag(int flag) {
            this.downloadFlag = flag;
        }
        public int getDownloadFlag() {
            return this.downloadFlag;
        }
        public void setTaskFlag(int flag) {
            this.taskFlag = flag;
        }
        public int getTaskFlag() {
            return this.taskFlag;
        }
   }
}

下载服务类:

@FeignClient(value = "http://downloader")
public interface DownloadService {
    @RequestMapping(value = "/downloader/api/", method = RequestMethod.POST, produces = {
            MediaType.APPLICATION_JSON_VALUE })
    void downloadFile(@PathVariable("file") String fileName);
}

我们是manager1的public 2 microservice,Manager2 调用 api 时假设:假设它会被 manager1 使用。

如果微服务管理器1在接收到过多的请求任务时崩溃了。

问题: 如何让 manager2 知道进度服务 manager1 继续 createTask ? 示例:Manager1 在崩溃之前已经完成了 3 个步骤(下载和设置任务)。

【问题讨论】:

    标签: java spring-boot failover fallback


    【解决方案1】:

    您应该尝试功能区负载平衡来平衡服务的 Manager1 和 Manager2 实例。此外,manager2 也无法在两者之间接取 manager1 的任务。

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2021-03-15
      • 2018-07-05
      • 2018-10-25
      • 2018-07-21
      • 1970-01-01
      相关资源
      最近更新 更多