In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use springboot+zookeeper to achieve distributed locks", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use springboot+zookeeper to achieve distributed locks.
InterProcessMutex implements the mechanism of zookeeper distributed locks internally, so let's try to use this tool to add distributed lock processing to our business.
The characteristics of zookeeper distributed lock: 1, distributed 2, fair lock 3, reentrant
Relies on org.apache.zookeeper zookeeper 3.4.10 org.apache.curator curator-framework 2.12.0 org.apache.curator curator-recipes 2.12.0 org.projectlombok lombok 1.18.16 provided native encapsulation
This utility class mainly encapsulates the client of CuratorFramework (connection Zookeeper)
@ Slf4jpublic class CuratorClientUtil {private String zookeeperServer; @ Getter private CuratorFramework client; public CuratorClientUtil (String zookeeperServer) {this.zookeeperServer = zookeeperServer;} / / create the CuratorFrameworkFactory and start the public void init () {/ / retry policy, wait for 1 s, and retry up to 3 times RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000Jing 3) This.client = CuratorFrameworkFactory.builder () .connectString (zookeeperServer) .sessionTimeoutMs (5000) .connectionTimeoutMs (5000) .retryPolicy (retryPolicy) .build (); this.client.start () } / / Container closes, CuratorFrameworkFactory closes public void destroy () {try {if (Objects.nonNull (getClient () {getClient (). Close ();}} catch (Exception e) {log.info ("CuratorFramework close error= > {}", e.getMessage ()) } when configuring @ Configurationpublic class CuratorConfigration {@ Value ("${zookeeper.server}") private String zookeeperServer; / / injection, specify initMethod and destroyMethod @ Bean (initMethod = "init", destroyMethod = "destroy") public CuratorClientUtil curatorClientUtil () {CuratorClientUtil clientUtil = new CuratorClientUtil (zookeeperServer); return clientUtil;}} test code
Simulate requests from different clients
@ Slf4j@RestController@RequestMapping ("/ test") public class TestController {/ / inject the client utility class @ Autowired private CuratorClientUtil curatorClientUtil; / / create a temporary ordered node corresponding to the lock under the / rootLock node of zookeeper private String rootLock = "/ rootLock" @ GetMapping ("/ testLock") public Object testLock () throws Exception {/ / gets the name of the current thread, making it easy to observe which threads are acquiring the lock String threadName = Thread.currentThread (). GetName (); InterProcessMutex mutex = new InterProcessMutex (curatorClientUtil.getClient (), rootLock); try {log.info ("{}-acquire lock start", threadName) / / attempt to acquire lock, wait for up to 3 seconds, timeout to abandon acquisition boolean lockFlag = mutex.acquire (3000, TimeUnit.SECONDS); / / acquire lock successfully, if (lockFlag) {log.info ("{}-acquire lock success", threadName) / / simulate business processing with 3s Thread.sleep (3000);} else {log.info ("{}-acquire lock fail", threadName);}} catch (Exception e) {log.info ("{}-acquire lock exception", threadName) } finally {/ / Business processing completes, releases the lock, wakes up the thread with a sequence number larger than the node created by the current thread (nearest) to acquire the lock mutex.release (); log.info ("{}-- Lock release", threadName);} return "thread:" + threadName + "execution complete";}} JMeter test
We use JMeter to simulate simultaneous access to localhost:8081/test/testLock by 100 clients, which is equivalent to 100 clients scrambling for distributed locks. As shown in the upper right corner of the figure, it takes 5 minutes and 6s for 100 requests. The ideal time for each thread to acquire the post-lock business processing 3sJing 100 threads is 300s (Thread.sleep (3000)), so the running time matches.
Zookeeper the temporary ordered nodes created by each thread under the / rooLock node are shown in the following figure. Because they are temporary, these nodes will also be deleted after the thread releases the lock.
100 thread program log printing
At this point, I believe you have a deeper understanding of "how to use springboot+zookeeper to achieve distributed locks". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.