由于篇幅问题,《程序员》杂志08年第12期《服务器负载均衡架构之传输层负载均衡》文中的源代码没有在杂志上刊登,下面贴出其中提到的4段代码,供读者使用。
CodeSeg1.java
  1. interface IHttpResponseCache {

  2.    IHttpResponse put(String key, IHttpResponse response) throws IOException;

  3.    void remove(String key) throws IOException;

  4.    IHttpResponse get(String key) throws IOException;
  5. }



  6. class RemoteHttpResponseCache implements IHttpResponseCache {

  7.    private MemcachedClient memCachedClient;

  8.    public RemoteHttpResponseCache(InetSocketAddress... cacheServers) throws IOException {
  9.       memCachedClient = new MemcachedClient(Arrays.asList(cacheServers));
  10.    }

  11.    public IHttpResponse put(String key, IHttpResponse response) throws IOException {
  12.       byte[] bodyData = response.getBlockingBody().readBytes();

  13.       memCachedClient.set(key, 3600, bodyData);
  14.       return null;
  15.    }


  16.    public IHttpResponse get(String key) throws IOException {
  17.       byte[] bodyData = (byte[]) memCachedClient.get(key);
  18.       if (bodyData != null) {
  19.          return new HttpResponse(200"text/plain", bodyData);
  20.       } else {
  21.          return null;
  22.       }
  23.    }


  24.    public void remove(String key) throws IOException {
  25.       memCachedClient.delete(key);
  26.    }
  27. }

CodeSeg2.java
  1. class MyRequestHandler implements IHttpRequestHandler {

  2.    public void onRequest(IHttpExchange exchange) throws IOException {

  3.       IHttpRequest request = exchange.getRequest();

  4.       int customerId = request.getRequiredIntParameter("id");
  5.       long amount = request.getRequiredLongParameter("amount");
  6.       //...


  7.       // perform some operations
  8.       //..
  9.       String response = ...

  10.       // and return the response
  11.       exchange.send(new HttpResponse(200, "text/plain", response));
  12.    }
  13. }


  14. class Server {

  15.    public static void main(String[] args) throws Exception {
  16.       HttpServer httpServer = new HttpServer(8180, new MyRequestHandler());
  17.       httpServer.run();
  18.    }
  19. }

CodeSeg3.java
  1. class CacheInterceptor implements IHttpRequestHandler {

  2.    private IHttpResponseCache cache;

  3.    public CacheInterceptor(IHttpResponseCache cache) {
  4.       this.cache = cache;
  5.    }


  6.    public void onRequest(final IHttpExchange exchange) throws IOException {

  7.       IHttpRequest request = exchange.getRequest();

  8.       // check if request is cacheable (Cache-Control header, ...)
  9.       // ...
  10.       boolean isCacheable = ...


  11.       // if request is not cacheable forward it to the next handler of the chain
  12.       if (!isCacheable) {
  13.          exchange.forward(request);
  14.          return;
  15.       }

  16.       // create the cache key
  17.       StringBuilder sb = new StringBuilder(request.getRequestURI());
  18.       TreeSet<String> sortedParamNames = new TreeSet<String>(request.getParameterNameSet());
  19.       for (String paramName : sortedParamNames) {
  20.          sb.append(URLEncoder.encode(paramName) + "=");

  21.          List<String> paramValues = Arrays.asList(request.getParameterValues(paramName));
  22.          Collections.sort(paramValues);
  23.          for (String paramValue : paramValues) {
  24.             sb.append(URLEncoder.encode(paramValue) + ", ");
  25.          }
  26.       }
  27.       final String cacheKey = URLEncoder.encode(sb.toString());

  28.       // is request in cache?
  29.       IHttpResponse cachedResponse = cache.get(cacheKey);
  30.       if (cachedResponse != null) {
  31.          IHttpResponse response = HttpUtils.copy(cachedResponse);
  32.          response.setHeader("X-Cached""true");
  33.          exchange.send(response);

  34.       // .. no -> forward it to the next handler of the chain
  35.       } else {

  36.          // define a intermediate response handler to intercept and copy the response
  37.          IHttpResponseHandler respHdl = new IHttpResponseHandler() {

  38.             @InvokeOn(InvokeOn.MESSAGE_RECEIVED)
  39.             public void onResponse(IHttpResponse response) throws IOException {
  40.                cache.put(cacheKey, HttpUtils.copy(response));
  41.                exchange.send(response);  // forward the response to the client
  42.             }

  43.             public void onException(IOException ioe) throws IOException {
  44.                exchange.sendError(ioe);  // forward the error to the client
  45.             }
  46.          };
  47.          // forward the request to the next handler of the chain
  48.          exchange.forward(request, respHdl);
  49.       }
  50.    }
  51. }

  52. class Server {

  53.    public static void main(String[] args) throws Exception {
  54.       RequestHandlerChain handlerChain = new RequestHandlerChain();
  55.       handlerChain.addLast(new CacheInterceptor(new RemoteHttpResponseCache(new InetSocketAddress(cachSrv1, 11211), new InetSocketAddress(cachSrv2, 11211))));
  56.       handlerChain.addLast(new MyRequestHandler());

  57.       HttpServer httpServer = new HttpServer(8180, handlerChain);
  58.       httpServer.run();
  59.    }
  60. }

CodeSeg4.java
  1. class LocalHttpResponseCache extends LinkedHashMap<String, IHttpResponse> implements IHttpResponseCache {

  2.    public synchronized IHttpResponse put(String key, IHttpResponse value) {
  3.       return super.put(key, value);
  4.    }

  5.    public void remove(String key) {
  6.       super.remove(key);
  7.    }

  8.    public synchronized IHttpResponse get(String key) {
  9.       return super.get(key);
  10.    }

  11.    protected boolean removeEldestEntry(Entry<String, IHttpResponse> eldest) {
  12.       return size() > 1000;   // cache up to 1000 entries
  13.    }
  14. }

  15. class Server {

  16.    public static void main(String[] args) throws Exception {
  17.       RequestHandlerChain handlerChain = new RequestHandlerChain();
  18.       handlerChain.addLast(new CacheInterceptor(new LocalHttpResponseCache()));
  19.       handlerChain.addLast(new MyRequestHandler());

  20.       HttpServer httpServer = new HttpServer(8080, handlerChain);
  21.       httpServer.run();
  22.    }
  23. }


Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐