SpringBoot整合Redis单机和整合Redis集群的完整例子

Redis的客户端有很多,如Jedis,Jredis,Spring Data Redis….,所以SpringBOOT用了Spring Data Redis为了Redis提供了一种自动化的配置,我们只要添加相关依赖和相关配置就行。这里将说明如何整合redis单机和整合redis集群。

一 SpringBoot整合Redis单机

1.添加相关依赖

  1. org.springframework.boot
  2. spring-boot-starter-data-redis
  3. org.springframework.boot
  4. spring-boot-starter-web

默认的时候spring-boot-starter-data-redis使用的Redis工具是Lettuce,但是你要改成Jedis,只要去除Lettuce引用Jedis,其他不变。

  1. org.springframework.boot
  2. spring-boot-starter-data-redis
  3. io.lettuce
  4. lettuce-core
  5. redis.clients
  6. jedis

2.配置Redis
接下来我们只要在配置文件application.properties中配置Redis连接信息就行。如:

  1. spring.redis.database=0 #redis有16库
  2. spring.redis.host=l92.168.174.10 #redis的IP
  3. spring.redis.port=6379 #redis的端口
  4. spring.edis.password=l23@123 #redis的密码
  5. spring.redis.jedis.pool.max-active=8 #redis的连接池最大连接数
  6. spring.redis.jedis.pool.max-idle=8 #redis的连接池中最大连接空闲数
  7. spring.redis.jedis.pool.max-wait=-1ms #redis的连接池中最大等待时间,-1,表示没有限制
  8. spring.redis.jedis.pool.min-idle=0 #redis的连接池中最小连接空闲数
  9. 我们只要配置这2个,其他的springboot就能帮我们自动配置了,我们就能直接用了。如springboot的自动配置源码:
  10. @Configuration
  11. @ConditionalOnClass({RedisOperations.class})
  12. @EnableConfigurationProperties({RedisProperties.class})
  13. @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
  14. public class RedisAutoConfiguration {
  15. public RedisAutoConfiguration() {
  16. }
  17. @Bean
  18. @ConditionalOnMissingBean(
  19. name = {"redisTemplate"}
  20. )
  21. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  22. RedisTemplate<Object, Object> template = new RedisTemplate();
  23. template.setConnectionFactory(redisConnectionFactory);
  24. return template;
  25. }
  26. @Bean
  27. @ConditionalOnMissingBean
  28. public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  29. StringRedisTemplate template = new StringRedisTemplate();
  30. template.setConnectionFactory(redisConnectionFactory);
  31. return template;
  32. }
  33. }

我们可以从这个注解@EnableConfigurationProperties({RedisProperties.class})看出springboot把我们的application.properties配置信息注入到RedisProperties中,从下面的2个Java配置方法可以看出,如果我们没有提供redisTemplate,stringRedisTemplate的实例,SpringBoot就会帮我们创建者2个实例。而这2个正是提供操作Redis的基本方法。

redisTemplate,stringRedisTemplate操作Redis

  1. public class Book implements Serializable {
  2. private Integer id;
  3. private String name;
  4. private String author;
  5. //省略getter/setter
  6. }
  7. @Rest Controller public class BookController {
  8. @Autowired
  9. RedisTemplate redisTemplate;
  10. @Autowired StringRedisTemplate stringRedisTemplate;
  11. @GetMapping(”/testl”)
  12. public void testl(){
  13. //使用stringRedisTemplate
  14. ValueOperations<String,String> opsl=stringRedisTemplate.opsForValue();
  15. opsl.set(”name”,”三国演义”);//向Redis中存储一条记录
  16. String name=opsl.get(”name”); //将之读取出来
  17. System.out.println(name);
  18. //使用redisTemplate
  19. ValueOperationsops2=redisTemplate.opsForValue();
  20. Bookbl=newBook();
  21. bl.setid(l);
  22. bl.setName(”红楼梦”);
  23. bl.setAuthor(”曹雪芹”);
  24. ops2.set(”bl”,bl);//向Redis中存储一个对象
  25. Bookbook=(Book)ops2.get(”bl”); //将之读取出来
  26. System.out.println(book);
  27. }
  28. }

StringRedisTemplate和RedisTemplate的区别

StringRedisTemplate是RedisTemplate的子类,StringRedisTemplate中的问和value都是字符串,采用的序列化方案是StringRedisSerializer,而RedisTemplate则可以用来操作对象,RedisTemplate采用的序列化方案是JdkSerializationRedisSerializer。无论是StringRedisTemplate还是RedisTemplate,操作Redis的方法都是一致的.StringRedisTemplate和RedisTemplate都是通过opsForValue、opsForZSet或者opsForSet等方法首先获取一个操作对象,再使用该操作对象完成数据的读写。

二 SpringBoot整合Redis集群
集群和单机的差不多,但是默认的自动配置是单机的,就是配置类要我们自己配置,创建StringRedisTemplate和RedisTemplate,其他操作方法是一样的。

修改配置文件
由于有多个节点,我们最好用application.yml的配置文件。

  1. spring:
  2. redis:
  3. cluster:
  4. ports:
  5. -8001
  6. -8002
  7. -8003
  8. -8004
  9. -8005
  10. -8006
  11. host: 192.168.174.10
  12. poolConfig:
  13. max-total: 8
  14. max-idle: 8
  15. max-wait-millis: -1
  16. min-idle: 0

我这里的集群是在一台机器的,所以Ip就一个,而端口是一个数组,配置这样,我们等下好获取。

配置Reids信息,创建StringRedisTemplate和RedisTemplate

  1. @Configuration
  2. @ConfigurationProperties"spring.redis.cluster") //
  3. public class RedisConfig {
  4. List<Integer> ports;
  5. String host;
  6. JedisPoolConfig poolConfig;
  7. @Bean
  8. RedisClusterConfiguration redisClusterConfguration(){
  9. RedisClusterConfiguration configuration=newRedisClusterConfiguration();
  10. List<RedisNode>nodes=newArrayList<>();
  11. for(Integerport:ports){
  12. nodes.add(newRedisNode(host,port));
  13. }
  14. configuration.setPassword(RedisPassword.of"123@123"));
  15. configuration.setClusterNodes(nodes);
  16. return configuration;
  17. }
  18. @Bean
  19. JedisConnectionFactory jedisConnectionFactory(){
  20. JedisConnectionFactory factory=newJedisConnectionFactory(redisClusterConfiguration(),poolConfig);
  21. return factory;
  22. }
  23. @Bean
  24. RedsTemplate redisTemplate(){
  25. RedisTemplate redisTemplatenew RedisTemplate();
  26. redisTemplate.setConnectionFactory(jedisConnectionFactory());
  27. redisTemplate.setKeySerializer(newStringRedisSerializer());
  28. redisTemplate.setValueSerializer(newJdkSerializationRedisSerializer());
  29. return redisTemplate;
  30. }
  31. @Bean StringRedisTemplate stringRedisTemplate(){
  32. StringRedisTemplate stringRedisTemplate=new StringRedisTemplate(jedisConnectionFactory());
  33. stringRedisTemplate.setKeySerializer(newStringRedisSerializer());
  34. stringRedisTemplate.setKeySerializer(newStringRedisSerializer());
  35. return stringRedisTemplate;
  36. //省setget方法
  37. }
  38. }

代码说明:

通过@CorrfigurationProperties注解声明直文件前缀,配直文中定义的ports数组、host以及连接池配直信息都将被注入ports、host、poolConfig三个属性中。
配直RedisClusterConfiguration实例,设直Redis登录密码以及Redis节点信息。
根据RedisClusterConfiguration实例以及连接池配直信息创建JedisJedisConnectionFactory.
根据JedisConnectionFactory创建RedisTemplate和StringRedisTemplate,同时配直key和value的序列化方式。
有了RedisTemplate和StringRedisTemplate,剩下的用法就和上面的一样了
在这引用Redis的安装和配置方法:Redis的下载和安装与集群的配置解决redis requiresRuby version >= 2.2.2的问题