免费域名x网站,有哪些做应援的网站,网站关键词有什么用,电子商务网站建设的范围是什么意思一、引言
在当今快速发展的软件开发领域#xff0c;系统的性能和可靠性至关重要。Springboot 3 整合 Redis 7 集群具有多方面的重大意义。
首先#xff0c;随着业务的不断发展#xff0c;数据量呈爆炸式增长#xff0c;单个 Redis 服务器往往难以满足存储和处理需求。Red…一、引言
在当今快速发展的软件开发领域系统的性能和可靠性至关重要。Springboot 3 整合 Redis 7 集群具有多方面的重大意义。
首先随着业务的不断发展数据量呈爆炸式增长单个 Redis 服务器往往难以满足存储和处理需求。Redis 7 集群通过将数据分布在多个节点上实现了数据的 横向扩展能够轻松应对大规模数据的存储和访问。
对于高并发的应用场景性能是关键考量因素。Springboot 3 与 Redis 7 集群整合后可以充分利用 Redis 的高性能缓存特性。Redis 以其快速的读写速度著称能够极大地减少数据库的访问压力提高系统的响应速度。例如在电商平台中商品信息、用户购物车等数据可以缓存到 Redis 集群中当用户查询或操作这些数据时直接从 Redis 中获取响应时间可以从几百毫秒降低到几毫秒大大提升用户体验。
可靠性方面Redis 7 集群提供了高可用性。如果其中一个 Redis 节点出现故障其他节点仍然可以继续提供服务确保系统的稳定运行。在金融交易系统、在线游戏等对稳定性要求极高的场景中这种高可用性至关重要。数据会在多个节点之间进行复制即使某个节点发生故障数据也不会丢失保证了数据的一致性和可用性。
此外Springboot 3 的强大框架特性与 Redis 7 集群的结合使得开发人员能够更加便捷地进行开发和维护。Springboot 提供了简洁的配置和开发模式而 Redis 7 集群则提供了高效的数据存储和访问方式两者相辅相成为开发高质量的应用程序奠定了坚实的基础。
二、准备工作
一Redis 集群安装请参照下文链接
CentOS 7 环境搭建 redis 最新版 7.4 分布式集群完整版详解
三、整合步骤
一依赖添加
在 Springboot 3 项目中要整合 Redis 7 集群首先需要在项目的pom.xml文件中添加必要的依赖。
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency二配置文件编写
接下来是编写 application.yml 文件下面配置了 Redis 7 集群的节点、超时时间以及连接池的相关参数。
server:port8080spring:application:name: redisdata:redis:password: 123456abccluster: timeout: 1000max-redirects: 3nodes: 192.168.117.128:7400,192.168.117.128:7401,192.168.117.129:7402,192.168.117.129:7403,192.168.117.131:7404,192.168.117.131:7405lettuce:pool:max-active: 8 # 连接池中的最大空闲连接max-wait: 500 # 连接池最大阻塞等待时间使用负值表示没有限制max-idle: 8 # 连接池最大连接数使用负值表示没有限制min-idle: 0 # 连接池中的最小空闲连接三缓存工具类
这个缓存工具类可以更灵活地进行缓存操作提供了设置和获取缓存的方法并支持设置过期时间。
package com.jsglxx.redis;import java.util.concurrent.TimeUnit;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;Component
public class CacheUtil {private final RedisTemplateString, Object redisTemplate;public CacheUtil(RedisTemplateString, Object redisTemplate) {this.redisTemplate redisTemplate;}public void set(String key, Object value, long timeout, TimeUnit unit) {redisTemplate.opsForValue().set(key, value, timeout, unit);}public Object get(String key) {return redisTemplate.opsForValue().get(key);}
}缓存工具类 CacheUtil 通过构造函数注入 RedisTemplate。set 方法用于设置缓存接收键、值、过期时间和时间单位作为参数。get 方法用于获取缓存根据键从 Redis 中获取对应的值。
四实体类
package com.jsglxx.redis.Entity;public class User {private Long id;private String name;public User() {super();// TODO Auto-generated constructor stub}public User(Long id, String name) {super();this.id id;this.name name;}public Long getId() {return id;}public void setId(Long id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}}
五Service 层
package com.jsglxx.redis.service;import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import com.jsglxx.redis.Entity.User;Service
public class UserService {private final RedisTemplateString, Object redisTemplate;public UserService(RedisTemplateString, Object redisTemplate) {this.redisTemplate redisTemplate;}public void saveUser(User user) {redisTemplate.opsForValue().set(user: user.getId(), user);}public Object getUser(Long id) {return (User) redisTemplate.opsForValue().get(user: id);}
}
这里通过构造函数注入 RedisTemplate然后使用它的opsForValue()方法进行对值类型的操作。在saveUser方法中将用户对象以键值对的形式存入 Redis键为user: user.getId()值为用户对象。在getUser方法中根据用户 ID 从 Redis 中获取对应的用户对象。
六Controller 层
package com.jsglxx.redis.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import com.jsglxx.redis.Entity.User;
import com.jsglxx.redis.service.UserService;RestController
public class UserController {private final UserService userService;public UserController(UserService userService) {this.userService userService;}PostMapping(/users)public void saveUser(RequestBody User user) {userService.saveUser(user);}GetMapping(/users/{id})public Object getUser(PathVariable Long id) {return userService.getUser(id);}
}UserController 提供了两个接口/users 用于保存用户/users/{id} 用于获取用户。UserController 接收 UserService 的实例通过 PostMapping 和 GetMapping 注解定义了两个接口。在 saveUser 方法中调用 userService 的 saveUser 方法保存用户。在 getUser 方法中调用 userService 的 getUser 方法获取用户。
(七配置类
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration
public class RedisConfig {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateString, Object template new RedisTemplate();template.setConnectionFactory(redisConnectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));return template;}
}四、测试保存用户接口POST 请求
选择 POST 请求方法。在 URL 输入栏中输入你的应用的地址加上 /usershttp://localhost:8080/users。在 Headers 选项卡中可以添加 Content-Type 为 application/json表示请求体是 JSON 格式的数据。在 Body 选项卡中选择 raw 和 JSON 格式。输入一个 JSON 对象来表示用户数据
{id:1,name:小明
}点击 Send 按钮发送请求。 检查响应状态码应该是 200 OK 表示请求成功并且你的应用应该已经将用户数据保存到 Redis 中。
五、测试获取用户接口GET 请求
选择 GET 请求方法。在 URL 输入栏中输入你的应用的地址加上 /users/{id}将 {id} 替换为你要查询的用户 ID例如 http://localhost:8080/users/1。 3. 点击 Send 按钮发送请求。
结束语
在本次探索中我们成功地将 Spring Boot 3.3 与 Redis 7.4 进行了整合实现了高效的缓存管理。通过利用 Redis 7.4 的强大功能和集群模式我们为应用程序带来了显著的性能提升和高可用性保障。 如果觉得本文能够帮到您请关注、点赞、收藏让这份美好延续下去 对技术管理感兴趣 请关注下方 ⬇ 【 技术管理修行】 过来人的建议学习技术的同时学一点管理小知识您不升职谁升职~~