ZooKeeper应用之负载均衡

负载均衡

  服务器端负载均衡分为算法包括:轮循算法(Round Robin)、哈希算法(HASH)、响应速度算法(Response Time)、加权法(Weighted )等。网上有文章提到“最少连接算法(Least Connection)”也是负载均衡算法的一种,当然,这样说是需要有前提条件的,就是当服务器性能良好,可以及时处理所有请求的时候,最少连接算法才能达到负载均衡的目的,否则最少连接的服务器可能正在处理比较耗时的操作,继续将请求分发到该服务器可能导致该服务器load值升高,性能下降。参见https://devcentral.f5.com/articles/back-to-basics-least-connections-is-not-least-loaded 。之后我会写关于负载均衡各类算法的博客。

ZK实现负载均衡
  使用zk做负载均衡,需要在zk上指定path下注册提供服务的集群中每个节点的ip地址,消费者需要在该path上注册watcher,当服务提供者集群中增删几点时,zk会发起watcher通知,所有消费者都会收到该watcher回调,此时,消费者可以及时知晓服务提供者集群中节点状况,在可用服务器列表中进行负载均衡计算。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.zk.loadbalance;

import com.java.roundrrobin.RoundRobin;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
* @auth zhengzhi.ren
* @date 2015/7/27.
*/

public class LoadBalance {

private ZooKeeper zkConn;

private String path = "/dubbo/org.zk.naming.HelloWorldService/provider";

private String connectionString = "127.0.0.1:2181";

private int timeout = 5000;

private List<String> hosts;

private RoundRobin<String> roundRobin = new RoundRobin<String>(hosts);

@Before
public void connect() {
try {
final CountDownLatch semaphore = new CountDownLatch(1);
zkConn = new ZooKeeper(connectionString, timeout, new Watcher() {
@Override
public void process(WatchedEvent e) {
if (e.getState() == Event.KeeperState.SyncConnected){
if (e.getType() == Event.EventType.None && e.getPath() == null){
System.out.println("Connect success!");
semaphore.countDown();
}
}
}
});
semaphore.await();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void registry() {
try {
hosts = zkConn.getChildren(this.path, new NodeChildrenChangedWatcher());
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public String get(){
return roundRobin.iterator().next();
}

class NodeChildrenChangedWatcher implements Watcher {

@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
registry();
}
}
}
}

负载均衡之轮询算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.java.roundrrobin;

import java.util.Iterator;
import java.util.List;

/**
* @auth zhengzhi.ren
* @date 2015/7/27.
* round robin algorithm of load balance
*/

public class RoundRobin<T>{
private List<T> collection;

public RoundRobin(List<T> collection) {
this.collection = collection;
}

public Iterator<T> iterator() {
return new Iterator<T>() {
private int index = 0;
@Override
public boolean hasNext() {
return false;
}

@Override
public T next() {
if (collection == null || collection.size() <= 0){
return null;
}
T e = collection.get(index);
index = (index + 1) % collection.size();
return e;
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}

public List<T> getCollection() {
return collection;
}

public void setCollection(List<T> collection) {
this.collection = collection;
}
}

原创技术文章,转载请注明:转自http://newliferen.github.io/