Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze ZooKeeper distributed Task scheduling Center

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to analyze the ZooKeeper distributed task scheduling center, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

One: background

In the software system, the timing task is often indispensable, and we may use Spring quartz to establish the timing task module. When the task module is distributed, it usually occurs that the scheduled tasks are executed repeatedly. How to avoid this situation? is it possible to build a task registry where Quartz registers the task but does not specifically execute the business logic within the task?

The task registry solves the problem of repeated registration of tasks and assigns tasks to several processors for specific business processing to ensure that a task will only be processed by one processor at the same time. The figure below is as follows

Two: task registration center and scheduling center

The client-side framework Curator of ZooKeeper is adopted and the Master election mechanism of ZooKeeper is used to realize it. The registration task is equivalent to creating or updating a node in ZooKeeper. Record the execution status of the task by updating the contents of the node.

Three: code simulation

Public class ZookeeperDemo5 {

Private static String PATH = "/ zkbk2"

Private static String FINISH_FLAG= "finish"

Private static CuratorFramework client = CuratorFrameworkFactory.builder ()

.connectString ("10.243.3.18) sessionTimeoutMs 2181 (10.243.3.17)

.retryPolicy (new ExponentialBackoffRetry (100,3)) .build ()

Public static void main (String [] args) throws Exception {

Client.start ()

PathChildrenCache cache = new PathChildrenCache (client, PATH, true)

Cache.start (StartMode.POST_INITIALIZED_EVENT)

Cache.getListenable () .addListener (new PathChildrenCacheListener () {

@ Override

Public void childEvent (CuratorFramework client, PathChildrenCacheEvent event) {

Switch (event.getType ()) {

Case CHILD_ADDED:

System.out.println ("CHILD_ADDED," + event.getData () .getPath ())

Deal (event)

Break

Case CHILD_UPDATED:

System.out.println ("CHILD_UPDATED," + event.getData () .getPath ())

Deal (event)

Break

Case CHILD_REMOVED:

System.out.println ("CHILD_REMOVED," + event.getData () .getPath ())

Break

Default:

Break

}

}

});

Thread.sleep (Integer.MAX_VALUE)

}

Private static void deal (PathChildrenCacheEvent event)

{

Final LeaderLatch latch = new LeaderLatch (client, event.getData (). GetPath ())

Latch.addListener (new LeaderLatchListener () {

@ Override

Public void notLeader () {

}

@ Override

Public void isLeader () {

Try {

System.out.println ("= begin deal=" + event.getData () .getPath ())

String data = new String (client.getData () .forPath (event.getData () .getPath ()

If (! data.contains ("finish")) {

Thread.sleep (10000L)

System.out.println ("")

System.out.println ("")

System.out.println ("* *")

System.out.println ("* *")

System.out.println ("* *")

System.out.println ("* *")

System.out.println ("")

System.out.println ("")

Client.setData () .forPath (event.getData () .getPath (), FINISH_FLAG.getBytes ())

}

System.out.println ("= finish deal=" + event.getData () .getPath ())

Latch.close ()

}

Catch (Exception e) {

E.printStackTrace ()

}

}

});

Try {

Latch.start ()

}

Catch (Exception e) {

E.printStackTrace ()

}

}

}

After reading the above, have you mastered the method of how to analyze ZooKeeper distributed task scheduling center? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report