zookeeper实现负载均衡

在这里插入图片描述
先看看这张图,业务服务在/web节点下注册临时节点,如server1服务对应/web/web1临时节点,然后监控/web节点的孩子节点事件,使用一个集合保存这些点数据,当新增和移除时,分别在记录这些节点数据,下边我们实现一个加权随机负载均衡的模拟实现

1.定义节点数据,保护节点路径,服务ip,服务端口,权重

/**
 * @ClassName NodeData
 * @Author mjlft
 * @Date 2020/1/21 15:11
 * @Version 1.0
 * @Description TODO
 */
public class NodeData implements Serializable {
    private static final long serialVersionUID = 4848971284812662834L;

    private String ip;
    private Integer port;
    private Integer weight;
    private String path;


    public NodeData() {
    }

    public NodeData(String ip, Integer port, Integer weight, String path) {
        this.ip = ip;
        this.port = port;
        this.weight = weight;
        this.path = path;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeData nodeData = (NodeData) o;
        return ip.equals(nodeData.ip) &&
                port.equals(nodeData.port);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ip, port);
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "NodeData{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", weight=" + weight +
                ", path='" + path + '\'' +
                '}';
    }
}

2.使用Curator时,保存到节点的数据必须时byte数组,所以这里我们需要定义一个工具类将节点数据转换byte数组,

/**
 * @ClassName DataUtil
 * @Author mjlft
 * @Date 2020/1/21 15:21
 * @Version 1.0
 * @Description TODO
 */
public class DataUtil {

    //将object转换为bytes
    public static byte[] getBytesFromObject(Object object) throws IOException {
        ObjectOutputStream out = null;
        ByteArrayOutputStream bos = null;
        try {
//            System.out.println(object);
            bos = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bos);
            out.writeObject(object);
            out.flush();
            byte[] yourBytes = bos.toByteArray();
            return yourBytes;
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    //
    public static Object getObjectFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new ByteArrayInputStream(bytes));
            return in.readObject();
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

3.定义服务注册类,这里我们把每个webService当初一台物理服务,他们会像zookeeper注册服务

/**
 * @ClassName Service
 * @Author mjlft
 * @Date 2020/1/21 10:24
 * @Version 1.0
 * @Description TODO
 */
public class Service {

    protected CuratorFramework client;

    //开启客户端
    public void start() {
        ClientUtil.start(this.client);
    }

    //关闭客户端
    public void stop() {
        System.out.println(this.configData.getNodeName() + "节点宕机");
        ClientUtil.stop(this.client);
    }
}

/**
 * @ClassName WebService
 * @Author mjlft
 * @Date 2020/1/21 15:11
 * @Version 1.0
 * @Description TODO
 */
public class WebService extends Service {

    public final static String SERVICE_PATH = "/web";
    private NodeData nodeData;


    public WebService(CuratorFramework client, NodeData nodeData) {
        super.client = client;
        this.nodeData = nodeData;
    }

    //节点初始化
    public void init() throws Exception {
        String path = SERVICE_PATH + "/" + nodeData.getPath();
        //首先判断当前节点是否已经存在
        Stat stat = client.checkExists().forPath(path);
        if (stat == null) {
            String parentpath = ClientUtil.getparrentpath(path);
            ClientUtil.createParentPath(parentpath, client);
            //如果不存在就创建一个新的节点
            System.out.println(nodeData);
            client.create().withMode(CreateMode.EPHEMERAL).forPath(path, DataUtil.getBytesFromObject(this.nodeData));
        } else {
            //如果已经存在,则更新数据
            client.setData().forPath(path, DataUtil.getBytesFromObject(this.nodeData));
        }
    }
}

4.负载均衡类,这个类中我们注册了一个节点监听器,监听子节点的变化,如果由新的子节点被注册,那么在集合nodeDatas添加新增节点数据,如果有服务下线,那么对应的子节点也会被删除,应为这里创建的都是临时节点,那么对应的节点数据也会从集合中删除,

为什么要使用监听器,因为服务上线和下线的频率相对较低,那么在进行负载算法的时候就不用每次到zookeeper中获取节点数据,负载每发送一次请求都要到zookeeper中获取,这样又多了好几次网络来回。

/**
 * @ClassName LoadBalanceService
 * @Author mjlft
 * @Date 2020/1/21 15:35
 * @Version 1.0
 * @Description 服务监控
 */
public class LoadBalanceService extends Service {

    private List<NodeData> nodeDatas = new ArrayList<>(16);

    public LoadBalanceService(CuratorFramework client) {
        super.client = client;
    }

    //拿去已经注册上来的所有节点
    public void init() throws Exception {
        List<String> children = client.getChildren().forPath(WebService.SERVICE_PATH);
        for(String path : children){
            path = WebService.SERVICE_PATH + "/" + path;
            try {
                byte[] data = client.getData().forPath(path);
                NodeData nodeData = (NodeData) DataUtil.getObjectFromBytes(data);
                nodeDatas.add(nodeData);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void register() throws Exception {
        PathChildrenCache watcher = new PathChildrenCache(client, WebService.SERVICE_PATH, true/*,false, service*/);
        watcher.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent) throws Exception {
                if(pathChildrenCacheEvent.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){
                    System.out.println(pathChildrenCacheEvent.getData().getPath() + "上线");
                    //新服务注册
                    NodeData data = (NodeData)DataUtil.getObjectFromBytes(pathChildrenCacheEvent.getData().getData());
                    nodeDatas.add(data);
                }
                if(pathChildrenCacheEvent.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){
                    //服务下架或宕机
                    System.out.println(pathChildrenCacheEvent.getData().getPath() + "下线");
                    NodeData data = (NodeData)DataUtil.getObjectFromBytes(pathChildrenCacheEvent.getData().getData());
                    nodeDatas.remove(data);
                }
            }
        });
        watcher.start(PathChildrenCache.StartMode.NORMAL);
    }

    //负载算法,随机选择当前在线的一台服务
    public NodeData loadBalance(){
        ThreadLocalRandom random = ThreadLocalRandom.current();
        NodeData result = null;
        if(nodeDatas.isEmpty()){
            return null;
        }
        synchronized (nodeDatas){
            if(nodeDatas.isEmpty()){
                return null;
            }
            int all = 0;
            for (NodeData nodeData : nodeDatas){
                all += nodeData.getWeight();
            }
            int index = random.nextInt(all);
            for (NodeData nodeData: nodeDatas){
                if(index <= nodeData.getWeight()){
                    result = nodeData;
                    break;
                }
                index -= nodeData.getWeight();
            }
        }
        return result;
    }
}

最后测试

/**
 * @ClassName Test
 * @Author mjlft
 * @Date 2020/1/21 15:57
 * @Version 1.0
 * @Description TODO
 */
public class Test {
    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new RetryNTimes(3, 100);
        for(int i = 0; i < 10; i ++){
            CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.1.107:2181, 192.168.1.107:2182",
                    30*60*1000, 5*1000, retryPolicy);
            NodeData nodeData = new NodeData("192.168.1."+i, 8080, i, "web"+i);
            WebService webService = new WebService(client, nodeData);
            webService.start();
            webService.init();
        }

        CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.1.107:2181, 192.168.1.107:2182",
                30*60*1000, 5*1000, retryPolicy);
        LoadBalanceService loadBalanceService = new LoadBalanceService(client);
        loadBalanceService.start();
        loadBalanceService.init();
        loadBalanceService.register();


        while (true){
            Scanner sc = new Scanner( System.in );
            String nextCommand = sc.nextLine();
            NodeData nodeData = loadBalanceService.loadBalance();
            System.out.println("本次请求由:" + nodeData.getIp() + ":" + nodeData.getPort() + " 执行");
        }
    }
}

测试结果:

/web/web9上线
/web/web8上线
/web/web7上线
/web/web6上线
/web/web5上线
/web/web4上线
/web/web3上线
/web/web2上线
/web/web1上线
/web/web0上线
1
本次请求由:192.168.1.5:8080 执行
1
本次请求由:192.168.1.5:8080 执行
1
本次请求由:192.168.1.3:8080 执行
1
本次请求由:192.168.1.8:8080 执行
1
本次请求由:192.168.1.3:8080 执行
1
本次请求由:192.168.1.2:8080 执行
1
本次请求由:192.168.1.5:8080 执行
2
本次请求由:192.168.1.1:8080 执行
1
本次请求由:192.168.1.9:8080 执行
111
本次请求由:192.168.1.7:8080 执行
1
本次请求由:192.168.1.9:8080 执行
1
本次请求由:192.168.1.2:8080 执行
1
本次请求由:192.168.1.8:8080 执行
11
本次请求由:192.168.1.6:8080 执行

本次请求由:192.168.1.9:8080 执行
1
本次请求由:192.168.1.8:8080 执行
1
本次请求由:192.168.1.4:8080 执行
1
本次请求由:192.168.1.9:8080 执行
1
本次请求由:192.168.1.8:8080 执行
1
本次请求由:192.168.1.8:8080 执行
1
本次请求由:192.168.1.8:8080 执行
/web/web3下线
/web/web8下线
/web/web3下线
/web/web8下线
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页