前言
redisson 是基于redis的扩展库(看门狗),使得redis除了应用于缓存以外,还能做队列等数据结构,直接使用的分布式锁,以及人物调度器等。
**
redisson单机的连接
yml
<!--redisson-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.15.0</version>
<optional>true</optional>
</dependency>
Config
@Bean(destroyMethod="shutdown")
public RedissonClient redisson() throws IOException {
Config config = new Config();
config.useSingleServer().setAddress("redis://121.5.147.69:6379").setPassword("121514769");
return Redisson.create();
}
LoginService
/**
* @author lanys
* @Description:
* @date 16/8/2021 下午2:38
*/
@Component
@Slf4j
public class LoginService {
@Autowired
private RedissonClient redissonClient;
@Autowired
private RedisUtil redisUtil;
/**
* 用户登录的逻辑处理
*
* @param username 账号
* @param password 密码
* @return
*/
public String loginService(String username, String password){
//定义锁
RLock lock = redissonClient.getLock(StrUtil.format(CacheKey.LOGIN_LOCK));
//加锁
lock.lock();
String token = null;
try {
SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password));
String data = (String) redisUtil.get(StrUtil.format(CacheKey.TOKEN,username));
if (data != null){
redisUtil.expire(StrUtil.format(CacheKey.TOKEN,username),Constraint.DAY_SECOND);
log.info("------- 登录重新更新token:{} -------",data);
return data;
}
token = TokenUtil.doGenerateToken(SecurityLogic.getName(), SecurityLogic.getPassword());
redisUtil.set(StrUtil.format(CacheKey.TOKEN, username),token, Constraint.DAY_SECOND, TimeUnit.SECONDS);
log.info("------- 登录重新生成token -------",token);
}catch (Exception e){
log.error("-------- 数据异常 -------",e);
}
finally {
//释放锁
lock.unlock();
}
return token;
}
}
推荐apache-jmeter(压测工具)
启动
总结
结束!