首先介紹Zookeeper的命名服務
命名服務,客戶端能夠根據指定節點的名字來獲取資源或者服務地址,然後進行下一步操作
Zookeeper集羣的負載均衡
可使用Nginx(或Haproxy) +keepalived 來實現Zookeeper集羣的負載均衡
單個Zookeeper 中znode的輪詢
java代碼實現輪詢:
當節點服務關閉,不會影響其他節點服務的使用
/**
*
* 模擬兩秒一個請求,分配到 /bank/下的bk1,bk2,bk3,bk4,bk5
* 實際不能這麼寫 = = 這只是一個測試,一個思想,具體線程安全等問題沒有實現
* @author start_lie
*
*/
public class MyZookeeper {
private final static Logger log = Logger.getLogger(MyZookeeper.class);
private static ZooKeeper zk=null;
private final static String CONNECT_STRING="192.168.83.128:2991";
private final static int SESSION_TIMEOUT=20*1000;
private static int cutNum=1;
private static int nextWindow=1;
private static int totalWindow=5;
static{
try {
zk=new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, (even)->{});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createZnode(String path,String data) throws KeeperException, InterruptedException {
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
}
public String getZnode(String path) throws KeeperException, InterruptedException {
byte[] result = zk.getData(path, new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
try {
getZnode(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Stat());
String dat = new String(result);
log.info("now this node value is :"+dat);
return dat;
}
public static String getZnode1(String path) throws KeeperException, InterruptedException {
byte[] result = zk.getData(path, false, new Stat());
String dat = new String(result);
//log.info("now this node value is :"+dat);
return dat;
}
public static String doRequest(String path) throws Exception {
List<String> list = zk.getChildren(path, false);
int sign=0;
while(true) {
sign++;
if(list.contains("bk"+nextWindow)) {
nextWindow++;
return getZnode1(path+"/bk"+(nextWindow-1));
}else {
if(nextWindow>=totalWindow) {
nextWindow=1;
}else {
nextWindow++;
}
}
if(sign==totalWindow) {
return "No useable Znode!";
}
}
}
public static void main(String[] args) throws Exception {
// MyZookeeper mzk = new MyZookeeper();
// String path = "/myzoo";
// String data = "zuishuai goodlook";
// String result = mzk.getZnode(path);
// if(result==null) {
// mzk.createZnode(path, data);
// }else {
// log.info("i have this node");
// }
// Thread.sleep(Long.MAX_VALUE);
while(true) {
String reault = doRequest("/bank");
System.out.println("Curstom : "+(cutNum++)+" result :"+reault);
Thread.sleep(2000);
}
}
}
與Dubbo整合
web.xml中整合spring
pom.xml加依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<exclusions>
<exclusion>
<!-- 排除傳遞spring依賴 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
在resourse下新建文件夾dubbo,在裏面新建dubbo.xml文件,內容(設置服務實現類,並將其接口暴露):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方應用信息,用於計算依賴關係 -->
<dubbo:application name="dubboProvider" />
<!-- 這裏使用的註冊中心是zookeeper,請換成自己的IP地址 -->
<dubbo:registry address="zookeeper://192.168.1.167:2181" client="zkclient" />
<!-- 用dubbo協議在20880端口暴露服務 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 將該接口暴露到dubbo中 -->
<dubbo:service ref="bookServiceImpl" interface="com.test.dubbo.service.BookService"/>
<!-- 將具體的實現類加入到Spring容器中 -->
<bean id="bookServiceImpl" class="com.test.dubbo.BookServiceImpl" />
</beans>
消費者服務中resourse下建立dubbo/dubbo.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方應用信息 -->
<dubbo:application name="dubboConsumer" />
<!--註冊中心是zookeeper -->
<dubbo:registry address="zookeeper://192.168.83.128:2991" client="zkclient" />
<!-- 從註冊中心中查找服務 -->
<dubbo:reference id="bookService" interface="com.test.service.BookService" />
</beans>
用spring自動注入該service就可以調用了
註冊多節點+輪詢算法可以實現軟負載均衡