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 主从架构 如何配置我上次已 ...
随机推荐
- .NET6: 开发基于WPF的摩登三维工业软件
MS Office和VisualStudio一直引领着桌面应用的时尚潮流,大型的工业软件一般都会紧跟潮流,搭配着Ribbon和DockPanel风格的界面.本文将介绍WPF下两个轻量级的Ribbon和 ...
- Spring学习二:Spring Bean 定义
Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean 是 ...
- Git起始操作之设置全局用户名和Email地址
引自:廖雪峰老师的Git教程 因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址.你也许会担心,如果有人故意冒充别人怎么办?这个不必担心,首先我们相信大家都是善良无 ...
- Python property动态属性
from datetime import datetime, date class User: def __init__(self, name, birthday): self.name = name ...
- 解决"Uncaught (in promise) Error: Navigation cancelled from "/" to "/login" with a new navigation"报错处理
Uncaught (in promise) Error: Navigation cancelled from "/" to "/login" with a ne ...
- 在Linux中设置php变量的方法
默认情况下已经安装好了PHP环境,并且知道安装好后的PHP文件路径,然后可以通过以下的方式设置PHP变量,快速执行PHP命令运行PHP文件. 环境:centos 第一步:vi ~/.bash_prof ...
- CentOS 7 升级 gcc-4.8.5 到 gcc-5.4.0
文章目录 1.环境介绍 2.下载gcc-5.4.0源码包 3.编译安装gcc 4.验证gcc版本 5.更新gcc连接 1.环境介绍 [root@localhost ~]# gcc -v Using b ...
- CentOS7搭建ntp时钟服务器
文章目录 服务器配置 远程客户端配置 服务器配置 # 关闭防火墙,selinux=disabled 1.# 服务器部署 [root@localhost ~]# yum -y install ntp n ...
- MXNet源码分析 | KVStore进程内通信
本文主要基于MXNet1.6.0版本进行分析. MXNet的KVStore模块下有几个比较重要的类.KVStore是一个抽象类,提供了一些通用的API,例如Init.Push和Pull等.因为KVSo ...
- 封装及其作用Java
封装 该露的露,该藏的藏 我们程序设计要求"高内聚,低耦合".高内聚就是类的内部数据操作细节自己完成,不允许外部干涉:低耦合:仅暴漏少量的方法给外部使用 封装(数据的隐藏): ...