springBoot (springCloud2025)集成redisCluster 集群的操作方法
作者:Java 码农
文章介绍了如何使用Spring Boot集成Redis Cluster集群,并详细说明了pom.xml、application.yaml、集群配置类、其他配置类、连接池配置类、Redis工具类以及测试接口的配置和使用方法,感兴趣的朋友跟随小编一起看看吧
在Spring Boot项目中集成Redis Cluster,你可以使用Spring Data Redis库,它提供了对Redis操作的抽象和封装。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.emall</groupId>
<artifactId>account-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>account-service</name>
<description>account service</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2025.0.0</spring-cloud.version> <!-- 添加 Spring Cloud 版本 -->
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--- redis 以及数据绑定 开始-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Redis客户端:Lettuce(默认,支持集群) -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<!-- Spring Boot Configuration Processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--- redis 以及数据绑定 结束-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>application.yaml
spring:
application:
name: account-service
redis:
password: wa123456
cluster:
password:
${spring.redis.password}
nodes:
- 192.168.88.104:6379
- 192.168.88.104:6380
- 192.168.88.105:6379
- 192.168.88.105:6380
- 192.168.88.106:6379
- 192.168.88.106:6380
max-redirects: 3
lettuce:
pool:
max-active: 20
max-idle: 10
min-idle: 5
max-wait: 2000ms
cluster:
refresh:
adaptive: true
period: 200
timeout: 5000ms
connect-timeout: 3000ms
eureka:
instance:
hostname: localhost
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka
server:
port: 8760
logging:
level:
com.emall.account_service: DEBUGcluster配置类
package com.emall.account_service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisClusterProperties {
private List<String> nodes = new ArrayList<>();
private Integer maxRedirects = 3;
private String password;
}其他配置类
package com.emall.account_service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.time.Duration;
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedisCommonProperties {
private Duration timeout = Duration.ofMillis(2000);
private String password;
private Integer database = 0;
private Lettuce lettuce = new Lettuce();
@Data
public static class Lettuce {
private Pool pool = new Pool();
private Cluster cluster = new Cluster();
@Data
public static class Pool {
private int maxActive = 8;
private int maxIdle = 8;
private int minIdle = 0;
private Duration maxWait = Duration.ofMillis(-1);
}
@Data
public static class Cluster {
private Refresh refresh = new Refresh();
@Data
public static class Refresh {
private Boolean adaptive = true;
private Duration period = Duration.ofMillis(2000);
}
}
}
}连接池配置类
package com.emall.account_service.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.time.Duration;
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
@Data
public class LettucePoolProperties {
private int maxActive = 8;
private int maxIdle = 8;
private int minIdle = 0;
private Duration maxWait = Duration.ofMillis(-1);
private Duration timeBetweenEvictionRuns = Duration.ofMillis(30000);
}redis 工具类
package com.emall.account_service.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 设置缓存
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 设置缓存并设置过期时间
*/
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);
}
/**
* 删除缓存
*/
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
/**
* 设置过期时间
*/
public Boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
* 判断key是否存在
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 哈希操作 - 设置字段值
*/
public void hSet(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}
/**
* 哈希操作 - 获取字段值
*/
public Object hGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
/**
* 列表操作 - 左推
*/
public Long lPush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 列表操作 - 右弹出
*/
public Object rPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
}测试接口
package com.emall.account_service.controller;
import com.emall.account_service.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AccountFeignController {
@Autowired
private RedisUtil redisUtil;
@GetMapping("/account/address")
public String getAddress(){
return "account-service:8760";
}
@GetMapping("/account/add")
public String addRedis(){
String key = "user:account:";
String value= "刘e非";
redisUtil.set(key,value);
System.out.println("保存数据成果");
System.out.println("获取数据成果"+redisUtil.get(key));
return "success";
}
}到此这篇关于springBoot (springCloud2025)集成redisCluster 集群的文章就介绍到这了,更多相关springBoot 集成redisCluster 集群内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
