forked from doufuplus/springboot-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
710 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# SpringBoot整合Activemq(一)快速集成 | ||
|
||
文章地址:[SpringBoot整合Activemq(一)快速集成](https://www.doufuplus.com/blog/activemq01.html) | ||
|
||
更多最新文章欢迎大家访问我的个人博客:blush::[豆腐别馆](https://www.doufuplus.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
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"> | ||
<parent> | ||
<artifactId>springboot-master</artifactId> | ||
<groupId>com.doufuplus</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>springboot-lock</artifactId> | ||
|
||
<dependencies> | ||
|
||
<!-- 子工程(common) --> | ||
<dependency> | ||
<groupId>com.doufuplus</groupId> | ||
<artifactId>springboot-common</artifactId> | ||
<version>${doufuplus.version}</version> | ||
</dependency> | ||
|
||
<!-- 子工程(redis) --> | ||
<dependency> | ||
<groupId>com.doufuplus</groupId> | ||
<artifactId>springboot-redis</artifactId> | ||
<version>${doufuplus.version}</version> | ||
</dependency> | ||
|
||
<!-- Jedis | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
<version>${jedis.version}</version> | ||
</dependency> | ||
--> | ||
|
||
<!-- web模块 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 测试模块,包括JUnit、Hamcrest、Mockito --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- mysql连接器 --> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
</dependency> | ||
|
||
<!-- druid连接池 --> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>${druid.version}</version> | ||
</dependency> | ||
|
||
<!-- myBatis plus starter --> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-boot-starter</artifactId> | ||
<version>${mybatis-plus-boot.version}</version> | ||
</dependency> | ||
|
||
<!-- myBatis plus --> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus</artifactId> | ||
<version>${mybatis-plus.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-integration</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.integration</groupId> | ||
<artifactId>spring-integration-redis</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
23 changes: 23 additions & 0 deletions
23
springboot-lock/src/main/java/com/doufuplus/boot/WebApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.doufuplus.boot; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* 启动类 | ||
* 转载请注明出处,更多技术文章欢迎大家访问我的个人博客站点:https://www.doufuplus.com | ||
* | ||
* @author 丶doufu | ||
* @date 2019/08/22 | ||
*/ | ||
@SpringBootApplication | ||
@MapperScan({"com.doufuplus.boot.mapper"}) | ||
public class WebApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(WebApplication.class, args); | ||
} | ||
|
||
} | ||
|
14 changes: 14 additions & 0 deletions
14
springboot-lock/src/main/java/com/doufuplus/boot/config/RedisLockConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.doufuplus.boot.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.integration.redis.util.RedisLockRegistry; | ||
|
||
@Configuration | ||
public class RedisLockConfiguration { | ||
@Bean | ||
public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) { | ||
return new RedisLockRegistry(redisConnectionFactory, "doufuplus"); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
springboot-lock/src/main/java/com/doufuplus/boot/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.doufuplus.boot.controller; | ||
|
||
import com.doufuplus.boot.constant.ResultCode; | ||
import com.doufuplus.boot.po.Result; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* TestController | ||
* 转载请注明出处,更多技术文章欢迎大家访问我的个人博客站点:https://www.doufuplus.com | ||
* | ||
* @author 丶doufu | ||
* @date 2019/08/22 | ||
*/ | ||
@RestController | ||
public class TestController { | ||
|
||
@RequestMapping("/test") | ||
public Result test() { | ||
return new Result(ResultCode.SUCCESS); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
springboot-lock/src/main/java/com/doufuplus/boot/entity/LockEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.doufuplus.boot.entity; | ||
|
||
import com.baomidou.mybatisplus.annotation.TableName; | ||
import org.springframework.data.annotation.Id; | ||
|
||
/** | ||
* 锁实体 | ||
* 转载请注明出处,更多技术文章欢迎大家访问我的个人博客站点:https://www.doufuplus.com | ||
* | ||
* @author 丶doufu | ||
* @date 2019/08/22 | ||
*/ | ||
@TableName("t_lock") | ||
public class LockEntity { | ||
|
||
/** | ||
* ID | ||
*/ | ||
@Id | ||
private int id; | ||
|
||
|
||
public LockEntity() { | ||
} | ||
|
||
public LockEntity(int id) { | ||
this.id = id; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
springboot-lock/src/main/java/com/doufuplus/boot/lock/MysqlLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.doufuplus.boot.lock; | ||
|
||
import com.doufuplus.boot.entity.LockEntity; | ||
import com.doufuplus.boot.mapper.LockMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
@Service("mysqlLock") | ||
public class MysqlLock implements Lock { | ||
|
||
@Autowired | ||
private LockMapper lockMapper; | ||
|
||
private static final int NUM = 1; | ||
|
||
private static final String MODULE = "LOCK"; | ||
|
||
// 尝试次数 | ||
private int count = 0; | ||
|
||
/** | ||
* 阻塞锁 | ||
*/ | ||
@Override | ||
public void lock() { | ||
if (!tryLock()) { | ||
try { | ||
Thread.sleep(new Random().nextInt(10) + 1); | ||
count++; | ||
} catch (InterruptedException e) { | ||
// e.printStackTrace(); | ||
} | ||
if (count < 8) { | ||
lock(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 非阻塞锁 | ||
*/ | ||
@Override | ||
public boolean tryLock() { | ||
try { | ||
lockMapper.insert(new LockEntity(NUM)); | ||
} catch (Exception e) { | ||
// e.printStackTrace(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void unlock() { | ||
lockMapper.deleteById(NUM); | ||
} | ||
|
||
// ---------- | ||
|
||
@Override | ||
public void lockInterruptibly() throws InterruptedException { | ||
} | ||
|
||
@Override | ||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Condition newCondition() { | ||
return null; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
springboot-lock/src/main/java/com/doufuplus/boot/lock/RedisLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//package com.doufuplus.boot.lock; | ||
// | ||
//import java.util.Arrays; | ||
//import java.util.Random; | ||
//import java.util.UUID; | ||
//import java.util.concurrent.TimeUnit; | ||
//import java.util.concurrent.locks.Condition; | ||
//import java.util.concurrent.locks.Lock; | ||
// | ||
//import javax.annotation.Resource; | ||
// | ||
//import com.doufuplus.boot.util.FileUtils; | ||
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | ||
//import org.springframework.stereotype.Service; | ||
// | ||
//import redis.clients.jedis.Jedis; | ||
// | ||
///** | ||
// * Redis分布式锁 | ||
// * | ||
// * @author lwx | ||
// */ | ||
//@Service("redisLock") | ||
//public class RedisLock implements Lock { | ||
// | ||
// @Resource | ||
// private JedisConnectionFactory factory; | ||
// | ||
// // 锁名称 | ||
// private static final String LOCK_NAME = "LOCK"; | ||
// | ||
// // 保存策略【NX|XX】:NX——只有在键不存在时才设置,XX——只有在键已经存在时才设置它 | ||
// private static final String NXXX = "NX"; | ||
// | ||
// // 过期时间单位【EX|PX】:EX=秒,PX=毫秒 | ||
// private static final String EXPX = "PX"; | ||
// | ||
// // 用于保存线程级变量 | ||
// private ThreadLocal<String> local = new ThreadLocal<String>(); | ||
// | ||
// // 尝试次数 | ||
// private int count = 0; | ||
// | ||
// @Override | ||
// public void lock() { | ||
// if (tryLock()) { | ||
// return; | ||
// } | ||
// try { | ||
// Thread.sleep(new Random().nextInt(10) + 1); | ||
// count++; | ||
// } catch (InterruptedException e) { | ||
// e.printStackTrace(); | ||
// } | ||
// if (count < 8) { | ||
// lock(); | ||
// } | ||
// } | ||
// | ||
// @Override | ||
// public boolean tryLock() { | ||
// // 1、随机值,避免误删锁 | ||
// String uuid = UUID.randomUUID().toString(); | ||
// // 2、保存至当前线程 | ||
// local.set(uuid); | ||
// // 3、设置失效时间、使用原子操作保存锁 | ||
// Jedis jedis = (Jedis) factory.getConnection().getNativeConnection(); | ||
// String result = jedis.set(LOCK_NAME, uuid, NXXX, EXPX, 3 * 1000); | ||
// if ("OK".equals(result)) { | ||
// return true; | ||
// } | ||
// return false; | ||
// } | ||
// | ||
// @Override | ||
// public void unlock() { | ||
// // 1、获取lua脚本 | ||
// String script = FileUtils.readFileByLines(this.getClass().getResource("/").getPath() + "/unlock.lua"); | ||
// // 2、获取线程内uuid变量 | ||
// String uuid = local.get(); | ||
// // 3、删除redis锁 | ||
// Jedis jedis = (Jedis) factory.getConnection().getNativeConnection(); | ||
// jedis.eval(script, Arrays.asList(LOCK_NAME), Arrays.asList(uuid)); | ||
// } | ||
// | ||
// // ---------------------------------- | ||
// @Override | ||
// public void lockInterruptibly() throws InterruptedException { | ||
// } | ||
// | ||
// @Override | ||
// public Condition newCondition() { | ||
// return null; | ||
// } | ||
// | ||
// @Override | ||
// public boolean tryLock(long arg0, TimeUnit arg1) throws InterruptedException { | ||
// return false; | ||
// } | ||
//} |
15 changes: 15 additions & 0 deletions
15
springboot-lock/src/main/java/com/doufuplus/boot/mapper/LockMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.doufuplus.boot.mapper; | ||
|
||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import com.doufuplus.boot.entity.LockEntity; | ||
|
||
/** | ||
* LockMapper | ||
* 转载请注明出处,更多技术文章欢迎大家访问我的个人博客站点:https://www.doufuplus.com | ||
* | ||
* @author 丶doufu | ||
* @date 2019/08/22 | ||
*/ | ||
public interface LockMapper extends BaseMapper<LockEntity> { | ||
|
||
} |
Oops, something went wrong.