由于担心添加一堆组件会使您的应用程序设置复杂化,我可能会考虑一个快速而肮脏的纯 Java 解决方案。
这是我使用 Spring 的 RestTemplate 想出的一些有趣的东西。如果您对可以封装方法调用的拦截器、方面和其他事物感到满意,则可以应用这些原则来封装所有各种 RestTemplate REST 调用。请参阅RestTemplate javadoc
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.*;
import org.springframework.web.util.UriTemplate;
import org.springframework.web.util.UriUtils;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
public class Stuff {
// Make this configurable.
Collection<String> serverList;
// or do something a little smarter, like this interface. I'll use this in this example.
ServerLookup serverLookup;
interface ServerLookup {
Iterator<String> getValidServerListIterator();
void markUnreachableServer(String url);
}
// Do it externally around RestTemplate...
@Test
public void testNormalRestTemplate() throws Exception {
RestTemplate restTemplate = new RestTemplate();
Iterator<String> serverIterator = serverLookup.getValidServerListIterator();
while (serverIterator.hasNext()) {
String server = serverIterator.next();
try {
Object obj = restTemplate.getForObject(server + "/objectIdentifier/511", Object.class);
break;
} catch (ResourceAccessException e) {
serverLookup.markUnreachableServer(server);
}
}
}
// or you can try to 'enhance' RestTemplate to contain the retry logic within. It's a bit hacky, but more fun.
@Test
public void testMyRestTemplate() {
RestTemplate rt = new MyRestTemplate();
Object obj = rt.getForObject("/objectIdentifier/511", Object.class);
rt.delete("/objectIdentifier/511");
}
// Here's a way to (hackily) augment RestTemplate with retry functionality
class MyRestTemplate extends RestTemplate {
// Unfortunately RestTemplate probably wasn't designed for much extensibility. URI objects can't be made from
// URL fragments, so these two methods are the 'furthest in' that we can override and cover all RestTemplate
// REST methods.
@Override
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException {
Iterator<String> serverIterator = serverLookup.getValidServerListIterator();
while (serverIterator.hasNext()) {
String server = serverIterator.next();
// prefix the URL fragment passed in with a server
String fullUrl = server + url;
UriTemplate uriTemplate = new HttpUrlTemplate(fullUrl);
URI expanded = uriTemplate.expand(urlVariables);
try {
return doExecute(expanded, method, requestCallback, responseExtractor);
} catch (ResourceAccessException e) {
serverLookup.markUnreachableServer(server);
}
}
throw new RuntimeException("Unable to reach any servers in the server list for " + url);
}
@Override
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables) throws RestClientException {
Iterator<String> serverIterator = serverLookup.getValidServerListIterator();
while (serverIterator.hasNext()) {
String server = serverIterator.next();
// prefix the URL fragment passed in with a server
String fullUrl = server + url;
UriTemplate uriTemplate = new HttpUrlTemplate(fullUrl);
URI expanded = uriTemplate.expand(urlVariables);
try {
return doExecute(expanded, method, requestCallback, responseExtractor);
} catch (ResourceAccessException e) {
serverLookup.markUnreachableServer(server);
}
}
throw new RuntimeException("Unable to reach any servers in the server list for " + url);
}
/** Exact duplicate of the inner class of RestTemplate. Can not touch privates. */
class HttpUrlTemplate extends UriTemplate {
public HttpUrlTemplate(String uriTemplate) {
super(uriTemplate);
}
@Override
protected URI encodeUri(String uri) {
try {
String encoded = UriUtils.encodeHttpUrl(uri, "UTF-8");
return new URI(encoded);
}
catch (UnsupportedEncodingException ex) {
// should not happen, UTF-8 is always supported
throw new IllegalStateException(ex);
}
catch (URISyntaxException ex) {
throw new IllegalArgumentException("Could not create HTTP URL from [" + uri + "]: " + ex, ex);
}
}
}
}
}