springboot 搭建一个 测试redis 集群连通性demo

背景:我需要用 springboot 建一个测试 redis 集群连通性的 demo

废话不多说直接上代码:

1.pom

xml 复制代码
</dependency>
			<!-- Spring Boot Starter Data Redis -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-data-redis</artifactId>
			</dependency>
			<!-- Jedis Client for cluster support -->
			<dependency>
				<groupId>redis.clients</groupId>
				<artifactId>jedis</artifactId>
			</dependency>

2.配置

yaml 复制代码
spring:
  application:
    name: demo
  redis:
    cluster:
      nodes:
        - 10.228.48.28:26379,10.228.48.19:26379,
        - 10.228.48.21:26379,10.228.48.28:26380,
        - 10.228.48.19:26380,10.228.48.21:26380
    password: 0666AAcuSl_VLC8e
    timeout: 10000
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0

3.建一个 config 类

java 复制代码
package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author wangjn
 * @Description
 * @createTime 2024-06-13 10:44:00
 */
@Service
public class RedisClusterService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void setValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

4.controller

java 复制代码
package com.example.demo;

import com.example.demo.config.RedisClusterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wangjn
 * @Description
 * @createTime 2024-06-13 10:49:00
 */
@RestController
public class RedisController {

    @Autowired
    private RedisClusterService redisClusterService;

    // 设置Redis键值对
    @PostMapping("/redis/set/{key}")
    public String setValue(@PathVariable("key") String key, @RequestBody String value) {
        redisClusterService.setValue(key, value);
        return "Value set successfully for key: " + key;
    }

    // 获取Redis中的值
    @GetMapping("/redis/get/{key}")
    public String getValue(@PathVariable("key") String key) {
        return redisClusterService.getValue(key);
    }
}
相关推荐
不懂的浪漫16 分钟前
mqtt-plus 架构解析(九):测试体系,为什么要同时有 MqttTestTemplate 和 EmbeddedBroker
spring boot·物联网·mqtt·架构
见山是山-见水是水27 分钟前
鸿蒙flutter第三方库适配 - 汇率换算器
redis·flutter·华为·harmonyos
014-code35 分钟前
Redis 删除缓存失败怎么办?重试、死信、补偿的工程化方案
数据库·redis·缓存
rannn_1111 小时前
【Redis|高级篇1】分布式缓存|持久化(RDB、AOF)、主从集群、哨兵、分片集群
java·redis·分布式·后端·缓存
PD我是你的真爱粉1 小时前
Redis 持久化、过期删除、淘汰策略与内存碎片全解析
java·redis·bootstrap
斌味代码1 小时前
Redis 分库分表实战:从垂直拆分到水平扩容完整记录
数据库·redis·bootstrap
rchmin1 小时前
阿里Tair分布式锁与Redis分布式锁的实现区别
数据库·redis·分布式
win x11 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
递归尽头是星辰14 小时前
Spring Boot 配置排除失效深度解析:时序与机制核心
spring boot·自动配置·bean 加载·exclude失效·组件扫描
程序员萌萌15 小时前
Redis的缓存机制和淘汰策略详解
数据库·redis·缓存机制·淘汰策略