SpringBoot连接Redis (Sentinel模式&Cluster模式)
一、引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、配置YML文件(二选一)
1.sentinel模式
server:
port: 80
spring:
redis:
sentinel:
nodes: 192.168.0.106:26379,192.168.0.106:26380,192.168.0.106:26381 //哨兵的ip和端口
master: mymaster //这个就是哨兵配置文件中 sentinel monitor mymaster 192.168.0.103 6379 2 配置的mymaster
2.Cluster模式
server:
port: 80
spring:
redis:
cluster:
nodes: 192.168.0.106:7000,192.168.0.106:7001,192.168.0.106:7002,192.168.0.106:7003,192.168.0.106:7004,192.168.0.106:7005
三、配置RedisTemplate模版
个人认为
setKeySerializer
setValueSerializer
不设置也可以,不过在使用的时候,需要自行将key\value 转换为json字符串后存入
@Configuration
public class RedisConf {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
Jackson2JsonRedisSerializer serializer=new Jackson2JsonRedisSerializer(Object.class);
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(serializer); //设置key序列化
template.setValueSerializer(serializer);//设置value序列化
return template; } }
四、测试(简单的model就省略了)
@RestController
public class RedisTestController {
@Autowired
RedisTemplate redisTemplate; @GetMapping("set")
public void set(){
redisTemplate.opsForValue().set("key1","123");
User u=new User();
u.setId(1);
u.setName("name姓名");
redisTemplate.opsForValue().set("user",u);
}
@GetMapping("get")
public Map get(){
Map map=new HashMap();
map.put("v1",redisTemplate.opsForValue().get("key1"));
map.put("v2",redisTemplate.opsForValue().get("user"));
return map;
}
}
SpringBoot连接Redis (Sentinel模式&Cluster模式)的更多相关文章
- [python]操作redis sentinel以及cluster
先了解清楚sentinel和cluster的差别,再学习使用python操作redis的API,感觉会更加清晰明白. 1.redis sentinel和cluster的区别 sentinel遵循主从结 ...
- springboot连接redis进行CRUD
springboot连接redis进行CRUD: 1.添加以下依赖: <dependency> <groupId>org.springframework.boot</gr ...
- springboot连接redis错误 io.lettuce.core.RedisCommandTimeoutException:
springboot连接redis报错 超时连接不上 可以从以下方面排查 1查看自己的配置文件信息,把超时时间不要设置0毫秒 设置5000毫秒 2redis服务长时间不连接就会休眠,也会连接不上 重 ...
- 不会用SpringBoot连接Redis,那就赶紧看这篇
摘要:如何通过springboot来集成操作Redis. 本文分享自华为云社区<SpringBoot连接Redis操作教程>,作者: 灰小猿. 今天来和大家分享一个如何通过springbo ...
- Jedis整合单机、Sentinel和Cluster模式
配置文件和配置类 @Data @Configuration @ConfigurationProperties("jedis-config") public class JedisC ...
- Redis集群-Cluster模式
我理解的此模式与哨兵模式根本区别: 哨兵模式采用主从复制模式,主和从数据都是一致的.全量数据: Cluster模式采用数据分片存储,对每个 key 计算 CRC16 值,然后对 16384 取模,可以 ...
- python连接redis sentinel集群
安装 python redis 客户端 pip install redis #!/usr/bin/env python # -*- coding:utf-8 -*- #!/usr/bin/env py ...
- redis集群cluster模式搭建
实验服务器 :192.168.44.139 192.168.44.138 192.168.44.144 在 192.168.44.139上操作: 将redis的包上传的新建的目录newtouc ...
- java架构之路-(Redis专题)SpringBoot连接Redis超简单
上次我们搭建了Redis的主从架构,哨兵架构以及我们的集群架构,但是我们一直还未投入到实战中去,这次我们用jedis和springboot两种方式来操作一下我们的redis 主从架构 如何配置我上次已 ...
随机推荐
- 写程序时try,catch查看报错的行号
try { //////////////// 代码段 //////////////// }catch(Exception ex) { MessageBox.Show(ex.St ...
- 羽夏看Win系统内核——同步篇
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...
- 【第十二期】腾讯后台实习初试、复试、HR面经 (许愿OC)
楼主投的很晚属于正常批才开始,初试面试官比较重基础,复试面试官比较看综合能力,HR小姐姐声音好听,腾讯面试官都特别nice! 一面: 看你项目很多,你挨个给我介绍一遍吧 我:一大堆按着简历介绍 日志文 ...
- 后缀自动机 (SAM)
后缀自动机 定义 定义 SAM 为一个有限状态自动机,接受且仅接受 \(S\) 的一个后缀. 同时,SAM 是这样的自动机中最小的那个,其中状态数至多为 \(2n - 1\),转移数至多为 \(3n ...
- sharding-jdbc5.0.0分表实践
本文基于shardingsphere-jdbc-core-spring-boot-starter 5.0.0,请注意不同版本的sharding-jdbc配置可能有不一样的地方,本文不一定适用于其它版本 ...
- js将HTML中table导出到EXCEL word (只支持IE) 另用php 配合AJAX可以支持所有浏览器
转载请注明来源:https://www.cnblogs.com/hookjc/ <HTML> <HEAD> <title>WEB页面导出为EXC ...
- linux计划任务之cron
目录 cron计划任务之用户级 cron计划任务之系统级 cron计划任务之用户级 1.安装crond centos7 执行命令: # yum install -y crontabs /bin/sys ...
- 解决Wordpress提示FTP登录问题
向wordpress目录的wordpress-config.php中添加 define("FS_METHOD", "direct"); define(" ...
- iOS时间处理之时间对比 by Nicky.Tsui
通过项目需求, 服务器返回了一个 order_canceled_time 订单自动取消时间 如果我要跟当前时间做一个对比,然后生成出一个倒计时的时间 那么首先我们要知道 order_cancel ...
- Redis——(主从复制、哨兵模式、集群)的部署及搭建
Redis--(主从复制.哨兵模式.集群)的部署及搭建 重点: 主从复制:主从复制是高可用redis的基础,主从复制主要实现了数据的多机备份,以及对于读操作的负载均衡和简单的故障恢复. 哨兵和集群都是 ...