第四章 springboot+jedisCluster

redis3.2.5集群搭建:第十二章 redis-cluster搭建(redis-3.2.5)

一、项目结构如下:

二、在上一章的基础上加入相关依赖

 1 <dependency>
 2             <groupId>redis.clients</groupId>
 3             <artifactId>jedis</artifactId>
 4         </dependency>
 5         <dependency>
 6             <groupId>com.alibaba</groupId>
 7             <artifactId>fastjson</artifactId>
 8             <version>1.1.15</version>
 9         </dependency>
10         <dependency>
11             <groupId>org.apache.commons</groupId>
12             <artifactId>commons-lang3</artifactId>
13             <version>3.3.2</version>
14         </dependency>
View Code
  • 说明:引入commons-lang3这个包是为了使用org.apache.commons.lang3.StringUtils这个类的isNotBlank()方法

三、基于上一章属性配置application.properties,加入集群属性配置

1 redis.cache.clusterNodes=10.211.55.6:7000,10.211.55.6:7001,10.211.55.6:7002,10.211.55.6:7003,10.211.55.6:7004,10.211.55.6:7005
2 redis.cache.commandTimeout=5
3 redis.cache.expireSeconds=120
  • clusterNodes是集群的六个ip+port
  • commandTimeout是什么❓
  • expireSeconds是缓存过期时间

四、JedisClusterConfig.java

 1 package com.weibo2.config;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 import org.springframework.core.env.Environment;
10 
11 import redis.clients.jedis.HostAndPort;
12 import redis.clients.jedis.JedisCluster;
13 
14 @Configuration
15 public class JedisClusterConfig {
16     @Autowired
17     private Environment env;
18 
19     @Bean
20     public JedisCluster jedisCluster() {
21         String[] serverArray = env.getProperty("redis.cache.clusterNodes").split(",");// 获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题
22         Set<HostAndPort> nodes = new HashSet<>();// 防止端口重复使用,故用set集合
23         for (String ipPort : serverArray) {
24             String[] ipPortPair = ipPort.split(":");
25             nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
26         }
27         return new JedisCluster(nodes, Integer.valueOf(env.getProperty("redis.cache.commandTimeout")));
28     }
29 }
View Code
    • 这里仍然使用org.springframework.core.env.Environment类读取属性配置文件
    • 使用java中的注解:
      • @Configuration:用于类上,相当于把该类交由spring容器管理;
      • @Bean:
        • 用于方法上,使用@Bean注解可以让方法的返回值为单例
        • 该方法的返回值可以直接注入到其他类中去使用

五、增加常量配置文件WeiboContants.java

package com.weibo2;

public class WeiboContants {
    public static final String REDIS_WEIBO_MAP = "weibo:map";
    public static final String REDIS_WEIBO_PREFIX = "weibo:";
}
  • 项目中的常量都可以配置在这里。
  • 缓存前缀常量定义类
    • 根据业务特点定义redis的缓存前缀,有助于防止缓存重复导致的缓存覆盖问题
    • 缓存前缀使用":"做分隔符

六、基于上一章,在WeiboService中加入缓存相关代码

 1 package com.weibo2.service;
 2 
 3 import org.apache.commons.lang3.StringUtils;
 4 import org.slf4j.Logger;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.core.env.Environment;
 7 import org.springframework.stereotype.Service;
 8 
 9 import com.alibaba.fastjson.JSON;
10 import com.weibo2.WeiboContants;
11 import com.weibo2.dao.WeiboDao;
12 import com.weibo2.model.Weibo;
13 
14 import redis.clients.jedis.JedisCluster;
15 
16 @Service
17 public class WeiboService {
18     private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(WeiboService.class);
19     @Autowired
20     private WeiboDao weiboDao;
21     @Autowired
22     private Environment env;
23     @Autowired
24     private JedisCluster jedisCluster;
25 
26     public boolean addWeibo(Weibo weibo) {
27         return weiboDao.add(weibo);
28     }
29 
30     public Weibo getWeibo(Integer id) {
31         Weibo weibo = null;
32         String weiboStr = jedisCluster.get(WeiboContants.REDIS_WEIBO_PREFIX + id);
33         if (StringUtils.isNotBlank(weiboStr)) {
34             LOGGER.info("get from redis,id:'{}'", id);
35             JSON.parseObject(weiboStr, Weibo.class);
36         } else {
37             LOGGER.info("get from mysql,id:'{}'", id);
38             weibo = weiboDao.select(id);
39             if (weibo != null) {
40                 jedisCluster.setex(WeiboContants.REDIS_WEIBO_PREFIX + id,
41                         Integer.valueOf(env.getProperty("redis.cache.expireSeconds")), JSON.toJSONString(weibo));
42             }
43         }
44         return weibo;
45     }
46 
47     public boolean updateWeibo(Weibo weibo) {
48         return weiboDao.updateSelective(weibo);
49     }
50 
51     public boolean deleteWeibo(Integer id) {
52         return weiboDao.deleteById(id);
53     }
54 
55 }
View Code

 

 

 

posted @ 2016-12-19 15:13  王小霞  阅读(246)  评论(0编辑  收藏  举报