当前位置: 首页 > news >正文

SpringBoot整合redis集群和redis单节点

// 连接redis单节点配置类@Configuration
public class RedisConfig {@Value("${spring.redis.host}")private String host;@Value("${spring.redis.port}")private Integer port;@Value("${spring.redis.password}")private String password;/*** @description: 非集群的Redis连接工厂* @return: org.springframework.data.redis.connection.RedisConnectionFactory* @author: hezha* @time: 2023/10/24 15:31*/@Beanpublic RedisConnectionFactory redisSingleNodeConnectionFactory() {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();config.setHostName(host);config.setPort(port);config.setPassword(password);config.setDatabase(0);return new JedisConnectionFactory(config);}/*** @description: 连接非集群的Redis的RedisTemplate* @return: org.springframework.data.redis.core.RedisTemplate<java.lang.String,java.lang.Object>* @author: hezha* @time: 2023/10/24 15:31*/@Beanpublic RedisTemplate<String, Object> redisSingleNodeTemplate(@Qualifier("redisSingleNodeConnectionFactory") RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// 设置key的序列化方式RedisSerializer<String> stringSerializer = new StringRedisSerializer();template.setKeySerializer(stringSerializer);template.setHashKeySerializer(stringSerializer);// 设置value的序列化方式Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);template.setValueSerializer(jsonSerializer);template.setHashValueSerializer(jsonSerializer);return template;}}

//连接redis集群的配置类@Configuration
public class RedisClusterConfig {@Value("${spring.redis.cluster.nodes}")private String clusterNodes;@Value("${spring.redis.password}")private String redisAuth;@Bean@Primarypublic RedisConnectionFactory redisClusterConnectionFactory() {RedisClusterConfiguration redisClusterConfig = new RedisClusterConfiguration();String[] split = clusterNodes.split(",");for(int i = 0; i< split.length; i++){String[] strings = split[i].split(":");String host = strings[0];Integer post = Integer.parseInt(strings[1]);redisClusterConfig.clusterNode(host, post);}JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfig);// 配置其他 JedisConnectionFactory 属性,如密码、超时等jedisConnectionFactory.setPassword(redisAuth);return jedisConnectionFactory;}@Beanpublic RedisTemplate<String, Object> redisClusterTemplate(RedisConnectionFactory redisClusterConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisClusterConnectionFactory);// 设置key的序列化方式RedisSerializer<String> stringSerializer = new StringRedisSerializer();redisTemplate.setKeySerializer(stringSerializer);redisTemplate.setHashKeySerializer(stringSerializer);// 设置value的序列化方式Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);redisTemplate.setValueSerializer(jsonSerializer);redisTemplate.setHashValueSerializer(jsonSerializer);return redisTemplate;}
}

pom.xml

 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Redis集群 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.6.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.54</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

使用如下:

//直接注入即可使用@Autowiredprivate  RedisTemplate<String, Object> redisSingleNodeTemplate;@Autowiredprivate RedisTemplate<String, Object> redisClusterTemplate;

http://www.lryc.cn/news/205692.html

相关文章:

  • 【ARM Coresight 系列文章19.1 -- Cortex-A720 PMU 详细介绍】
  • FoneDog iOS Unlocker(ios解锁工具) 适用macos电脑
  • 雅可比矩阵和雅可比坐标
  • macOS鼠标管理操作增强BetterMouse简体中文
  • 塔式服务器介绍
  • 上海市道路数据,有63550条数据(shp格式和xlsx格式)
  • 计算机网络_03_tcp/ip四层模型
  • [SQL开发笔记]IN操作符: 在WHERE子句中规定多个值
  • Java基础(第一期):IDEA的下载和安装(步骤图) 项目结构的介绍 项目、模块、类的创建。第一个代码的实现
  • 0基础学习PyFlink——使用PyFlink的SQL进行字数统计
  • 【Java系列】ArrayList
  • sqlalchemy 使用
  • Python深度学习实战-基于class类搭建BP神经网络实现分类任务(附源码和实现效果)
  • GIS 数据结构整理:网格索引
  • 【打靶】vulhub打靶系列(一)—小白视野的渗透测试
  • kafka3.X集群安装(不使用zookeeper)
  • 2023 年 的 DBA 有哪些变化?
  • vs2022 使用git同步报错以及解决每次推送要输入密码问题
  • 有哪些适用于 Windows 的PDF 阅读器?免费 PDF 阅读器清单
  • 避雷!新增2本期刊被标记为「On Hold」,1区TOP刊仍在调查中!
  • iOS 配置通用链接(Universal Link)服务端和开发者后台都配置好了,还是跳转不到App
  • 【环境】Linux下Anaconda/ Miniconda安装+百度Paddle环境搭建+Cudnn(3090显卡+CUDA11.8+cudnn8.6.0)
  • 【Python机器学习】零基础掌握AdaBoostRegressor集成学习
  • 各种添加路由的方法
  • MySQL外键
  • 自制数据库迁移工具-C版-02-HappySunshineV1.1-(支持Gbase8a)
  • k8s创建pod-affinity亲和性时报错解决办法
  • 基于边缘智能网关的储能系统安全监测管理方案
  • 大数据Flink(一百零一):SQL 表值函数(Table Function)
  • 内核进程初始化和创建