Commit a0119f45 authored by liuxingyu's avatar liuxingyu

修改文件夹名称

parent 4bf3a8b6
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<modules> <modules>
<module>redisson-lock</module> <!-- <module>redisson-lock</module>-->
</modules> </modules>
<groupId>com.mushiny</groupId> <groupId>com.mushiny</groupId>
<artifactId>pubTools</artifactId> <artifactId>pubTools</artifactId>
......
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>pubTools</artifactId> <groupId>org.springframework.boot</groupId>
<groupId>com.mushiny</groupId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0-RELEASE</version> <version>2.7.8</version>
<relativePath></relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>redisson-lock</artifactId> <artifactId>redisson-lock</artifactId>
<packaging>jar</packaging>
<properties> <properties>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
...@@ -52,6 +53,23 @@ ...@@ -52,6 +53,23 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
* 版权所有,侵权必究! * 版权所有,侵权必究!
*/ */
package com.mushiny.pubtools.annotation; package com.mushiny.redisson.annotation;
import com.mushiny.pubtools.enums.LockType; import com.mushiny.redisson.enums.LockType;
import java.lang.annotation.*; import java.lang.annotation.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
......
package com.mushiny.pubtools.aspect; package com.mushiny.redisson.aspect;
import com.mushiny.pubtools.annotation.RedissonLock; import com.mushiny.redisson.annotation.RedissonLock;
import com.mushiny.pubtools.execption.RedissonErrorCode; import com.mushiny.redisson.execption.RedissonErrorCode;
import com.mushiny.pubtools.execption.RedissonException; import com.mushiny.redisson.execption.RedissonException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature; import org.aspectj.lang.Signature;
...@@ -35,7 +35,7 @@ public class RedissonLockAspect { ...@@ -35,7 +35,7 @@ public class RedissonLockAspect {
RedissonClient redissonClient; RedissonClient redissonClient;
@Pointcut("@annotation(com.mushiny.pubtools.annotation.RedissonLock)") @Pointcut("@annotation(com.mushiny.redisson.annotation.RedissonLock)")
public void pointCut() { public void pointCut() {
} }
......
package com.mushiny.pubtools.config; package com.mushiny.redisson.config;
import org.redisson.Redisson; import org.redisson.Redisson;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
...@@ -16,13 +16,13 @@ import org.springframework.context.annotation.Configuration; ...@@ -16,13 +16,13 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class RedissonManager { public class RedissonManager {
@Value("${spring.redis.host}") @Value("${spring.redis.host:192.168.3.104}")
private String addresses; private String addresses;
@Value("${spring.redis.password}") @Value("${spring.redis.password:q1w2e3r4}")
private String password; private String password;
@Value("${spring.redis.port}") @Value("${spring.redis.port:6379}")
private String port; private String port;
@Bean(destroyMethod = "shutdown") @Bean(destroyMethod = "shutdown")
......
package com.mushiny.redisson.controller;
import com.mushiny.redisson.enums.LockType;
import com.mushiny.redisson.utils.RedissonLockUtil;
import com.mushiny.redisson.utils.ResultForLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 描述信息
*
* @auther lxy
* @since 2023/8/10 13:03
*/
@RestController
public class TestController {
@Autowired
RedissonLockUtil redissonLockUtil;
@Autowired
JdbcTemplate jdbcTemplate;
public int count = 0;
@PostMapping ("api/ping")
public ResultForLock<String> contextLoads() {
String threadId = Thread.currentThread().getName() + Thread.currentThread().getId();
redissonLockUtil.getLockByKey("265213225444411", LockType.REENTRANT_LOCK.value());
Boolean getLock = redissonLockUtil.tryLock(50000L, "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("拿锁失败");
}
return new ResultForLock().ok(threadId);
}
}
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* 版权所有,侵权必究! * 版权所有,侵权必究!
*/ */
package com.mushiny.pubtools.enums; package com.mushiny.redisson.enums;
/** /**
* 行政区域 级别枚举 * 行政区域 级别枚举
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* 版权所有,侵权必究! * 版权所有,侵权必究!
*/ */
package com.mushiny.pubtools.execption; package com.mushiny.redisson.execption;
/** /**
* 错误编码,由5位数字组成,前2位为模块编码,后3位为业务编码 * 错误编码,由5位数字组成,前2位为模块编码,后3位为业务编码
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* 版权所有,侵权必究! * 版权所有,侵权必究!
*/ */
package com.mushiny.pubtools.execption; package com.mushiny.redisson.execption;
/** /**
......
package com.mushiny.pubtools.utils; package com.mushiny.redisson.utils;
import com.mushiny.pubtools.execption.RedissonErrorCode; import com.mushiny.redisson.execption.RedissonErrorCode;
import com.mushiny.pubtools.execption.RedissonException; import com.mushiny.redisson.execption.RedissonException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock; import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
...@@ -73,7 +73,7 @@ public class RedissonLockUtil { ...@@ -73,7 +73,7 @@ public class RedissonLockUtil {
lock = rLock.tryLock(waitTime, TimeUnit.MILLISECONDS); lock = rLock.tryLock(waitTime, 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();
......
/*
* Copyright (c) 2020 牧星仓库管理系统 All rights reserved.
*
* http://www.mushiny.com
*
* 版权所有,侵权必究!
*/
package com.mushiny.redisson.utils;
import lombok.Data;
import java.io.Serializable;
/**
* 响应数据
*
* @author Elen elen.shen@mushiny.comn
* @since 2.1.0
*/
@Data
public class ResultForLock<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 编码:0表示成功,其他值表示失败
*/
private int code = 0;
/**
* 消息内容
*/
private String msg = "success";
/**
* 响应数据
*/
private T data;
public ResultForLock<T> ok(T data) {
this.setData(data);
return this;
}
public boolean success() {
return code == 0;
}
public ResultForLock<T> error() {
this.code = 500;
this.msg = "error";
return this;
}
public ResultForLock<T> error(int code) {
this.code = 500;
this.msg = "error";
return this;
}
public ResultForLock<T> error(int code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResultForLock<T> error(String msg) {
this.code = 500;
this.msg = "error";
return this;
}
}
package com.mushiny.pubtools.utils; package com.mushiny.redisson.utils;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import java.util.UUID;
/** /**
* 描述信息 * 描述信息
...@@ -8,14 +13,14 @@ package com.mushiny.pubtools.utils; ...@@ -8,14 +13,14 @@ package com.mushiny.pubtools.utils;
*/ */
public class SnowFlakeUtil { public class SnowFlakeUtil {
/** /**
* 起始时间戳,从2021-12-01开始生成 * 起始时间戳,从2023-08-01开始生成
*/ */
private final static long START_STAMP = 1638288000000L; private final static long START_STAMP = 1690819200000L;
/** /**
* 序列号占用的位数 12 * 序列号占用的位数 12
*/ */
private final static long SEQUENCE_BIT = 12; private final static long SEQUENCE_BIT = 22;
/** /**
* 机器标识占用的位数 * 机器标识占用的位数
...@@ -91,6 +96,32 @@ public class SnowFlakeUtil { ...@@ -91,6 +96,32 @@ public class SnowFlakeUtil {
| sequence; // 序列号部分 | sequence; // 序列号部分
} }
/**
* 产生下一个String类型的ID
*/
public synchronized String nextStrId() {
String prefix = RandomUtil.randomString(10);
long currStamp = getNewStamp();
if (currStamp < lastStamp) {
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() {
long mill = getNewStamp(); long mill = getNewStamp();
while (mill <= lastStamp) { while (mill <= lastStamp) {
...@@ -103,11 +134,5 @@ public class SnowFlakeUtil { ...@@ -103,11 +134,5 @@ public class SnowFlakeUtil {
return System.currentTimeMillis(); return System.currentTimeMillis();
} }
public static void main(String[] args) {
// 192.168.3.104 192.168.3.221测试生成12位id
SnowFlakeUtil snowFlake1 = new SnowFlakeUtil(104);
System.out.println(snowFlake1.nextId());
SnowFlakeUtil snowFlake2 = new SnowFlakeUtil(221);
System.out.println(snowFlake2.nextId());
}
} }
#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
#
#
#
#
package pubtools; package pubtools;
import com.mushiny.pubtools.enums.LockType; import com.mushiny.redisson.enums.LockType;
import com.mushiny.pubtools.utils.RedissonLockUtil; import com.mushiny.redisson.utils.RedissonLockUtil;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
......
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