任何处理REST客户端服务器列表的框架?

时间:2012-07-17 10:04:11

标签: java rest restlet rest-client

这个想法是REST Client可以配置REST服务器列表。因此,服务器列表将以循环方式在REST客户端上轮换。

e.g。 REST客户端应用程序我将配置服务器列表(REST_SERVER1,REST_SERVER2,REST_SERVER3)

1 request -> REST_SERVER1
2 request -> REST_SERVER2
3 request  -> REST_SERVER3
4 request -> REST_SERVER1

我搜索了这么多,我找不到支持此功能的正确框架。

2 个答案:

答案 0 :(得分:6)

我只是打包你的客户端,然后重复请求,直到成功为止。这样你就可以整齐地跟踪哪些服务器正在工作。这是我从我们的系统改编的一些代码......

import java.io.IOException;
import java.net.URL;
import java.util.Calendar;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RestClient {

    private final String mediaType = "application/json";
    private RestServer[] servers = new RestServer[] {new RestServer("server1", 8080), new RestServer("server2", 8080)};

    protected RestClient() {
    }

    protected ClientResponse post(String methodUrl, Object postData) throws IOException {
        return doRequest(methodUrl, postData, true);
    }

    protected ClientResponse get(String methodUrl) throws IOException {
        return doRequest(methodUrl, null, false);
    }

    private ClientResponse doRequest(String methodUrl, Object postData, boolean isPost) throws IOException {

        Client client = Client.create();

        for (RestServer restServer : servers) {

            if (!restServer.shouldTry()) {
                System.out.println(restServer + " not ready");
                continue;
            }

            System.out.println("Trying with " + restServer);

            try {
                URL url = new URL("http", restServer.getHost(), restServer.getPort(), '/' + methodUrl);
                WebResource webResource = client.resource(url.toString());

                System.out.println("Calling " + url);
                ClientResponse response = isPost
                    ? webResource.type(mediaType).post(ClientResponse.class, postData)
                    : webResource.type(mediaType).get(ClientResponse.class);

                if (response.getStatus() < 300) {
                    restServer.succeeded();
                    return response;
                }

                restServer.failed();

            } catch (Exception ex) {
                System.out.println(restServer + " failed with exception " + ex.getMessage());
                restServer.failed();
            }
        }

        // No servers worked
        return null;
    }
}

class RestServer {

    private final int TIME_TO_WAIT_BEFORE_TRYING_AGAIN = 1000 * 30; // 30 seconds
    private String host;
    private int port;
    private Calendar lastAttempted;
    private boolean lastCallFailed;

    public RestServer(String host, int port) {
        this.host = host;
        this.port = port;
        lastAttempted = Calendar.getInstance();
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public void failed() {
        lastCallFailed = true;
        lastAttempted = Calendar.getInstance();
    }

    public void succeeded() {
        lastCallFailed = false;
        lastAttempted = Calendar.getInstance();
    }

    public boolean shouldTry() {
        if (!lastCallFailed)
            return true;

        return Calendar.getInstance().compareTo(lastAttempted) > TIME_TO_WAIT_BEFORE_TRYING_AGAIN;
    }

    @Override
    public String toString() {
        return new StringBuilder(host).append(':').append(port).toString();
    }
}

答案 1 :(得分:1)

由于担心添加一堆组件会使应用程序设置变得复杂,我可能会考虑使用一种快速而又脏的纯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);
                }
            }
        }
    }
}