我有一些服务器可以从我的程序中发出REST请求。他们对特定请求都会有相同的回应。
我接受一个ip
作为参数并向该服务器发出请求。如果我现在希望接受List<ip>
,当一个失败时,如何优雅地切换到列表中的下一个服务器?在每个网络电话上循环播放列表似乎很愚蠢。
不幸的是,我无法使用catch-repeat_to_next_server解压缩到一个接受HttpClient和其他请求的函数的REST调用,因为我使用第三方SDK与服务器通信,每个请求都是方法链调用。
我不能这样做(伪代码):
def doRequest(HttpClient client)
for ip in list_of_ips:
try:
client.host = ip
return client.execute()
catch exp:
// move failed ip to end of list or something
throw "None of them worked"
HttpClient c
c.method = "GET /api/employees"
doRequest(c)
c.method = "GET /api/department/:id"
doRequest(c)
有没有标准方法可以清洁地解决这个问题?
我使用的是Java和Spring,所以我受到静态类型的约束,但可能有一些我可以使用的Spring注释魔法,我不知道。
答案 0 :(得分:1)
如何进行REST调用并不重要。 即使您需要为每个REST调用进行一百万个方法调用,您的伪代码也应该是正确的。
for ip in ip_list
do_stuff_to_make_the_rest_call
perhaps_note_the_ip_that_was_used
indicate_success
break_out_of_for_loop
catch some_exception
perhaps_note_the_ip_that_failed
end for
if !success
do all_ip_failed_stuff.
procedure do_stuff_to_make_the_rest_call
make a million method calls to get one REST call attempt.