首页>代码>SpringBoot2整合Apache Zookeeper集群管理、负载均衡功能代码>/zookeeper-demo1/src/main/java/com/test/distributed/ImLoadBalance.java
package com.test.distributed;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

/**
 * 负载均衡节点获取
 * @author 程就人生
 * @date 2020年1月13日
 */
@Component
public class ImLoadBalance {
	
	private static Logger log = LoggerFactory.getLogger(ImLoadBalance.class);

	@Value("${zookeeper.register.node}")
    private String managerPath;
    
    @Autowired
    private CuratorFramework zkClient;

    /**
     * 获取负载最小的IM节点
     *
     * @return
     */
    public ImNode getBestWorker() {
        List<ImNode> workers = getWorkers();
        log.info("全部节点如下:");
        workers.stream().forEach(node -> {
            log.info("节点信息:{}",JSONObject.toJSONString(node));
        });
        ImNode best = balance(workers);
        return best;
    }

    /**
     * 按照负载排序
     *
     * @param items 所有的节点
     * @return 负载最小的IM节点
     */
    protected ImNode balance(List<ImNode> items) {
        if (items.size() > 0) {
            // 根据balance值由小到大排序
            Collections.sort(items);

            // 返回balance值最小的那个
            ImNode node = items.get(0);

            log.info("最佳的节点为:{}",JSONObject.toJSONString(node));
            return node;
        } else {
            return null;
        }
    }


    /**
     * 从zookeeper中拿到所有IM节点
     */
    protected List<ImNode> getWorkers() {

        List<ImNode> workers = new ArrayList<ImNode>();

        List<String> children = null;
        try {
            children = zkClient.getChildren().forPath(managerPath);
        } catch (Exception e) {
            e.printStackTrace();
            return workers;
        }

        for (String child : children) {
            log.info("child:"+child);
            byte[] payload = null;
            try {
                payload = zkClient.getData().forPath(managerPath+"/"+child);

            } catch (Exception e) {
                e.printStackTrace();
            }
            if (null == payload) {
                continue;
            }
            ImNode worker = JSONObject.parseObject(payload, ImNode.class, Feature.AllowArbitraryCommas);
            workers.add(worker);
        }
        return workers;

    }
}
最近下载更多
xiaoyuer2  LV8 2022年11月20日
mudingc木钉  LV30 2021年6月16日
wujb315  LV1 2021年1月21日
baynight  LV2 2020年7月22日
qian123go  LV17 2020年6月10日
chyy001  LV5 2020年4月21日
xgaccp  LV8 2020年4月7日
nj1penny  LV2 2020年4月3日
860595563  LV15 2020年3月26日
tiansitong  LV14 2020年3月23日
最近浏览更多
fellowfun  LV12 2023年9月1日
心如止水  LV17 2023年8月15日
yzshabzbbdvw  LV4 2023年6月3日
lironggang  LV38 2023年3月28日
zxc131313  LV12 2023年2月2日
bibibi234  LV1 2022年12月5日
xiaoyuer2  LV8 2022年11月20日
Hachi6  LV13 2022年9月19日
别胡思乱想啦 2022年6月18日
暂无贡献等级
李明翰  LV4 2022年6月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友