[英]Fallback service spring Boot
我們執行運行一個API來下載具有以下api的網上論壇文件:curl -X POST localhost:port / manager / createTask Rest API:(manager)
@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;
}
}
}
DownloadService類:
@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);
}
當api被調用時,我們將管理器的公共2微服務稱為Manager1,Manager2,然后假定:假設它將被manager1使用。
如果微服務管理器1在接收太多請求任務時崩潰了。
問題:如何使manager2知道進度服務manager1繼續執行createTask? 示例:Manager1在崩潰之前已完成3個步驟(下載和設置任務)。
您應該嘗試進行功能區負載平衡以在服務的Manager1和Manager2實例之間進行平衡。 同樣,manager2也無法在兩者之間接管manager1的任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.