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);
    }
}
相关推荐
ly76895 分钟前
生产环境 Spring Boot 应用内存泄漏排查实战
java·spring boot·spring·内存泄漏·threadlocal·gc日志·堆转储
行百里er2 小时前
Spring Insight 里上报 Span 为什么用 JDK HttpClient 而不是 RestTemplate
spring boot·spring cloud·监控
莫得感情 o2 小时前
Redis 10 · 集群:16384槽、扩缩容与请求路由
redis·缓存
Bs_MoneyMagnet2 小时前
基于springboot+vue的家居生活商城平台的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring
乐观的Terry2 小时前
Redis生产环境redis-conf配置教程
数据库·redis
微三云 - 廖会灵 (私域系统开发)2 小时前
Spring Boot实战:五级分销定价与自动分佣系统设计与实现
java·spring boot·后端
SeaTunnel3 小时前
Redis 数据迁移不用写脚本:SeaTunnel 支持 String、Hash、Set、ZSet 四种数据类型
数据库·redis·哈希算法·数据迁移·seatunnel·数据同步
莫得感情 o3 小时前
Redis 11 · 缓存问题:穿透、击穿与雪崩
redis·缓存
风中的小熊生气3 小时前
Spring Boot 面试知识(一):从启动流程到 AOP 与事务失效
spring boot·后端·面试
kiss strong3 小时前
redis服务器登录(内网环境,无法使用客户端)
数据库·redis·缓存