一、Windows下搭建Redis Cluster
下载Redis-Windows版本
https://github.com/microsoftarchive/redis/releases
下载后解压,目录如下:

1、集群节点目录:
拷贝开始下载的redis解压后的目录,并修改文件名(比如按集群下redis端口命名)如下:

在节点目录下新建文件,输入(举例在6380文件夹下新建文件)

title redis-6380;
redis-server.exe redis.windows.conf

保存为start.bat 下次启动时直接执行该脚本即可;
分别打开各个文件下的 redis.windows.conf,分别修改如下配置(举例修改6380文件下的redis.window.conf文件):

port 6380 //修改为与当前文件夹名字一样的端口号
appendonly yes //指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。 yes表示:存储方式,aof,将写操作记录保存到日志中
cluster-enabled yes //开启集群模式
cluster-config-file nodes-6380.conf  //保存节点配置,自动创建,自动更新(建议命名时加上端口号)
cluster-node-timeout 15000 //集群超时时间,节点超过这个时间没反应就断定是宕机

注意:在修改配置文件这几项配置时,配置项前面不能有空格,否则启动时会报错(参考下面)

其他文件节点 6381~6385也修改相应的节点配置信息和建立启动脚本

2、下载Ruby并安装
下载地址:https://rubyinstaller.org/downloads/
然后对ruby进行配置:

3、构建集群脚本redis-trib.rb

可以打开 https://raw.githubusercontent.com/antirez/redis/unstable/src/redis-trib.rb 然后复制里面的内容到本地并保存为redis-trib.rb; redis-trib.rb

与redis集群节点保存在同一个文件夹下,目录如下:

依次启动所有集群节点start.bat
然后cmd进入redis集群节点目录后,执行: (–replicas 1 表示为集群中的每个主节点创建一个从节点)
redis-trib.rb create --replicas 1 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 127.0.0.1:6385

可能会遇见这个错误:
[ERR] Node is not empty. Either the node already knows other nodes (check with C

解决方法:
1)、将需要新增的节点下aof、rdb等本地备份文件删除;
2)、同时将新Node的集群配置文件删除,即:删除你redis.conf里面cluster-config-file所在的文件;
3)、再次添加新节点如果还是报错,则登录新Node,./redis-cli–h x –p对数据库进行清除:
172.168.63.201:7001> flushdb #清空当前数据库
参考:https://blog.csdn.net/vtopqx/article/details/50235737?spm=1001.2101.3001.6650.6&depth_1-

输出如下:

上图可看出 主节点为6380,6381,6382 端口的三个地址,6384,6385,6383为三个从节点

中途会询问是否打印更多详细信息,输入yes即可,然后redis-trib 就会将这份配置应用到集群当中,让各个节点开始互相通讯

4.测试集群
进入任意一个集群节点,cmd执行 redis-cli.exe -c -p 6381
写入任意一个value,查询

集群采用CRC16算法取模得到所属slot,然后将该key分到哈希槽区间的节点上CRC16(key) % 16384


二、SpringBoot架构整合

1、添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version>
</dependency>

2、配置application.yml文件

spring:redis:cache.clusterNodes: redis01.cloud.local:6379,redis02.cloud.local:6379,redis03.cloud.local:6379cache.commandTimeout: 5000password:database: 0lettuce:pool:max-active: 8max-wait: -1max-idle: 8min-idle: 0

添加redis配置文件目录如下:

创建RedisProperties装载配置到对象

package com.example.datafile.common.utils;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@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.example.datafile.common.utils;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;@Configuration
public class JedisClusterConfig {@Autowiredprivate 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接口进行get 、set测试

package com.example.datafile.common.utils;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class RedisClientTemplate {private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class);@Autowiredprivate 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;}
}

写测试类测试:

package com.example.datafile;import com.example.datafile.common.utils.RedisClientTemplate;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {@Autowiredprivate RedisClientTemplate redisClientTemplate;@Testpublic void set(){redisClientTemplate.setToRedis("Frank","Frank测试redis");System.out.println(redisClientTemplate.getRedis("Frank"));String traceId = TraceContext.traceId();System.out.println(traceId);}
}

打印:

SpringBoot整合Redis集群相关推荐

  1. SpringBoot整合Redis集群版本问题

    QUESTION:SpringBoot整合Redis集群版本问题? ANSWER: 版本依赖: <dependencies><!-- https://mvnrepository.co ...

  2. java集群解析文件_java相关:springboot整合redis集群过程解析

    java相关:springboot整合redis集群过程解析 发布于 2020-4-13| 复制链接 摘记: 简介 在springboot使用搭建好的redis集群添加redis和连接池依赖 ```x ...

  3. springboot篇】二十二. springboot整合Redis集群

    springboot整合Redis集群 **中国加油,武汉加油!** 案例准备 1. 新建Springboot项目springboot-redis-sentinel 2. 编写yml 3. Test ...

  4. springboot整合redis集群master宕机后连接超时

    前提: #        本文是在确保redis集群配置正确的情况下,连接超时的解决方案. 项目登录认证使用的是sa-token(这个不重要,主要说的是springboot和redis集群),最近应甲 ...

  5. 超完整!Springboot整合redis集群(Sentine),spring boot自动配置集成redis集群(Sentine)

    1.添加maven依赖 注意maven版本依赖,版本要相互匹配,如不知道如何查看相对应的版本时,可进入博主主页查看博主上一篇博文. <parent><groupId>org.s ...

  6. springBoot整合redis集群配置

    最近发现这篇博客阅读量比较大,今天特意抽空创建了一个可运行的开源项目. 项目的代码和之前的博客内容相比,做了些优化,请大家参考项目源码. 开源项目源码: springboot-redis-cluste ...

  7. Springboot项目整合redis集群

    文章目录 1.redis集群搭建 详情见: https://blog.csdn.net/qq_45076180/article/details/103026150 2.springboot整合redi ...

  8. 搭建第一个SpringBoot工程;SpringBoot整合mybatis;SpringBoot整合Redis-cluster集群;SpringBoot整合EhCache;

    写在前头,以下内容主要是为了自己复习之用,如果你有幸看到这篇文章,请不要嫌弃某些地方有所跳跃或省略. 1. 搭建第一个SpringBoot工程 1.1 创建工程 1.2 初始pom.xml文件内容 1 ...

  9. 七、SpringBoot整合elasticsearch集群

    @Author : By Runsen @Date : 2020/6/12 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘 ...

最新文章

  1. 2016年SDN通往成功路的5大步
  2. Tensorflow分批量读取tfrecords
  3. 违反了primarykey约束怎么解决_前期物业服务合同对主业有约束力吗?
  4. php 插入数据 不成功,thinkphp5连接oracle用insert插入数据失败
  5. php的在线问卷调查_基于php技术的问卷调查系统
  6. 设置bootstrap modal模态框的宽度和宽度
  7. oform java_客户端表单通用验证checkForm(oForm)(1)
  8. SQL Server时间粒度系列----第4节季、年时间粒度详解
  9. 华为鸿蒙系统英语报纸_“鸿蒙”系统的英文名叫这个!华为注册的这些《山海经》神兽都该怎么翻译?...
  10. sublime test3 php语法错误高亮
  11. c语言汉字属于什么类型_【C语言】必学知识点 - 基本数据类型!你学会了吗?...
  12. 月薪30K的硬件工程师需要哪些技能
  13. 进程隐藏的各种方法 以及分析比较以及实现链接
  14. 干货!!不同程序员岗位对不同电脑性能的要求(编程开发选电脑)
  15. 甄别客户需求,提高解决问题的效率
  16. 大数据分析和大数据开发哪个好就业啊?
  17. rosdep update 错误
  18. Wireless Power Tranmissions
  19. 移动端跨平台开发方案解析
  20. 05-使用Redis缓存数据,管理员相关数据表

热门文章

  1. Linux 查看进程状态
  2. 【Android 12.0】Android S WiFi启动业务流程分析(UML图)
  3. 大数据入门(Hadoop生态系统)
  4. 基于java失物招领系统
  5. experience(1):一个博士的经历(小木虫精华帖)
  6. ZOJ 3380 Patchouli's Spell Cards [基础概率DP+大数]
  7. php采集所有a标签,dedecms采集去除a标签代码
  8. 计算机芯片制造原理,从沙粒到芯片,原来CPU制造工序过程是这样的(视频)
  9. python用类写一个简单的猫狗大战游戏
  10. 利用Python进行数据分析之金融数据的统计分析