Zookeeper notes

eason 2021-01-23 21:29:06
zookeeper notes


zookeeper

  • effect : Distributed coordination service framework , Solve the consistency problem of application system in distributed cluster , It's essentially a file storage system , And es Unlike him, he has only one master-slave system , The master node is responsible for the transaction , The request received from the slave node is forwarded to the master node , The slave node is responsible for reading
  • nature

    • Global consistency : Each server keeps the same data
    • reliability : The message is received by a radio , Will be received by all
    • Sequence

      • Overall order , On the server , What is inserted first must be in front of what is inserted later
      • Partial order , The client inserts data into a To zookeeper, In the insert b To zookeeper, When the client is looking at the data a stay b front
    • Atomicity of data updates : Data updates either all succeed or fail
    • The real time : After the master node data changes , Other nodes will immediately perceive the data change information , And real-time perception of server status
  • Similar to a normal file system , The internal nodes have a namespace , It's a tree structure , stay zookeeper Each node in the is called znode
  • however zookeeper There is a difference between the node of the system and that of the ordinary system

    • Nodes have both the characteristics of file and directory , The common file system is separated from the common file system

      create -s /test/child1 acl # You can configure child nodes just like directories ,acl For permissions
      create -s /test aaaaa acl # You can also enter the contents of the file ,acl For permissions 
    • Atomic operation
    • The size is limited znode<1M, The actual use is far less than this value
    • By absolute path reference
  • Node type , It was decided at the time of creation

    • Permanent nodes , Only when the user actually performs the delete operation will it be deleted

      • Non serialized nodes
      • Serialize nodes . create -s

        • create file a.txt The serial number will be appended to the end 10 The initial bit is 0,
    • Temporary session node , Session end temporary node deletion , It is not allowed to build child nodes

      • Non serialized nodes
      • Serialize nodes
  • operation

    • Create nodes

      create /zk01 aaaaa # permanent
      create -s /zk02 bbbbb # Permanent serialization
      create -e /zk03 ccccc # temporary
      create -e -s /zk04 dd # Temporary serialization
      create /zk05/zk06 aaa # With nodes, you need to start from the parent node 
    • ls,get and ls2

      ls path [watch] # Check which nodes are in the root directory
      ls2 path [watch] # Check the details in the root directory
      #ls2 /
      #dataVersion Data version number , Every time set+1, Even setting the same value will change
      #cversion Version number of the child node , znode When the child node changes cversion increase 1
      #cZxid znode Create transaction id
      #mZxid znode Modified transactions id
      #ctime Created timestamp
      #mtime The time stamp when the update occurred
      #ephemeralOwner The temporary node has this sessionid, It's not a temporary node
      get path [watch] # Specify the data content and attribute information of the node 
    • Set to update

      set path data
      set /zk01 aaa
      Each change covers the original crisis 
    • Delete node

      rmr path # Recursively delete nodes
      delete path [version] # Delete one level at a time , If there are child nodes that cannot be deleted 
    • zookeeper Limit quota

      Setquota -n|-b val path # n Is the maximum number of nodes , -b The data volume and length of each node
      # -n Don't forget to count yourself , And even if n And b It's all out of the log warning, The task can still be carried out , In which directory to start , Log will be generated in which directory
      # -b And -n Only one of them can work 
    • Remove the restrictions

      delquota [-n|-b] path # Restrictions must be removed before setting 
    • Check history history
  • Publish subscribers , Observer mode

    • nature

      • One time trigger , If the same event occurs later, it will not be triggered
      • Event encapsulation ,WatchedEvent Object to encapsulate the server event and pass

         Notification status (keeperState), Event type (EventType) And node paths (path)
      • event Send asynchronously :watcher Notification events are sent asynchronously from the server to the client
      • Register before sending
    • Step process

      • 1. Register binding to listen
      • 2. Events trigger watcher

        get node watch 
    • This mode can be used as

      • zookeeper Handle server up and down line operation

        • Temporary node +watcher Monitoring mechanism
        • No, zookeeper Just use the heartbeat
      • Multiple nodes select the master node

        • Lock snatching mechanism : Temporary node ( stay master Create under directory , Whoever creates successfully is the master node )+watcher( From node to node master Is it down? , Downtime can be repeated master Steps for )+quota( Limit has only one child node )
  • java operation zookeeper

    • curator
    // 1. establish zookeeper Connect objects to write before
    @Before
    public void before(){
    ExponentialBackoffRetry retry = new ExponentialBackoffRetry(1000,3);
    String connectStr = "node1:2181,node2:2181,node3:2181";
    client = CuratorFrameworkFactory.newClient(connectStr, 5000, 5000, retry);
    // Start client
    client.start();
    }
    // 2. Modifying data
    @Test
    public void test02() throws Exception{
    client.setData().forPath("/zk_java01", " I love you! , Motherland ".getBytes());
    // delete.forpath
    // or deletechildrenifneeded
    // or client.setData().forPath("/zk_java01", " I love you! , Motherland ".getBytes());
    // After setting, you can't get it directly from the command line utf8 The string needs to be processed as a string in the code
    // String operatorObjStr = new String(bytes);
    // System.out.println(operatorObjStr);
    }
    // 3. Close the connection
    @After
    public void after(){
    client.close();
    }
    

The election mechanism

  • The first strategy is a new collective election : In the first boot zookeeper In clusters

    • The whole cluster is configured as :3 Nodes
    • When you start one , Vote for yourself , But the number of clusters started is less than half , Even after voting, the cluster is still not started
    • When you start the second one , More than half of the entire cluster has been started , Because there was no boss elected before , Will make all previous nodes vote again , By default, it is cast to id maximal , The second one is leader
    • When you start the third one , Because there is already leader,id yes 2 Than 3 Small but unable to re elect
  • The second strategy is not a new collective election : Back up zookeeper In clusters

    • When you start one , Vote for yourself , But the number of clusters started is less than half , Even after voting, the cluster is still not started
    • When you start the second one , More than half of the entire cluster has been started , Because there was no boss elected before , Will make all previous nodes vote again , At this time, the vote will be for those with a large amount of data ( Generally, the amount of data is the same without downtime , In case of downtime, the information of data volume and table name is closer to reality ), If it's all the same, it's still for id Big , More than half of them have a boss

版权声明
本文为[eason]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210123212833662g.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云