当前位置:   article > 正文

mysql实现分布式锁的缺点_分布式锁的实现方式和优缺点&Java代码实现

mysql作为分布式锁的优缺点

packagecom.zhi.test;importjava.util.Random;importjava.util.concurrent.CountDownLatch;importorg.apache.commons.dbcp2.BasicDataSource;importorg.apache.commons.lang3.StringUtils;importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importorg.apache.zookeeper.WatchedEvent;importorg.apache.zookeeper.Watcher;importorg.apache.zookeeper.ZooKeeper;importorg.junit.jupiter.api.MethodOrderer;importorg.junit.jupiter.api.Order;importorg.junit.jupiter.api.Test;importorg.junit.jupiter.api.TestInstance;importorg.junit.jupiter.api.TestInstance.Lifecycle;importorg.junit.jupiter.api.TestMethodOrder;importcom.zhi.util.DatabaseLock;importcom.zhi.util.DistributeLock;importcom.zhi.util.RedisLock;importcom.zhi.util.ZookeeperLock;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;/*** 分布式锁测试

*

*@author张远志

*@since2020年5月3日13:10:43

**/@TestInstance(Lifecycle.PER_CLASS)

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)public classLockTest {private final Logger logger = LogManager.getLogger(this.getClass());private static final String key = "test";

@Order(1)

@Testpublic voidtest1() {

logger.info("开始测试数据库(MySQL)分布式锁-----------------------------------------------");

BasicDataSource dataSource= newBasicDataSource();

dataSource.setUsername("root");

dataSource.setPassword("abc123");

dataSource.setUrl("jdbc:mysql://192.168.59.131:3306/zhi_test");

dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");

doJob(newDatabaseLock(dataSource));

}

@Order(2)

@Testpublic voidtest2() {

logger.info("开始测试Redis分布式锁-----------------------------------------------");

JedisPoolConfig config= newJedisPoolConfig();

config.setMaxIdle(5);

config.setTestOnBorrow(false);

JedisPool jedisPool= new JedisPool(config, "192.168.59.131", 6379);

doJob(newRedisLock(jedisPool));

}

@Order(3)

@Testpublic voidtest3() {

logger.info("开始测试Zookeeper分布式锁-----------------------------------------------");

ZooKeeper zooKeeper= null;try{

zooKeeper= new ZooKeeper("192.168.59.131:2181", 60000, newWatcher() {public voidprocess(WatchedEvent event) {

logger.info("事件类型:{},路径:{}", event.getType(), event.getPath());

}

});

doJob(newZookeeperLock(zooKeeper));

}catch(Exception e) {

}finally{try{

zooKeeper.close();

}catch(Exception e) {

}

}

}private voiddoJob(DistributeLock locker) {int jobCount = 100;

CountDownLatch latch= newCountDownLatch(jobCount);for (int i = 0; i < jobCount; i++) {newWorker(i, locker, latch).start();

}try{

latch.await();

}catch(InterruptedException e) {

}

}class Worker extendsThread {privateDistributeLock locker;privateCountDownLatch latch;public Worker(inttNo, DistributeLock locker, CountDownLatch latch) {super("线程" + StringUtils.leftPad(String.valueOf(tNo), 3, "0"));this.locker =locker;this.latch =latch;

}

@Overridepublic voidrun() {

LockTest.doSleep(new Random().nextInt(20) * 1000);long startTime =System.currentTimeMillis();if(locker.lock(key, getName())) {

logger.info("{}申请到锁,申请动作耗时{}毫秒", getName(), System.currentTimeMillis() -startTime);

LockTest.doSleep(1000);boolean flag =locker.unlock(key, getName());

logger.info("{}释放锁{}", getName(), flag ? "成功" : "失败");

}

latch.countDown();

}

}public static void doSleep(longmillis) {try{

Thread.sleep(millis);

}catch(Exception e) {

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/745106
推荐阅读
相关标签
  

闽ICP备14008679号