SpringBoot整合redis集群和redis单节点

编程入门 行业动态 更新时间:2024-10-09 03:21:33

SpringBoot整合redis集群和redis单<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点"/>

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.apachemons</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;

更多推荐

SpringBoot整合redis集群和redis单节点

本文发布于:2023-12-03 10:53:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1654557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   集群   SpringBoot   redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!