最近遇到一个比较紧急的任务,要求统计在线用户,目的是配合性能测评,要求证明自己系统的在线用户能够达标,不过系统因为历史原因,并没有这个功能,所以只能去springSession官网和网上搜资料,想到通过统计Redis里缓存的数据
因为系统原先的逻辑是使用Spring Session加上Redis做的会话共享实现的单点登录,登录之后会在session设置一个key值表示用户已经登录过,同时重写HttpServletRequestWrapper 设置remoteUser数据值
class RemoteUserRequestWrapper extends HttpServletRequestWrapper {
String userCode;
RemoteUserRequestWrapper(HttpServletRequest request) {
super(request);
this.userCode = (String) request.getSession()
.getAttribute(org.apache.commons.lang3.StringUtils.isBlank(sessionKeyName)?DEFAULT_SESSION_KEY_NAME:sessionKeyName);
}
@Override
public String getRemoteUser() {
return userCode;
}
}
Spring Session缓存在redis里的数据
这个ssoLoginUser
key是自己登录时候设置的,根据业务修改,经过测试,在登出系统时候,session设置过期获取removeAttribute不能清redis里的key数据,所以只能在登出系统逻辑加上:
Set<String> keys = RedisUtils.redisTemplate.keys("spring:session:sessions:*");
for(String key : keys){
if(key.indexOf("expires")==-1){
String s = (String)RedisUtils.redisTemplate.opsForHash().get(key, "sessionAttr:ssoLoginUser");
if(request.getRemoteUser().equals(s)) {
logger.info("loginusername:{}",s)
RedisUtils.redisTemplate.opsForHash().delete(key, "sessionAttr:ssoLoginUser");
}
}
}
进行数据统计:
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
List<Map<String,Object>> data = new ArrayList<Map<String, Object>>();
Set<String> keys = redisTemplate.keys("spring:session:sessions:*");
for(String key : keys){
if(key.indexOf("expires")==-1){
String s = (String)redisTemplate.opsForHash().get(key, "sessionAttr:ssoLoginUser");
if(StringUtils.isNotBlank(s)) {
System.out.println(s);
Map<String,Object> map = new HashMap<String,Object>(16);
map.put("usercode", s);
list.add(map);
}
}
}
return list;
pom.XML:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.2.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>3.5.0.Final</version>
</dependency>
RedisUtils.Java:
package com.common.utils.redis;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class RedisUtils {
private RedisUtils() {
}
@SuppressWarnings("unchecked")
public static RedisTemplate<String, Object> redisTemplate =
ContextLoader.getCurrentWebApplicationContext().getBean(RedisTemplate.class);
public static boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
public static boolean expire(final String key, final long timeout, final TimeUnit unit) {
Boolean ret = redisTemplate.expire(key, timeout, unit);
return ret != null && ret;
}
public static boolean del(final String key) {
redisTemplate.delete(key);
return true;
}
public static long del(final Collection<String> keys) {
redisTemplate.delete(keys);
return 0;
}
public static void set(final String key, final Object value) {
redisTemplate.opsForValue().set(key, value, 1, TimeUnit.MINUTES);
}
// 存储普通对象操作
public static void set(final String key, final Object value, final long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
public static Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
// 存储Hash操作
public static void hPut(final String key, final String hKey, final Object value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
public static void hPutAll(final String key, final Map<String, Object> values) {
redisTemplate.opsForHash().putAll(key, values);
}
public static Object hGet(final String key, final String hKey) {
return redisTemplate.opsForHash().get(key, hKey);
}
public static List<Object> hMultiGet(final String key, final Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
// 存储Set相关操作
public static long sSet(final String key, final Object... values) {
Long count = redisTemplate.opsForSet().add(key, values);
return count == null ? 0 : count;
}
public static long sDel(final String key, final Object... values) {
Long count = redisTemplate.opsForSet().remove(key, values);
return count == null ? 0 : count;
}
// 存储List相关操作
public static long lPush(final String key, final Object value) {
Long count = redisTemplate.opsForList().rightPush(key, value);
return count == null ? 0 : count;
}
public static long lPushAll(final String key, final Collection<Object> values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
return count == null ? 0 : count;
}
public static long lPushAll(final String key, final Object... values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
return count == null ? 0 : count;
}
public static List<Object> lGet(final String key, final int start, final int end) {
return redisTemplate.opsForList().range(key, start, end);
}
}
ok,本博客只能学习参考,因为只是要给客户一些在线用户的证明而已,这个临时的统计不能用于生产,要做比较齐全的在线用户统计,需要花多点时间,有问题希望能指出。ok,简单记录一下,方便之后自己回顾
到此这篇关于SpringSession通过Redis统计在线用户数量的文章就介绍到这了,更多相关Redis在线用户数量内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!