RedisLock——讓 Redis 分布式鎖變得簡單
- 1. 專案介紹
- 2. 快速使用
- 2.1 引入 maven 坐標
- 2.2 注冊 RedisLock
- 2.3 使用
- 3. 參與貢獻
- 4. 聯系作者
- 5. 開源協議
1. 專案介紹
該專案主要簡化了使用 redis 分布式事務所的操作,實作傻瓜式加鎖,釋放鎖的操作,并優雅的實作了等待鎖釋放的操作,等待鎖釋放的程序主要是使用了redis的監聽功能,所以在使用該專案前,要確保redis已經開啟了key事件監聽,即“Ex”,
- 如何查看 redis 是否已經開啟了監聽功能?
登錄 redis 后,使用命令config get notify-keyspace-events進行查看
github地址:https://github.com/chimmhuang/redislock
碼云地址:https://gitee.com/chimmhuang/redislock
歡迎 Start、Fork~
2. 快速使用
2.1 引入 maven 坐標
<dependency>
<groupId>com.github.chimmhuang</groupId>
<artifactId>redislock</artifactId>
<version>1.0.2</version>
</dependency>
2.2 注冊 RedisLock
- 方式一(推薦): 在專案的啟動類上添加包掃描的路徑
@ComponentScan(basePackages = "com.github.chimmhuang.redislock")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 方式二:手動注冊相關的 bean
@Configuration
public class RedisConfig {
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
@Bean
public RedisListener redisListener(RedisMessageListenerContainer redisMessageListenerContainer) {
return new RedisListener(redisMessageListenerContainer);
}
@Bean
public RedisLock redisLock(RedisTemplate redisTemplate) {
return new RedisLock(redisTemplate);
}
}
2.3 使用
- 注入
redisLock - 使用
redisLock.lock(key,expire)進行加鎖 - 使用
redisLock.unlock(key)進行解鎖
以下提供一個單元測驗的案例(火車站賣票的案例)
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisListenerTest {
@Autowired
private RedisLock redisLock;
/** 100張票 */
private static Integer count = 100;
@Test
public void ticketTest() throws Exception {
TicketRunnable tr = new TicketRunnable();
// 四個執行緒對應四個視窗
Thread t1 = new Thread(tr,"視窗A");
Thread t2 = new Thread(tr,"視窗B");
Thread t3 = new Thread(tr,"視窗C");
Thread t4 = new Thread(tr,"視窗D");
t1.start();
t2.start();
t3.start();
t4.start();
Thread.currentThread().join();
}
public class TicketRunnable implements Runnable {
@Override
public void run() {
while (count > 0) {
redisLock.lock("ticketLock", 3L);
if (count > 0) {
System.out.println(Thread.currentThread().getName() + "售出第" + (count--) + "張火車票");
}
redisLock.unlock("ticketLock");
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
3. 參與貢獻
非常歡迎你的加入!提一個 Issue 或者提交一個 Pull Request,
目前僅僅是實作了加鎖解鎖的簡單程序,還有其他操作有待完善和測驗,如:
-[ ] 在 redis 的集群環境中,需要監聽每一個 redis 的 key 事件
-[ ] 在 redis 的主備模式下,可能會存在主備 redis 切換的期間,資料(key)未同步過去問題
4. 聯系作者
QQ(Wechat) : 905369866
Email : [email protected]
5. 開源協議
MIT ? Chimm Huang
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/170781.html
標籤:Java
