由于篇幅问题,《程序员》杂志08年第12期《服务器负载均衡架构之传输层负载均衡》文中的源代码没有在杂志上刊登,下面贴出其中提到的4段代码,供读者使用。
CodeSeg1.java
CodeSeg2.java
CodeSeg3.java
CodeSeg4.java
CodeSeg1.java
- interface IHttpResponseCache {
- IHttpResponse put(String key, IHttpResponse response) throws IOException;
- void remove(String key) throws IOException;
- IHttpResponse get(String key) throws IOException;
- }
- class RemoteHttpResponseCache implements IHttpResponseCache {
- private MemcachedClient memCachedClient;
- public RemoteHttpResponseCache(InetSocketAddress... cacheServers) throws IOException {
- memCachedClient = new MemcachedClient(Arrays.asList(cacheServers));
- }
- public IHttpResponse put(String key, IHttpResponse response) throws IOException {
- byte[] bodyData = response.getBlockingBody().readBytes();
- memCachedClient.set(key, 3600, bodyData);
- return null;
- }
- public IHttpResponse get(String key) throws IOException {
- byte[] bodyData = (byte[]) memCachedClient.get(key);
- if (bodyData != null) {
- return new HttpResponse(200, "text/plain", bodyData);
- } else {
- return null;
- }
- }
- public void remove(String key) throws IOException {
- memCachedClient.delete(key);
- }
- }
- class MyRequestHandler implements IHttpRequestHandler {
- public void onRequest(IHttpExchange exchange) throws IOException {
- IHttpRequest request = exchange.getRequest();
- int customerId = request.getRequiredIntParameter("id");
- long amount = request.getRequiredLongParameter("amount");
- //...
- // perform some operations
- //..
- String response = ...
- // and return the response
- exchange.send(new HttpResponse(200, "text/plain", response));
- }
- }
- class Server {
- public static void main(String[] args) throws Exception {
- HttpServer httpServer = new HttpServer(8180, new MyRequestHandler());
- httpServer.run();
- }
- }
- class CacheInterceptor implements IHttpRequestHandler {
- private IHttpResponseCache cache;
- public CacheInterceptor(IHttpResponseCache cache) {
- this.cache = cache;
- }
- public void onRequest(final IHttpExchange exchange) throws IOException {
- IHttpRequest request = exchange.getRequest();
- // check if request is cacheable (Cache-Control header, ...)
- // ...
- boolean isCacheable = ...
- // if request is not cacheable forward it to the next handler of the chain
- if (!isCacheable) {
- exchange.forward(request);
- return;
- }
- // create the cache key
- StringBuilder sb = new StringBuilder(request.getRequestURI());
- TreeSet<String> sortedParamNames = new TreeSet<String>(request.getParameterNameSet());
- for (String paramName : sortedParamNames) {
- sb.append(URLEncoder.encode(paramName) + "=");
- List<String> paramValues = Arrays.asList(request.getParameterValues(paramName));
- Collections.sort(paramValues);
- for (String paramValue : paramValues) {
- sb.append(URLEncoder.encode(paramValue) + ", ");
- }
- }
- final String cacheKey = URLEncoder.encode(sb.toString());
- // is request in cache?
- IHttpResponse cachedResponse = cache.get(cacheKey);
- if (cachedResponse != null) {
- IHttpResponse response = HttpUtils.copy(cachedResponse);
- response.setHeader("X-Cached", "true");
- exchange.send(response);
- // .. no -> forward it to the next handler of the chain
- } else {
- // define a intermediate response handler to intercept and copy the response
- IHttpResponseHandler respHdl = new IHttpResponseHandler() {
- @InvokeOn(InvokeOn.MESSAGE_RECEIVED)
- public void onResponse(IHttpResponse response) throws IOException {
- cache.put(cacheKey, HttpUtils.copy(response));
- exchange.send(response); // forward the response to the client
- }
- public void onException(IOException ioe) throws IOException {
- exchange.sendError(ioe); // forward the error to the client
- }
- };
- // forward the request to the next handler of the chain
- exchange.forward(request, respHdl);
- }
- }
- }
- class Server {
- public static void main(String[] args) throws Exception {
- RequestHandlerChain handlerChain = new RequestHandlerChain();
- handlerChain.addLast(new CacheInterceptor(new RemoteHttpResponseCache(new InetSocketAddress(cachSrv1, 11211), new InetSocketAddress(cachSrv2, 11211))));
- handlerChain.addLast(new MyRequestHandler());
- HttpServer httpServer = new HttpServer(8180, handlerChain);
- httpServer.run();
- }
- }
- class LocalHttpResponseCache extends LinkedHashMap<String, IHttpResponse> implements IHttpResponseCache {
- public synchronized IHttpResponse put(String key, IHttpResponse value) {
- return super.put(key, value);
- }
- public void remove(String key) {
- super.remove(key);
- }
- public synchronized IHttpResponse get(String key) {
- return super.get(key);
- }
- protected boolean removeEldestEntry(Entry<String, IHttpResponse> eldest) {
- return size() > 1000; // cache up to 1000 entries
- }
- }
- class Server {
- public static void main(String[] args) throws Exception {
- RequestHandlerChain handlerChain = new RequestHandlerChain();
- handlerChain.addLast(new CacheInterceptor(new LocalHttpResponseCache()));
- handlerChain.addLast(new MyRequestHandler());
- HttpServer httpServer = new HttpServer(8080, handlerChain);
- httpServer.run();
- }
- }