最近准备弄一个springBoot电商秒杀的demo项目,在搭建后台框架时,非关系型数据库准备整合redis,之前用的是单机版,考虑到是电商项目,后面用jmeter做压力测试效果可能不好,于是想试试redisCluster集群模式,看看并发qps变化效果,在网上看了很多博客吸取到了很多干货,但是很多都不全只是写了代码这块怎么整合的例子,并没有指出redis部署集群的教程,所以摸索成功之后觉得有必要分享出来整套的东西,让整合的人少踩坑,废话不多说下面开始。
1.在windows上进行redis集群部署。
参考这位兄弟的帖子:https://blog.csdn.net/zsg88/article/details/73715947点击打开链接
帖子里面Ruby 环境安装的地址已经失效,我是在这里下载安装的
另外如果项目里配置的redis地址用电脑ip的话,需要去redis安装目录下的redis.windows.conf配置文件里更改一下默认绑 定的ip,改掉默认bind 127.0.0.1为bind 0.0.0.0,这样的话redis就可以供别的机器访问了。
部署完成最终效果图
2.springBoot架构整合
①springBoot架构创建(这里不做过多描述,推荐使用idea一键创建)。
②pom文件增加依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
③.配置application.yml或application.properties配置文件
spring: redis: cache.clusterNodes: 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384 cache.commandTimeout: 5000
④.创建RedisProperties装载配置到对象
package com.springboot.buying.snapup.utils.redis; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Author: nanjunyu * @Description:读取redis配置信息并装载 * @Date: Create in 2018/6/14 16:16 */ @Component @ConfigurationProperties(prefix = "spring.redis.cache") public class RedisProperties { private int expireSeconds; private String clusterNodes; private int commandTimeout; public int getExpireSeconds() { return expireSeconds; } public void setExpireSeconds(int expireSeconds) { this.expireSeconds = expireSeconds; } public String getClusterNodes() { return clusterNodes; } public void setClusterNodes(String clusterNodes) { this.clusterNodes = clusterNodes; } public int getCommandTimeout() { return commandTimeout; } public void setCommandTimeout(int commandTimeout) { this.commandTimeout = commandTimeout; } }
⑤.创建JedisClusterConfig读取读取配置信息
package com.springboot.buying.snapup.utils.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import java.util.HashSet; import java.util.Set; /** * @Author: nanjunyu * @Description: * @Date: Create in 2018/6/14 16:28 */ @Configuration public class JedisClusterConfig { @Autowired private RedisProperties redisProperties; public JedisCluster getJedisCluster(){ String [] serverArray=redisProperties.getClusterNodes().split(","); Set<HostAndPort> nodes=new HashSet<>(); for (String ipPort:serverArray){ String [] ipPortPair=ipPort.split(":"); nodes.add(new HostAndPort(ipPortPair[0].trim(),Integer.valueOf(ipPortPair[1].trim()))); } return new JedisCluster(nodes,redisProperties.getCommandTimeout()); } }
⑥.创建RedisClientTemplate接口进行set get测试 这里简单写了两个方法,自行修改
package com.springboot.buying.snapup.utils.redis; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @Author: nanjunyu * @Description: * @Date: Create in 2018/6/14 16:34 */ @Service public class RedisClientTemplate { private static final Logger log=LoggerFactory.getLogger(RedisClientTemplate.class); @Autowired private JedisClusterConfig jedisClusterConfig; public boolean setToRedis(String key,Object value){ try { String str=jedisClusterConfig.getJedisCluster().set(key, String.valueOf(value)); if("OK".equals(str)) return true; }catch (Exception ex){ log.error("setToRedis:{Key:"+key+",value"+value+"}",ex); } return false; } public Object getRedis(String key){ String str=null; try { str=jedisClusterConfig.getJedisCluster().get(key); }catch (Exception ex){ log.error("getRedis:{Key:"+key+"}",ex); } return str; } }
ok到这里我们redis集群搭建好了,代码配置也都弄好了,现在我们创建一个controller见证奇迹吧!
package com.springboot.buying.snapup.controller; import com.springboot.buying.snapup.utils.redis.RedisClientTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author: nanjunyu * @Description: * @Date: Create in 2018/6/14 17:31 */ @RestController @RequestMapping(value = "/test") public class TestController { @Autowired RedisClientTemplate redisClientTemplate; @GetMapping(value = "/testSet") public Object testSet(){ redisClientTemplate.setToRedis("Frank","Frank测试redis"); System.out.println(redisClientTemplate.getRedis("Frank")); return null; } }
下面就是见证奇迹的时刻。。
到这里我们的springBoot整合redisCluster集群模式就完成了 ,谢谢大家观看,有问题留言。