欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > springboot项目Redis统计在线用户

springboot项目Redis统计在线用户

2025/2/7 21:43:51 来源:https://blog.csdn.net/w18478272407/article/details/145451131  浏览:    关键词:springboot项目Redis统计在线用户

SEO Meta Description: 了解如何在Spring Boot项目中使用Redis实现在线用户统计,提供详细的实现步骤和代码示例,帮助您高效管理在线用户。

介绍

在现代Web应用中,统计在线用户是一个常见需求。通过Redis可以高效地管理和统计在线用户。本文将详细介绍如何在Spring Boot项目中使用Redis统计在线用户,包括配置Redis、实现用户登录和注销逻辑,以及统计在线用户数。

环境准备

在开始之前,请确保您的开发环境中已经安装并配置了以下组件:

  • Java 8或以上版本
  • Spring Boot
  • Redis服务器
  • Maven或Gradle

配置Redis

添加依赖

在 pom.xml中添加Redis和Spring Data Redis的依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
​

配置Redis连接

在 application.properties文件中配置Redis连接信息:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=your_password # 如果有设置密码
​

实现用户在线统计

Redis配置类

创建一个Redis配置类,配置RedisTemplate:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}
​

用户服务类

创建一个服务类,处理用户登录、注销和在线用户统计逻辑:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.Set;
import java.util.concurrent.TimeUnit;@Service
public class UserService {private static final String ONLINE_USERS_KEY = "onlineUsers";@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void loginUser(String userId) {redisTemplate.opsForSet().add(ONLINE_USERS_KEY, userId);redisTemplate.expire(ONLINE_USERS_KEY, 30, TimeUnit.MINUTES); // 设置过期时间}public void logoutUser(String userId) {redisTemplate.opsForSet().remove(ONLINE_USERS_KEY, userId);}public Set<Object> getOnlineUsers() {return redisTemplate.opsForSet().members(ONLINE_USERS_KEY);}public Long getOnlineUserCount() {return redisTemplate.opsForSet().size(ONLINE_USERS_KEY);}
}
​

控制器类

创建一个控制器类,处理前端请求:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.Set;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@PostMapping("/login")public String loginUser(@RequestParam String userId) {userService.loginUser(userId);return "User logged in: " + userId;}@PostMapping("/logout")public String logoutUser(@RequestParam String userId) {userService.logoutUser(userId);return "User logged out: " + userId;}@GetMapping("/online")public Set<Object> getOnlineUsers() {return userService.getOnlineUsers();}@GetMapping("/online/count")public Long getOnlineUserCount() {return userService.getOnlineUserCount();}
}
​

运行和测试

启动应用

启动Spring Boot应用,确保Redis服务器正在运行。

测试API

使用Postman或其他API测试工具,测试以下API:

  1. 登录用户

    • URL: POST http://localhost:8080/users/login
    • 参数: userId
    {"userId": "user1"
    }
    ​
    
  2. 注销用户

    • URL: POST http://localhost:8080/users/logout
    • 参数: userId
    {"userId": "user1"
    }
    ​
    
  3. 获取在线用户列表

    • URL: GET http://localhost:8080/users/online
  4. 获取在线用户数量

    • URL: GET http://localhost:8080/users/online/count

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com