Commit 3810529e authored by liuxingyu's avatar liuxingyu

修改文件夹名称

parent 85311783
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
<artifactId>redisson-lock</artifactId> <artifactId>redisson-lock</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
......
/*
* Copyright (c) 2020 牧星仓库管理系统 All rights reserved.
*
* http://www.mushiny.com
*
* 版权所有,侵权必究!
*/
package com.mushiny.redisson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
...@@ -3,11 +3,15 @@ package com.mushiny.redisson.controller; ...@@ -3,11 +3,15 @@ package com.mushiny.redisson.controller;
import com.mushiny.redisson.enums.LockType; import com.mushiny.redisson.enums.LockType;
import com.mushiny.redisson.utils.RedissonLockUtil; import com.mushiny.redisson.utils.RedissonLockUtil;
import com.mushiny.redisson.utils.ResultForLock; import com.mushiny.redisson.utils.ResultForLock;
import jodd.net.HttpStatus;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/** /**
* 描述信息 * 描述信息
* *
...@@ -24,8 +28,10 @@ public class TestController { ...@@ -24,8 +28,10 @@ public class TestController {
public int count = 0; public int count = 0;
@PostMapping ("api/ping") public int errCount = 0;
public ResultForLock<String> contextLoads() {
@PostMapping ("api/reentrant")
public ResultForLock<String> reentrant(HttpServletResponse response) {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId(); String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value()); redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(50000L, "265213225444411", LockType.REENTRANT_LOCK.value()); Boolean getLock = redissonLockUtil.tryLock(50000L, "265213225444411", LockType.REENTRANT_LOCK.value());
...@@ -47,7 +53,97 @@ public class TestController { ...@@ -47,7 +53,97 @@ public class TestController {
redissonLockUtil.releaseLock("265213225444411", LockType.REENTRANT_LOCK.value()); redissonLockUtil.releaseLock("265213225444411", LockType.REENTRANT_LOCK.value());
} }
} else { } else {
errCount++;
System.out.println("拿锁失败"+errCount);
response.setStatus(500);
return new ResultForLock().error();
}
return new ResultForLock().ok(threadId);
}
@PostMapping ("api/reentrantWatchDog")
public ResultForLock<String> reentrantWatchDog(HttpServletResponse response) {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(1000L,-1L, "265213225444411", LockType.REENTRANT_LOCK.value());
if (getLock) {
System.out.println(threadId + "拿锁成功");
try {
count++;
jdbcTemplate.execute("UPDATE descrease a SET a.number = a.number-1");
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread.sleep(2000);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
System.out.println("执行数量为"+count);
} catch (Exception e) {
e.printStackTrace();
return new ResultForLock().error();
}finally {
redissonLockUtil.releaseLock("265213225444411", LockType.REENTRANT_LOCK.value());
}
} else {
System.out.println("拿锁失败");
response.setStatus(500);
return new ResultForLock().error();
}
return new ResultForLock().ok(threadId);
}
/**
* 手动加锁和解锁,最常用的方式
* @return
*/
@PostMapping ("api/reentrantByManual")
public ResultForLock<String> reentrantByManual() {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
RLock rLock =redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
// 支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁
//lock.lock(10, TimeUnit.SECONDS);
rLock.lock();// * 加锁 锁的有效期默认30秒
try {
count++;
jdbcTemplate.execute("UPDATE descrease a SET a.number = a.number-1");
Thread.sleep(2000);
System.out.println("执行数量为"+count);
} catch (Exception e) {
e.printStackTrace();
return new ResultForLock().error();
}finally {
rLock.unlock();
}
return new ResultForLock().ok(threadId);
}
/**
* 使用公平锁,先请求的线程先拿到锁
* @return
*/
@PostMapping ("api/fair")
public ResultForLock<String> fair() {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.FAIR_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(50000L, "265213225444411", LockType.FAIR_LOCK.value());
if (getLock) {
System.out.println(threadId + "拿锁成功");
try {
count++;
jdbcTemplate.execute("UPDATE descrease a SET a.number = a.number-1");
Thread.sleep(2000);
System.out.println("执行数量为"+count);
} catch (Exception e) {
e.printStackTrace();
return new ResultForLock().error();
}finally {
redissonLockUtil.releaseLock("265213225444411", LockType.REENTRANT_LOCK.value());
}
} else {
System.out.println("拿锁失败"); System.out.println("拿锁失败");
return new ResultForLock().error();
} }
return new ResultForLock().ok(threadId); return new ResultForLock().ok(threadId);
} }
......
...@@ -104,7 +104,7 @@ public class RedissonLockUtil { ...@@ -104,7 +104,7 @@ public class RedissonLockUtil {
lock = rLock.tryLock(waitTime, releaseTime, TimeUnit.MILLISECONDS); lock = rLock.tryLock(waitTime, releaseTime, TimeUnit.MILLISECONDS);
if (!lock) { if (!lock) {
log.error("分布式锁加锁:{}失败,请重新尝试"); log.error("分布式锁加锁:{}失败,请重新尝试");
throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION); // throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -2,6 +2,7 @@ package com.mushiny.redisson.utils; ...@@ -2,6 +2,7 @@ package com.mushiny.redisson.utils;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import org.springframework.stereotype.Component;
import java.util.UUID; import java.util.UUID;
...@@ -101,25 +102,9 @@ public class SnowFlakeUtil { ...@@ -101,25 +102,9 @@ public class SnowFlakeUtil {
* 产生下一个String类型的ID * 产生下一个String类型的ID
*/ */
public synchronized String nextStrId() { public synchronized String nextStrId() {
String prefix = RandomUtil.randomString(10); String prefix = RandomUtil.randomString(5);
long currStamp = getNewStamp(); String netId = String.valueOf(nextId());
if (currStamp < lastStamp) { return prefix + netId;
throw new RuntimeException("时钟后移,拒绝生成ID!");
}
if (currStamp == lastStamp) {
// 相同毫秒内,序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE;
// 同一毫秒的序列数已经达到最大
if (sequence == 0L) {
currStamp = getNextMill();
}
} else {
// 不同毫秒内,序列号置为0
sequence = 0L;
}
lastStamp = currStamp;
Long outStamp = (currStamp - START_STAMP) << 8;
return prefix + outStamp;
} }
private long getNextMill() { private long getNextMill() {
......
server:
port: 10101
servlet:
context-path: /
shutdown: graceful #开启优雅停机,默认IMMEDIATE是立即关机
tomcat:
maxThreads: 1000
connection-timeout: 15000ms
keepAliveTimeout: 15000
maxKeepAliveRequests: 200
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
username: root
password: 123456
#server:
# port: 10101
# servlet:
# context-path: /
# shutdown: graceful #开启优雅停机,默认IMMEDIATE是立即关机
# tomcat:
# maxThreads: 1000
# connection-timeout: 15000ms
# keepAliveTimeout: 15000
# maxKeepAliveRequests: 200
#
#spring:
# datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
# username: root
# password: 123456
#
#
#
#
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment