Redis Sentinel 是一个分布式系统,它为 Redis 服务器提供高可用性。其主要功能有:
优点:
缺点:
application.yml
配置:# 配置 Redis Sentinel 属性
spring:
redis:
sentinel:
master: mymaster # 主节点名称
nodes: 127.0.0.1:26379, 127.0.0.1:26380 # Sentinel 节点地址
username: sentineluser # Sentinel 用户名 (Redis 6及以上版本)
password: sentinelpassword # Sentinel 密码
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration
public class RedisConfig {
// 从 application.yml 获取 Sentinel 配置属性
@Value("${spring.redis.sentinel.master}")
private String master;
@Value("${spring.redis.sentinel.nodes}")
private String nodes;
@Value("${spring.redis.sentinel.username}")
private String username;
@Value("${spring.redis.sentinel.password}")
private String password;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
// 创建 Sentinel 配置对象
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master(master) // 设置主节点名称
.sentinel(nodes.split(",")[0].split(":")[0], Integer.parseInt(nodes.split(",")[0].split(":")[1])) // 添加第一个 Sentinel 节点
.sentinel(nodes.split(",")[1].split(":")[0], Integer.parseInt(nodes.split(",")[1].split(":")[1])); // 添加第二个 Sentinel 节点
sentinelConfig.setDatabase(0); // 设置数据库索引
sentinelConfig.setPassword(RedisPassword.of(password)); // 设置密码
return new LettuceConnectionFactory(sentinelConfig); // 创建连接工厂
}
@Bean
public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory); // 设置连接工厂
return template;
}
}
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
// 注入 StringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
// 注入 RedisTemplate
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 存储字符串数据到 Redis
*
* @param key 键
* @param value 值
*/
public void setStringData(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
/**
* 从 Redis 获取字符串数据
*
* @param key 键
* @return 值
*/
public String getStringData(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
/**
* 存储对象数据到 Redis
*
* @param key 键
* @param value 值
*/
public void setObjectData(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 从 Redis 获取对象数据
*
* @param key 键
* @return 值
*/
public Object getObjectData(String key) {
return redisTemplate.opsForValue().get(key);
}
}
package com.example.demo.controller;
import com.example.demo.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
// 注入 RedisService
@Autowired
private RedisService redisService;
/**
* 存储数据到 Redis
*
* @param key 键
* @param value 值
* @return 响应消息
*/
@GetMapping("/set")
public String set(@RequestParam String key, @RequestParam String value) {
redisService.setStringData(key, value);
return "Data set successfully";
}
/**
* 从 Redis 获取数据
*
* @param key 键
* @return 值
*/
@GetMapping("/get")
public String get(@RequestParam String key) {
return redisService.getStringData(key);
}
}