1. 程式人生 > >使用zookeeper實現叢集和負載均衡

使用zookeeper實現叢集和負載均衡

package com.bubble.cluster;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

/**
 * @author 
[email protected]
* @date 2013-2-10 */ public class Client extends ClusterClient { private static String appServer; private static String zkServer = "127.0.0.1:2181"; private static ClientBootstrap bootstrap; private static Client client; public static void main(String[] args) throws Exception { ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); bootstrap = new ClientBootstrap(factory); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("encode", new StringEncoder()); pipeline.addLast("decode", new StringDecoder()); pipeline.addLast("handler", new DemoHandler()); return pipeline; } }); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); client=new Client(); ZkClient zkClient = new ZkClient(zkServer); client.connect(zkClient); client.failOver(); } @Override public void connect(ZkClient zkClient) { while (true) { try { RoundRobinLoadBalance loadBlance = new RoundRobinLoadBalance(); //loadBlance.SetClient("127.0.0.1:"+new Random().nextInt(1000)); String server = loadBlance.select(zkServer); if (server != null) { String ip = server.split(":")[0]; int port = Integer.parseInt(server.split(":")[1]); appServer = server; System.out.println(server); bootstrap.connect(new InetSocketAddress(ip, port)); client.setZkClient(zkClient); client.join( "127.0.0.1:"+new Random().nextInt(5000)); ZookeeperConnStatistic.incrementConn(zkServer, appServer); break; } Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); try { Thread.sleep(1000); } catch (InterruptedException e1) { } connect(zkClient); } } }
package com.bubble.cluster;

import java.util.List;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public abstract class ClusterClient {
	public abstract void connect(ZkClient zkClient);
	public abstract String getAPPServer();
	public void setZkClient(ZkClient zkClient){
		this.zkClient=zkClient;
	}
    private ZkClient zkClient;

	public void failOver() {
		zkClient.subscribeChildChanges(Constant.root, new IZkChildListener() {
			@Override
			public void handleChildChange(String parentPath, List currentChilds) throws Exception {
				boolean has = false;
				for (int i = 0; i < currentChilds.size(); i++) {
					if (getAPPServer().equals(currentChilds.get(i))) {
						has = true;
						break;
					}
				}
				if (!has) {
					connect(zkClient);
				}
			}
		});
	}
	
	public void join(String client){
		if(!zkClient.exists(Constant.client)){
			zkClient.createPersistent(Constant.client);
		}
		if(!zkClient.exists(Constant.client+"/"+client)){
			zkClient.createEphemeral(Constant.client+"/"+client);
		}
	}
	
	public void leave(String client){		
		if(zkClient.exists(Constant.client+"/"+client)){
			zkClient.delete(Constant.client+"/"+client);
		}
		zkClient.close();
	}
}

package com.bubble.cluster;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public class ConsistentHashLoadBalance implements LoadBlance {
	private String client;
	
	public void SetClient(String client){
		this.client=client;
	}
	
	@Override
	public String select(String zkServer) {
		ZkClient zkClient = new ZkClient(zkServer);
		List<String> serverList = zkClient.getChildren(Constant.root);
		ConsistentHashSelector selector=new ConsistentHashSelector(client,serverList);
		return selector.select();
		
	}
	
	 private static final class ConsistentHashSelector {
		 	public ConsistentHashSelector(String client,List<String> appServer){
		 		this.client=client;
		 		this.appServer=appServer;
		 	}
		 
		 	private String client;
		 	private List<String> appServer;
		 	
	        public String select() {
	            String key =client ;
	            byte[] digest = md5(key);
	            String server =appServer.get((int) hash(digest, 0));
	            return server;
	        }

	        private long hash(byte[] digest, int number) {
	            return (((long) (digest[3 + number * 4] & 0xFF) << 24)
	                    | ((long) (digest[2 + number * 4] & 0xFF) << 16)
	                    | ((long) (digest[1 + number * 4] & 0xFF) << 8) 
	                    | (digest[0 + number * 4] & 0xFF)) 
	                    & 0xFFFFFFFFL;
	        }

	        private byte[] md5(String value) {
	            MessageDigest md5;
	            try {
	                md5 = MessageDigest.getInstance("MD5");
	            } catch (NoSuchAlgorithmException e) {
	                throw new IllegalStateException(e.getMessage(), e);
	            }
	            md5.reset();
	            byte[] bytes = null;
	            try {
	                bytes = value.getBytes("UTF-8");
	            } catch (UnsupportedEncodingException e) {
	                throw new IllegalStateException(e.getMessage(), e);
	            }
	            md5.update(bytes);
	            return md5.digest();
	        }

	    }

}

package com.bubble.cluster;
/**
 * @author [email protected]
 * @date 2013-2-11
 */
public class Constant {
	public static final String root="/cluster";
	public static final String round="/round";
	public static final String client="/client";
	public static final String route="/route";
}

package com.bubble.cluster;

import java.util.Date;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

/**
 * @author [email protected]
 * @date 2013-2-10
 */
public class DemoHandler extends SimpleChannelUpstreamHandler {

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		Thread.sleep(5000);
		System.out.println(e.getMessage());
		ctx.getChannel().write("bbb");
	}
	@Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
        e.getChannel().write("abcd");
    }


}

package com.bubble.cluster;

import java.util.List;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public class LeastActiveLoadBalance implements LoadBlance {

	@Override
	public String select(String zkServer) {
		ZkClient zkClient = new ZkClient(zkServer);
		List<String> serverList = zkClient.getChildren(Constant.root);

		String tempServer = null;
		int tempConn = -1;
		for (int i = 0; i < serverList.size(); i++) {
			String server = serverList.get(i);
			if (zkClient.readData(Constant.root + "/" + server) != null) {
				int connNum = zkClient.readData(Constant.root + "/" + server);
				if (tempConn == -1) {
					tempServer = server;
					tempConn = connNum;
				}
				if (connNum < tempConn) {
					tempServer = server;
					tempConn = connNum;
				}
			}else{
				zkClient.close();
				return server;
			}
		}
		zkClient.close();
		if (tempServer != null && !tempServer.equals("")) {
			return tempServer;
		}

		return null;
	}

}

package com.bubble.cluster;

import java.util.List;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public interface LoadBlance {
	String select(String zkServer);
}

package com.bubble.cluster;

import java.util.List;
import java.util.Random;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public class RandomLoadBalance implements LoadBlance {

	@Override
	public String select(String zkServer) {
	    ZkClient zkClient = new ZkClient(zkServer);
		List<String> serverList = zkClient.getChildren(Constant.root);
		zkClient.close();
		Random r=new Random();
		if(serverList.size()>=1){
			String server=serverList.get(r.nextInt(serverList.size()));
			return server;
		}else{
			return null;
		}
		
	}

}

package com.bubble.cluster;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public class RoundRobinLoadBalance implements LoadBlance {
	
	@Override
	public String select(String zkServer) {
		ZkClient zkClient = new ZkClient(zkServer);
		List<String> serverList = zkClient.getChildren(Constant.root);
		int round=0;
		if(!zkClient.exists(Constant.round)){
			zkClient.createPersistent(Constant.round);
			zkClient.writeData(Constant.round, 0);
		}else{
			round=(Integer)zkClient.readData(Constant.round);
			zkClient.writeData(Constant.round, ++round);
		}
		zkClient.close();
		if (serverList != null && serverList.size() > 0) {
			return serverList.get(round % serverList.size());
		} else {
			return null;
		}

	}

}

package com.bubble.cluster;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

/**
 * @author [email protected]
 * @date 2013-2-10
 */
public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ChannelFactory factory = new NioServerSocketChannelFactory(
	            Executors.newCachedThreadPool(),
	            Executors.newCachedThreadPool());
	        ServerBootstrap bootstrap = new ServerBootstrap (factory);
	        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
	            public ChannelPipeline getPipeline() {
	                 ChannelPipeline pipeline = Channels.pipeline();
	                pipeline.addLast("encode",new StringEncoder());
	                pipeline.addLast("decode",new StringDecoder());
	                pipeline.addLast("handler",new DemoHandler());
	                return pipeline;
	            }
	        });
	        bootstrap.setOption("child.tcpNoDelay", true);
	        bootstrap.setOption("child.keepAlive", true);
	        bootstrap.bind(new InetSocketAddress(8081));
	        
	        ClusterServer.join("127.0.0.1:8081", "127.0.0.1:2181");
	}

}

package com.bubble.cluster;

import java.util.List;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author [email protected]
 * @date 2013-2-11
 */
public class ZookeeperConnStatistic {
	public static void incrementConn(String zkServer,String appServer){
		ZkClient zkClient = new ZkClient(zkServer);
		List<String> serverList = zkClient.getChildren(Constant.root);
		for(int i=0;i<serverList.size();i++){
			String server=serverList.get(i);
			if(server.equals(appServer)){
				if(zkClient.readData(Constant.root+"/"+appServer)==null){
					zkClient.writeData(Constant.root+"/"+appServer, 1);	
				}else{
					int conn=zkClient.readData(Constant.root+"/"+appServer);
					zkClient.writeData(Constant.root+"/"+appServer, ++conn);
				}
				break;
			}
		}
		zkClient.close();
	}
	
	public static int getNodeConn(String zkServer,String appServer){
		ZkClient zkClient = new ZkClient(zkServer);
		List<String> serverList = zkClient.getChildren(Constant.root);
		for(int i=0;i<serverList.size();i++){
			String server=serverList.get(i);
			if(server.equals(appServer)){
				int conn=zkClient.readData(Constant.root+"/"+appServer);
				zkClient.close();
				return conn;
			}
		}
		zkClient.close();
		return 0;
	}
}

@Overridepublic String getAPPServer() {return appServer;}}

相關推薦

使用zookeeper實現叢集負載均衡

package com.bubble.cluster; import java.net.InetSocketAddress; import java.util.List; import java.util.Random; import java.util.concurre

Zookeeper實現叢集負載均衡---(5)Zabbix整合Zookeeper示例

1.前言 上一章主要描述了zabbix整合Zookeeper的方案,本文主要描述zabbix整合Zookeeper示例。 2.整合前置條件 1. 安裝完按成Zabbix,其中包括Java Gateway元件 2. 修改Zook

.net core webapi使用nginx實現叢集負載均衡

第一步:先編寫webapi介面:      介面介紹:     1、介面採用appkey和appsecret     2、訪問的話,在報文頭加上,appkey和sign。       1、sign由請求地址(例如:http://www.xxx.com/api/user/xx/,那麼地址是/api/u

zookeeper+dubbo+nginx叢集負載均衡簡單例子

1:服務層面用Dubbo+Zookeeper實現分散式服務,然後Http web層用 Nginx 實現高可用叢集方案,本文記錄下demo例子 2:windows環境,  zk 3臺偽叢集 ,idea,maven 3:zk叢集已搭建好,首先開起zk叢集  cd /bin 目錄下 zkcl

大話叢集負載均衡

在“高併發,海量資料,分散式,NoSql,雲端計算......”概念滿天飛的年代,相信不少朋友都聽說過甚至常與人提起“叢集,負載均衡”等, 但不是所有人都有機會真正接觸到這些技術,也不是所有人都真正理解了這些“聽起來很牛的”技術名詞。下面簡單解釋一下吧。 要了解這些概念首先要了解一下專案架構的演進,我這裡

叢集負載均衡”在實戰當中的運用技巧

在“高併發,海量資料,分散式,NoSql,雲端計算……”概念滿天飛的年代,相信不少朋友都聽說過甚至常與人提起“叢集,負載均衡”等,但不是所有人都有機會真正接觸到這些技術,也不是所有人都真正理解了這些“聽起來很牛的”技術名詞。下面簡單解釋一下吧。 叢集(Cluster) 所謂叢集是指一組獨立的計算機系

叢集負載均衡”的通俗版解釋

在“高併發,海量資料,分散式,NoSql,雲端計算……”概念滿天飛的年代,相信不少朋友都聽說過甚至常與人提起“叢集,負載均衡”等,但不是所有人都有機會真正接觸到這些技術,也不是所有人都真正理解了這些“聽起來很牛的”技術名詞。下面簡單解釋一下吧。 叢集(Cluster) 所謂叢集是指一組獨立的計算機系

DNS實現HA負載均衡

NAPTR和SRV均可以實現後臺伺服器的master/slave和load balancing。但是兩者實現的粒度不一樣,所以兩者通常結合起來使用。通過NAPTR可以查詢服務(service),通過SRV可以查詢某個service的細節比如協議,PORT,server。1 S

Apache+tomcat叢集負載均衡(httpd.conf,workers.properties,uriworkermap.properties)

用apache和tomcat搭建叢集,實現負載均衡 一、叢集和負載均衡的概念(一)叢集的概念  叢集(Cluster)是由兩臺或多臺節點機(伺服器)構成的一種鬆散耦合的計算節點集合,為使用者提供網路服務或應用程式(包括資料庫、Web服務和檔案服務等)的單一客戶檢視,同時提供

FastDFS蛋疼的叢集負載均衡(十五)之lvs四層+Nginx七層負載均衡

Interesting things lvs+nginx的拓撲圖 vip:192.168.12.100 lvs-director:192.168.12.4 nginx1:192.168.12.2 nginx

叢集負載均衡

在“高併發,海量資料,分散式,NoSql,雲端計算……”概念滿天飛的年代,相信不少朋友都聽說過甚至常與人提起“叢集,負載均衡”等,但不是所有人都有機會真正接觸到這些技術,也不是所有人都真正理解了這些“聽起來很牛的”技術名詞。下面簡單解釋一下吧。 叢集(Cl

apache2.4 + mod_proxy + tomcat7 配置叢集負載均衡

叢集和負載均衡好處自然不用說,概念不理解查百度,這裡介紹下怎麼利用mod_proxy配置叢集和負載均衡。 首先我們來實現tomcat的叢集以及session的複雜 1.解壓tomcat7到cluster的目錄,並命名為apache-tomcat-7.0.

apache+tomcat叢集負載均衡配置

Apache+tomcat 負載均衡的入門配置這份文件只是一個初步的配置負載均衡的文件,沒有涉及過多的效能優化的東西所以也就不多言了,直接切入主題。 一、準備工作 安裝JDK並配置環境變數。這裡要求至少1.5以上版本。 安裝 apache2.2 。我的安裝目錄為:D:\A

注意這幾點,輕輕鬆鬆配置 Nginx + Tomcat 的叢集負載均衡

Tomcat 叢集是當單臺伺服器達到效能瓶頸,通過橫向擴充套件的方式提高整體系統效能的有效手段。Nginx 是一個高效能的 HTTP 和反向代理 web 伺服器,可以通過簡單的配置實現 Tomcat 叢集的負載均衡。 本文使用的 Tomcat 是 8.5.35 版本,Nginx 是 1.14.2 版本。接下來

Nginx的叢集負載均衡

Nginx的叢集和負載均衡 負載均衡配置案例1 設定上游伺服器: # 設定上游伺服器: upstream imgserver{

服務負載均衡實現zookeeper

Zookeeper   1、Zookeeper是什麼? Zookeeper是一個大型分散式系統的可靠地協調系統,提供的功能包括配置維護,名字服務,分散式同步,組服務等。Zeepkeeper是可以通過叢

Docker的安裝鏡像管理並利用Docker容器實現nginx的負載均衡、動靜分離

docker的安裝和鏡像管理並利用docker容器實現nginx的負載均衡、動靜分離Docker的安裝一、Docker的概念Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後發布到任何流行的 Linux 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互

LVS+Keepalived 實現高可用負載均衡叢集

LVS+Keepalived  實現高可用負載均衡叢集     隨著網站業務量的增長,網站的伺服器壓力越來越大?需要負載均衡方案!商業的硬體如 F5 ,Array又太貴,你們又是創業型互聯公司如何有效節約成本,節省不必要的浪費?同時還需要實現商業硬體一樣的高效能高可

《商城專案02》--用Nginx實現反向代理負載均衡

一, Nginx的安裝 1, 下載解壓安裝 (這裡提供一個1.8.0的windows版本:  連結:https://pan.baidu.com/s/1Cf0sbrlUuc15OKDQ36J9mQ  提取碼:y5z5) 2, 啟動測試 雙擊nginx.ex

編譯安裝nginx並實現反向代理負載均衡快取功能

一、編譯安裝nginx 1、下載 [[email protected] ~]# wget http://nginx.org/download/nginx-1.10.0.tar.gz 2、解壓 [[email protected] ~]#&