您的位置:首页 > 编程语言 > Java开发

用java实现简单url负载均衡以及容错

2006-09-13 14:32 1071 查看
公司的服务器经常出问题,只好写了一个负载均衡器(无法安装apache)
package com.sourceware.httploadbalance;
import java.util.Hashtable;
import java.util.Collection;
import java.util.Iterator;
/**
* <p>Title: 负载均衡器</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author 黑山
* @version 1.0
*/
public class Dispatcher {
private java.util.Hashtable allClienct = new Hashtable();
//负载均衡地址;
private Collection allHTTPURLs;
//一旦所有均衡都无法工作时的地址;
private Collection allBackURLs;
private Collection allLiveURLs;
private int refreshSeconds = 60;
private static long latestCheckTime = 0;
private String page600 = "600.html";
private String page601 = "601.html";
private String page602 = "602.html";
private String page603 = "603.html";
private String page604 = "604.html";
 
private Dispatcher() {
allHTTPURLs = new java.util.ArrayList();
allBackURLs = new java.util.ArrayList();
int lbsize = Integer.parseInt(com.sourceware.util.ConfigProperties.
getProperty("lb.size"));
        int backupsize = Integer.parseInt(com.sourceware.util.ConfigProperties.getProperty("backup.size"));
 
for (int i = 0; i < lbsize; i++) {String s = com.sourceware.util.ConfigProperties.getProperty("work" +i);
            if (s != null && s.trim().length() != 0) {allHTTPURLs.add(s);
}}
        for (int i = 0; i < backupsize; i++) {String s = com.sourceware.util.ConfigProperties.getProperty("backup" +i);
if (s != null && s.trim().length() != 0) {
allBackURLs.add(s);
}}refreshSeconds = Integer.parseInt(com.sourceware.util.ConfigProperties.getProperty("refresh.seconds"));
}private static Dispatcher dis = new Dispatcher();public static Dispatcher getInstance() {return dis;}
public String route() {
if (System.currentTimeMillis() - latestCheckTime >refreshSeconds * 1000) {checkURLs();
}//随机挑一个地址;然后检测地址是否可用;如可用则返回该地址;
if (allLiveURLs.size() == 0) {if (allBackURLs.size() == 0) {return page601;
}
            int random = (int) java.lang.Math.random() *allBackURLs.size();
String url = (String) allBackURLs.toArray()[random];
return url;}int random = (int) java.lang.Math.random() *allLiveURLs.size();
System.out.println(allLiveURLs.size() + ":" + random);
String url = (String) allLiveURLs.toArray()[random];
 
if (isLive(url)) {
return url;
} else {
checkURLs();
if (allLiveURLs.size() == 0) {
random = (int) java.lang.Math.random() *allBackURLs.size();
url = (String) allBackURLs.toArray()[random];
return url;
            }random = (int) java.lang.Math.random() *allLiveURLs.size();
url = (String) allLiveURLs.toArray()[random];
 
            return url;}}
public static void main(String[] args) {
String url = Dispatcher.getInstance().route();
        System.out.println(url);}private void checkURLs() {allLiveURLs = new java.util.ArrayList();
Iterator it = allHTTPURLs.iterator();
while (it.hasNext()) {String url = (String) it.next();
 
    if (isLive(url)) {
allLiveURLs.add(url);
System.out.println("check true:" + url);
 
            }}}
    private boolean isLive(String url) {for (int i = 0; i < 3; i++) {try {java.net.HttpURLConnection urlconn = (java.net.HttpURLConnection)new java.net.URL(url).openConnection();
int code = urlconn.getResponseCode();
                if (code == 200) {return true;
                }} catch (Exception ex) {}}return false;}}
 
config.properties
refresh.seconds=60
lb.size=1
backup.size=1
work0=http://sms.i6688.com
backup0=http://www.i6688.com:8080/cmvp
test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%String url = com.sourceware.httploadbalance.Dispatcher.getInstance().route();
 
out.println("<script>");
out.println("window.location='"+url+"'");
 
out.println("</script>");
%>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息