TL--3--zookeeper--3

分布式的集群管理:

1.线上有多少个节点:临时节点

2.多少个节点是在运行的:

3.节点资源的状态:节点数据

4.资源超出我们的阈值实时的警报:监听

----------------------------------------------------------

代码:

import org.I0Itec.zkclient.ZkClient;

import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;

public class Agent {
    private static Agent ourInstance = new Agent();
    private String server = "10.211.55.10:2181";
    ZkClient zkClient;
    private static final String rootPath = "/tuling-manager";
    private static final String serverpath = rootPath+"/service";
    private String nodePath;
    private Thread stateThread;
    public static Agent getInstance() {
        return ourInstance;
    }

    private Agent() {
    }

    // 主要是做监控的 javaagent
    public static void premain(String argd, Instrumentation instrumentation){
        Agent.getInstance().init();
    }

    public void init(){
        zkClient = new ZkClient(server, 5000, 10000);
        System.out.println("zk连接成功" + server);
        buildRoot();
        createServerNode();
        // 多线程更新数据
        stateThread = new Thread(() -> {
            while (true) {
                updateServerNode();
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "zk_stateThread");
        stateThread.setDaemon(true);
        stateThread.start();
    }

    // 构建根节点
    public void buildRoot() {
        if (!zkClient.exists(rootPath)) {
            zkClient.createPersistent(rootPath);
        }
    }

    public void createServerNode(){
        // 临时的序号节点
        nodePath = zkClient.createEphemeralSequential(serverpath,getOsInfo());
        System.out.println("创建节点:"+nodePath);
    }

    // 更新服务节点状态
    public String getOsInfo() {
        return "cpu";
    }

    public void updateServerNode() {
        zkClient.writeData(nodePath, getOsInfo());
    }
}

-------

监控中心的代码:

获取:

首先:

拿出所有的子节点,监听下面的子节点变化。监听主节点只能监听添加和删除子节点。

 

-------------

分布式的注册中心:服务发现,帮助客户端找到服务器,注册中心。

客户端发现了服务端的话,就和服务端建立长连接,去保持心跳。最多2秒。

注册中心要20s的。

列表的同步是通过注册中心的通知机制来实现的。

我们看下zookeeper下面的dubbo的信息:

转码:临时节点

上客户端:临时节点

心跳:https://blog.csdn.net/weixin_34080903/article/details/86014091

https://blog.csdn.net/qq_28411869/article/details/95198290

---------

分布式的JOB。

获得数据判断有没有master。

监听子节点的增加删除变化:

如果是主节点挂了要选举。

这个相当于起的定时任务。master为true才执行。

-------

分布式锁,重点的:https://www.cnblogs.com/toov5/p/9899489.html

代码:

第一步:

第二步:

第三步:加锁

添加监听事件去释放:

尝试去激活这把锁。

监听上一个节点是不是女诶删除掉了。

lock可以写r或者w确保是读锁还是写锁。

第四步:释放锁

测试:

发布了402 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_28764557/article/details/105110152