《程序員》08.12期《服務器負載均衡架構之傳輸層負載均衡》源代碼公布

          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. }




          posted on 2009-04-10 23:50 .VwV. 閱讀(369) 評論(0)  編輯  收藏 所屬分類: java

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 萨迦县| 彩票| 额济纳旗| 宜兰县| 永胜县| 祁连县| 封丘县| 北宁市| 普宁市| 广河县| 大同市| 桂东县| 雷州市| 佛冈县| 邵阳县| 防城港市| 江油市| 金沙县| 巫溪县| 南召县| 利辛县| 隆德县| 民乐县| 澄迈县| 密山市| 罗平县| 宁远县| 锡林郭勒盟| 湘西| 固始县| 苗栗县| 保康县| 涿州市| 平度市| 甘孜县| 文水县| 汤阴县| 大埔县| 西充县| 甘肃省| 平顺县|