Introduce third-party SDKS

<dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.10</version> </dependency> </dependencies>Copy the code

Distributed lock integration using a template approach

package com.test.zk; Public interface lock {// Get the lock resource void getLock(); Void unLock(); }Copy the code
package com.test.zk; import org.I0Itec.zkclient.ZkClient; /** * @ClassName ZookeeperAbstractLock * @Description TODO * @Author moran * @Date 2019/12/10 11:29 **/ public abstract Class ZookeeperAbstractLock implements Lock {// zK server.2=192.168.2.101:2888:3888 1=192.168.2.102:2888:3888 //server.3=192.168.2.148 Private Static Final String CONNECTSTRING = "192.168.2.148 192.168.2.101, 192.168.2.102,"; ZkClient = new ZkClient(CONNECTSTRING); protected static final String PATH = "/lock"; Public void getLock() {if (tryLock()) {system.out.println ("## getLock ####"); } else {// wait for waitLock(); // retrieve the lock resource getLock(); }} // get the lock resource. // wait for abstract void waitLock(); public void unLock() { if (zkClient ! = null) { zkClient.close(); System.out.println(" Release lock resource...") ); }}}Copy the code
package com.test.zk; import java.text.SimpleDateFormat; import java.util.Date; Public class OrderNumGenerator {private static int count = 0; public String getNumber() { try { Thread.sleep(200); } catch (Exception e) { } SimpleDateFormat simpt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); return simpt.format(new Date()) + "-" + ++count; }}Copy the code
package com.test.zk; public class OrderService implements Runnable { private OrderNumGenerator orderNumGenerator = new OrderNumGenerator(); / / use the lock lock / / private Java. Util. Concurrent. The locks. The lock lock = new already (); private lock lock = new ZookeeperLock(); public void run() { getNumber(); } public void getNumber() { try { lock.getLock(); String number = orderNumGenerator.getNumber(); System.out.println(thread.currentThread ().getName() + ", "+ number"); } catch (Exception e) { e.printStackTrace(); } finally { lock.unLock(); }} public static void main(String[] args) {system.out.println ("#### generate unique order ###"); // OrderService orderService = new OrderService(); for (int i = 0; i < 100; i++) { new Thread( new OrderService()).start(); }}}Copy the code
package com.test.zk; import org.I0Itec.zkclient.IZkDataListener; import java.util.concurrent.CountDownLatch; public class ZookeeperLock extends ZookeeperAbstractLock { private CountDownLatch countDownLatch = null; @Override boolean tryLock() { try { zkClient.createEphemeral(PATH); return true; } catch (Exception e) { // e.printStackTrace(); return false; } } @Override void waitLock() { IZkDataListener izkDataListener = new IZkDataListener() { public void HandleDataDeleted (String path) throws Exception {// Wake up the waiting thread if (countDownLatch! = null) { countDownLatch.countDown(); } } public void handleDataChange(String path, Object data) throws Exception { } }; / / register event zkClient. SubscribeDataChanges (PATH, izkDataListener); if (zkClient.exists(PATH)) { countDownLatch = new CountDownLatch(1); try { countDownLatch.await(); } catch (Exception e) { e.printStackTrace(); }} / / delete listening zkClient. UnsubscribeDataChanges (PATH, izkDataListener); }}Copy the code