83 lines
3.7 KiB
Markdown
83 lines
3.7 KiB
Markdown
# 精尽 Redisson 源码分析 —— 调试环境搭建
|
||
|
||
> 本文基于 Redisson `3.11.4-SNAPSHOT` 版本,望知悉。
|
||
>
|
||
> 可能有些胖友不是很了解 Redisson ,可以看看 [Redis 客户端 Redisson](https://www.oschina.net/p/redisson) 。
|
||
|
||
# 1. 依赖工具
|
||
|
||
- Maven
|
||
- Git
|
||
- JDK
|
||
- IntelliJ IDEA
|
||
|
||
另外,胖友需要启动一个 Redis 节点。例如说,艿艿使用默认配置,在 127.0.0.1:6379 启动了一个。
|
||
|
||
# 2. 源码拉取
|
||
|
||
从官方仓库 https://github.com/redisson/redisson Fork 出属于自己的仓库。
|
||
|
||
- 为什么要 Fork ?既然开始阅读、调试源码,我们可能会写一些注释,有了自己的仓库,可以进行自由的提交。😈
|
||
|
||
在拉取项目的过程中,我们来看看每个 Redisson 的子项目:
|
||
|
||
- [redisson](https://github.com/redisson/redisson/tree/master/redisson) :Redisson 实现。
|
||
|
||
- [redisson-tomcat](https://github.com/redisson/redisson/tree/master/redisson-tomcat) :Redisson 集成到 Tomcat 中,实现分布式 Session 功能。
|
||
|
||
- [redisson-spring-data](https://github.com/redisson/redisson) :Redisson 集成到 Spring Data Redis 中。
|
||
|
||
> 不了解 Spring Data Redis 的胖友,可以看看艿艿写的 [《芋道 Spring Boot Redis 入门》](http://www.iocoder.cn/Spring-Boot/Redis/?vip) 。
|
||
|
||
- [redisson-spring-boot-starter](https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter) :Redisson 实现的 Spring Boot Starter ,方便我们在使用 Spring Boot 的项目中,自动化配置 Redisson 。
|
||
|
||
- [redisson-hibernate](https://github.com/redisson/redisson) :Redisson 集成到 Hibernate 中,提供二级缓存的功能。
|
||
|
||
😈 Redisson 团队,还是很勤劳的,**主动**集成了这么多框架。当然,也是因为 Redisson 出的比较晚,前有 Jedis 后有 Lettuce ,不努力点可能无法被开发者所接受。
|
||
|
||
# 3. 测试运行
|
||
|
||
在测试目录下,创建 [YunaiDebugDemo](https://github.com/YunaiV/redisson/blob/master/redisson/src/test/java/org/redisson/yunai/YunaiDebugDemo.java) 类,编写代码如下:
|
||
|
||
```
|
||
public class YunaiDebugDemo {
|
||
|
||
public static void main(String[] args) throws InterruptedException {
|
||
// 创建 RedissonClient 对象
|
||
RedissonClient client = Redisson.create();
|
||
|
||
// 创建 RRateLimiter 对象
|
||
RRateLimiter rateLimiter = client.getRateLimiter("myRateLimiter");
|
||
// 初始化:最大流速 = 每 1 分钟产生 2 个令牌
|
||
rateLimiter.trySetRate(RateType.OVERALL, 2, 1, RateIntervalUnit.SECONDS);
|
||
|
||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
for (int i = 0; i < 5; i++) {
|
||
System.out.println(String.format("%s:获得锁结果(%s)", simpleDateFormat.format(new Date()),
|
||
rateLimiter.tryAcquire()));
|
||
Thread.sleep(250L);
|
||
}
|
||
}
|
||
|
||
}
|
||
```
|
||
|
||
在参考 [《Redisson 文档 —— 限流器(RateLimiter)》](https://github.com/redisson/redisson/wiki/6.-分布式对象#612-限流器ratelimiter) ,艿艿编写了一个简单的限流,进行测试。点击运行,输出结果如下:
|
||
|
||
```
|
||
2019-10-02 10:48:00:获得锁结果(true)
|
||
2019-10-02 10:48:01:获得锁结果(true)
|
||
2019-10-02 10:48:01:获得锁结果(false)
|
||
2019-10-02 10:48:01:获得锁结果(false)
|
||
2019-10-02 10:48:02:获得锁结果(true)
|
||
```
|
||
|
||
后续,胖友自己 Debug 运行即可,想调哪个调哪个。
|
||
|
||
# 666. 彩蛋
|
||
|
||
Redisson 的源码解析,会更新的比较随缘。艿艿自己项目中,还是只使用 Jedis 哈。不过考虑在 [onemall](https://github.com/YunaiV/onemall) 中,尝试使用下 Redisson ,嘿嘿。
|
||
|
||
如果胖友想看 Redisson 功能的源码解析,可以在星球给我留言。
|
||
|
||
😈 本文有点水更,大家 2019-10-02 国庆快乐。 |