2021-06-21 22:45:05 +08:00
|
|
|
|
# Sa-Token-Alone-Redis 独立Redis插件
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
Sa-Token默认的Redis集成方式会把权限数据和业务缓存放在一起,但在部分场景下我们需要将他们彻底分离开来,比如:
|
|
|
|
|
|
|
|
|
|
> 搭建两个Redis服务器,一个专门用来做业务缓存,另一台专门存放Sa-Token权限数据
|
|
|
|
|
|
|
|
|
|
要将Sa-Token的数据单独抽离出来很简单,你只需要为Sa-Token单独配置一个Redis连接信息即可
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 1、首先引入Alone-Redis依赖
|
|
|
|
|
|
|
|
|
|
``` xml
|
2021-06-26 23:30:29 +08:00
|
|
|
|
<!-- Sa-Token插件:权限缓存与业务缓存分离 -->
|
2021-06-21 22:45:05 +08:00
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>cn.dev33</groupId>
|
|
|
|
|
<artifactId>sa-token-alone-redis</artifactId>
|
2021-07-02 17:33:31 +08:00
|
|
|
|
<version>1.21.0</version>
|
2021-06-21 22:45:05 +08:00
|
|
|
|
</dependency>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 2、然后在application.yml中增加配置
|
|
|
|
|
``` yml
|
2021-07-01 15:15:54 +08:00
|
|
|
|
# Sa-Token配置
|
|
|
|
|
sa-token:
|
|
|
|
|
# Token名称
|
|
|
|
|
token-name: satoken
|
|
|
|
|
# Token有效期
|
|
|
|
|
timeout: 2592000
|
|
|
|
|
# Token风格
|
|
|
|
|
token-style: uuid
|
|
|
|
|
|
|
|
|
|
# 配置Sa-Token单独使用的Redis连接
|
|
|
|
|
alone-redis:
|
|
|
|
|
# Redis数据库索引(默认为0)
|
|
|
|
|
database: 2
|
|
|
|
|
# Redis服务器地址
|
|
|
|
|
host: 127.0.0.1
|
|
|
|
|
# Redis服务器连接端口
|
|
|
|
|
port: 6379
|
|
|
|
|
# Redis服务器连接密码(默认为空)
|
|
|
|
|
password:
|
|
|
|
|
# 连接超时时间(毫秒)
|
|
|
|
|
timeout: 10ms
|
|
|
|
|
lettuce:
|
|
|
|
|
pool:
|
|
|
|
|
# 连接池最大连接数
|
|
|
|
|
max-active: 200
|
|
|
|
|
# 连接池最大阻塞等待时间(使用负值表示没有限制)
|
|
|
|
|
max-wait: -1ms
|
|
|
|
|
# 连接池中的最大空闲连接
|
|
|
|
|
max-idle: 10
|
|
|
|
|
# 连接池中的最小空闲连接
|
|
|
|
|
min-idle: 0
|
2021-06-21 22:45:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
具体可参考:[码云:application.yml](https://gitee.com/dromara/sa-token/blob/dev/sa-token-demo/sa-token-demo-alone-redis/src/main/resources/application.yml)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 3、测试
|
|
|
|
|
新建Controller测试一下
|
|
|
|
|
``` java
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/test/")
|
|
|
|
|
public class TestController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
|
|
|
// 测试Sa-Token缓存
|
|
|
|
|
@RequestMapping("login")
|
|
|
|
|
public AjaxJson login(@RequestParam(defaultValue="10001") String id) {
|
|
|
|
|
System.out.println("--------------- 测试Sa-Token缓存");
|
|
|
|
|
StpUtil.login(id);
|
|
|
|
|
return AjaxJson.getSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 测试业务缓存
|
|
|
|
|
@RequestMapping("test")
|
|
|
|
|
public AjaxJson test() {
|
|
|
|
|
System.out.println("--------------- 测试业务缓存");
|
|
|
|
|
stringRedisTemplate.opsForValue().set("hello", "Hello World");
|
|
|
|
|
return AjaxJson.getSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
分别访问两个接口,观察Redis中增加的数据
|
|
|
|
|
|
|
|
|
|
![alone-redis](https://oss.dev33.cn/sa-token/doc/alone-redis.png 's-w')
|
|
|
|
|
|
|
|
|
|
测试完毕!
|