springboot2 连接redis5集群,连接redis,连接redis哨兵模式,Jedis,Lettuce

àì夳堔傛蜴生んèń 2023-07-20 12:30 68阅读 0赞

一、引入依赖

  1. <!--redis-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!-- 要用redis连接池 必须有pool依赖-->
  7. <dependency>
  8. <groupId>org.apache.commons</groupId>
  9. <artifactId>commons-pool2</artifactId>
  10. </dependency>
  11. <!-- jackson -->
  12. <!-- java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException -->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-json</artifactId>
  16. </dependency>

二、使用Lettuce连接redis集群

springboot模式使用lettuce

application.yml

  1. spring:
  2. redis:
  3. database: 0 # Redis数据库索引(默认为0)
  4. #host: 192.168.0.201 # Redis服务器地址
  5. #port: 7001 # Redis服务器连接端口
  6. password: redis508 # Redis服务器连接密码(默认为空)
  7. timeout: 5000ms # 连接超时时间(毫秒)默认是2000ms
  8. lettuce:
  9. pool:
  10. max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
  11. max-idle: 20 # 连接池中的最大空闲连接
  12. min-idle: 0 # 连接池中的最小空闲连接
  13. max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
  14. #sentinel:
  15. #master: mymaster #哨兵监听的master名称
  16. #nodes: 192.168.203.205:26479, 192.168.203.205:26480 #哨兵地址列表,多个以,分割
  17. cluster:
  18. max-redirects: 3
  19. nodes: 192.168.0.201:7001,192.168.0.201:7002,192.168.0.201:7003,192.168.0.201:7004,192.168.0.201:7005,192.168.0.201:7006

LettuceConfig.java

  1. package com.xin.redis.rediscluster.config;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. @Configuration
  11. @EnableCaching //支持缓存注解
  12. public class LettuceConfig extends CachingConfigurerSupport {
  13. @Bean
  14. public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
  15. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  16. redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
  17. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
  18. redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
  19. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
  20. redisTemplate.setConnectionFactory(redisConnectionFactory);
  21. return redisTemplate;
  22. }
  23. }

RedisClusterTest.java

  1. package com.xin.redis.rediscluster;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. //@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
  11. //@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
  12. class RedisClusterTest {
  13. @Autowired
  14. private RedisTemplate<String, Object> redisTemplate;
  15. @Test
  16. void test() {
  17. redisTemplate.opsForValue().set("spring","redis");
  18. System.out.println(redisTemplate.opsForValue().get("spring"));
  19. redisTemplate.opsForValue().set("foo","bar"); //这个键在集群中可能有节点跳转
  20. System.out.println(redisTemplate.opsForValue().get("foo"));
  21. }
  22. }

三、使用Jedis连接redis集群

application.yml

就是把lettuce改成redis

  1. spring:
  2. redis:
  3. database: 0 # Redis数据库索引(默认为0)
  4. #host: 192.168.0.201 # Redis服务器地址
  5. #port: 7001 # Redis服务器连接端口
  6. password: redis508 # Redis服务器连接密码(默认为空)
  7. timeout: 5000ms # 连接超时时间(毫秒)默认是2000ms
  8. jedis:
  9. pool:
  10. max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
  11. max-idle: 20 # 连接池中的最大空闲连接
  12. min-idle: 0 # 连接池中的最小空闲连接
  13. max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
  14. #sentinel:
  15. #master: mymaster #哨兵监听的master名称
  16. #nodes: 192.168.203.205:26479, 192.168.203.205:26480 #哨兵地址列表,多个以,分割
  17. cluster:
  18. max-redirects: 3
  19. nodes: 192.168.0.201:7001,192.168.0.201:7002,192.168.0.201:7003,192.168.0.201:7004,192.168.0.201:7005,192.168.0.201:7006

JedisConfig.java

就是把LettuceConnectionFactory改成RedisConnectionFactory

  1. package com.xin.redis.rediscluster.config;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. @Configuration
  11. @EnableCaching //支持缓存注解
  12. public class JedisConfig extends CachingConfigurerSupport {
  13. @Bean
  14. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  15. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  16. redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
  17. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
  18. redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
  19. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
  20. redisTemplate.setConnectionFactory(redisConnectionFactory);
  21. return redisTemplate;
  22. }
  23. }

RedisClusterTest.java

  1. package com.xin.redis.rediscluster;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. //@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
  11. //@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
  12. class RedisClusterTest {
  13. @Autowired
  14. private RedisTemplate<String, Object> redisTemplate;
  15. @Test
  16. void test() {
  17. redisTemplate.opsForValue().set("spring","redis");
  18. System.out.println(redisTemplate.opsForValue().get("spring"));
  19. redisTemplate.opsForValue().set("foo","bar"); //这个键在集群中可能有节点跳转
  20. System.out.println(redisTemplate.opsForValue().get("foo"));
  21. }
  22. }

四、默认连接池连接单台

这种基本不用了

application.yml

  1. spring:
  2. redis:
  3. database: 0 # Redis数据库索引(默认为0)
  4. host: 192.168.0.201 # Redis服务器地址
  5. port: 7001 # Redis服务器连接端口
  6. password: redis508 # Redis服务器连接密码(默认为空)
  7. timeout: 2000ms # 连接超时时间(毫秒)默认是2000ms
  8. pool:
  9. #Deprecated
  10. max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
  11. max-idle: 20 # 连接池中的最大空闲连接
  12. min-idle: 0 # 连接池中的最小空闲连接
  13. max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)

RedisConfig.java

  1. package com.xin.redis.rediscluster.config;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. @Configuration
  11. @EnableCaching
  12. public class RedisConfig extends CachingConfigurerSupport {
  13. @Bean
  14. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  15. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  16. redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
  17. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
  18. redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
  19. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
  20. redisTemplate.setConnectionFactory(redisConnectionFactory);
  21. return redisTemplate;
  22. }
  23. }

RedisTest.java

  1. package com.xin.redis.rediscluster;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. //@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
  11. //@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
  12. class RedisTest {
  13. @Autowired
  14. private RedisTemplate<String, Object> redisTemplate;
  15. @Test
  16. void test() {
  17. redisTemplate.opsForValue().set("spring","redis");
  18. System.out.println(redisTemplate.opsForValue().get("spring"));
  19. }
  20. }

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java连接redis哨兵模式

    redis 哨兵模式 怎么查看每个redis 集群的状态 您好,BRPOP命令接收两个参数,第一个是键名,第二个是超时时间,单位是秒。当超过了此时间仍然没有获得新元素的话