springboot集成redis哨兵主从怎么实现
一、环境springboot2.3.12.RELEASEJDK1.8IntelliJIDEA开发工具Redis哨兵主从搭建二、POM文件pom文件其他忽略,只展示和redis有关系统的依赖org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-data-redisorg.apache.common
一、环境
spring boot 2.3.12.RELEASE
JDK 1.8
IntelliJ IDEA开发工具
Redis哨兵主从搭建
二、POM文件
pom文件其他忽略,只展示和redis有关系统的依赖
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2
三、application.yml配置
关于springboot的配置忽略
spring 约定大于配置,对于默认的就可以不用再配置文件中体现
spring: redis: # redis库 database: 1 # redis节点的密码 password: jwssw # 集群配置 sentinel: # 集群哨兵节点配置,多个节点之间用英文逗号分割 nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381 # 主节点名称 master: mymaster # 密码 password: jwssw
注意如果redis的sentinel配置文件增加了requirepass(访问秘钥),其sentinel节点下必须加上【password】,否则不需要添加。
四、reidsTemplate配置
该配置文件可以直接加载启动类中,因为启动类也是springboot的一种配置类
/**
* 方法描述: 初始化redis连接
*
* @param factory redis连接工厂
* @return {@link RedisTemplate}
*/
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
// 新建redisTemplate对象
RedisTemplate template = new RedisTemplate<>();
// 设置工厂
template.setConnectionFactory(factory);
// 键值类型
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
// 返回redisTemplate对象
return template;
} 五、单元测试(JUnit4)
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
// 注入redisTemplate对象
@Autowired
RedisTemplate redisTemplate;
@Test
public void setOrGetTest() {
// redis键值
String redisKey = "name";
// 向redis存放内容
redisTemplate.opsForValue().set(redisKey, "张三" + new Random().nextInt());
// 获取redis中的内容并打印
System.out.println(redisTemplate.opsForValue().get(redisKey));
}
}
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















