iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现
  • 249
分享到

Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现

2024-04-02 19:04:59 249人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录原理  搭建集群SpringBoot中使用RedisTemplate原理   Redis Cluster 一般由多个节点组成,节点数量至少

原理  

Redis Cluster 一般由多个节点组成,节点数量至少为 6 个才能保证组成完整高可用的集群,其中三个为主节点,三个为从节点。三个主节点会分配槽,处理客户端的命令请求,而从节点可用在主节点故障后,顶替主节点。 

如上图所示,该集群中包含6个Redis节点3个主服务器3个从服务器,分别为M1,M2,M3,S1,S2,S3。除了主从 Redis 节点之间进行数据复制外,所有 Redis 节点之间采用 Gossip 协议进行通信,交换维护节点元数据信息。一般来说,主Redis节点会处理Clients的读写操作,而从节点只处理读操作。

搭建集群

1.进入redis目录中,打开redis.conf文件,注释掉bind 127.0.0.1,bind自己服务器的IP。并将protected -mode yes改为no。daemonize 默认是no,需要改成yes,以守护进程运行。

 

2.分别创建6个conf文件对应不同的端口号,正常每个节点对应一台服务器,为了方便测试本次使用一台服务来搭建集群模式。

redis6379.conf

include redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6379.conf 

redis6380.conf

include redis.conf
pidfile /var/run/redis_6380.pid
port 6380
dbfilename dump6380.rdb
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6380.conf

redis6381.conf

include redis.conf
pidfile /var/run/redis_6381.pid
port 6381
dbfilename dump6381.rdb
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6381.conf 

redis6389.conf 

include redis.conf
pidfile /var/run/redis_6389.pid
port 6389
dbfilename dump6389.rdb
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6389.conf 

redis6390.conf

include redis.conf
pidfile /var/run/redis_6390.pid
port 6390
dbfilename dump6390.rdb
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6390.conf 

redis6391.conf

include redis.conf
pidfile /var/run/redis_6391.pid
port 6391
dbfilename dump6391.rdb
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6391.conf 

3.进入到redis安装目录启动redis服务(如之前有nodes、rdb、aof文件请先删除,非则可能会报错)。

 

 

4.进入src目录中执行如下命令搭建集群。

redis-cli --cluster create --cluster-replicas 1 IP:6379 IP:6380 IP:6381 IP:6389 IP:6390 IP:6391

5.进入其中任意一个节点,执行命令查看当前节点状态

redis-cli - h ip -p 端口info replication

springBoot中使用RedisTemplate

1.pom文件中加入依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.创建redis配置类.

package com.wzy.demo.config;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JSONRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
@Configuration
public class RedisConfig {
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    
    @Bean
    public RedisTemplate redisTemplateInit() {
        //设置序列化Key的实例化对象
        redisTemplate.seTKEySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2jsonRedisSerializer());
        return redisTemplate;
    }
 
}

3.创建RedisUtil工具

package com.wzy.demo.util;
 
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
 
@Component
public class RedisUtil {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    //=============================common============================
 
    
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }
 
    
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    @SuppressWarnings("unchecked")
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }
 
    //============================String=============================
 
    
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }
 
    
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
 
    }
 
    
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }
 
    
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }
 
    //================================Map=================================
 
    
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }
 
    
    public Map<Object, Object> hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }
 
    
    public boolean hmset(String key, Map<String, Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean hmset(String key, Map<String, Object> map, long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean hset(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean hset(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public void hdel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }
 
    
    public boolean hHasKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }
 
    
    public double hincr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, by);
    }
 
    
    public double hdecr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, -by);
    }
 
    //============================set=============================
 
    
    public Set<Object> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    
    public boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    
    public long sSetAndTime(String key, long time, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0) expire(key, time);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    
    public long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    
    public long setRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    //===============================list=================================
 
    
    public List<Object> lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    
    public long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
 
    
    public Object lGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    
    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean lSet(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) expire(key, time);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean lSet(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean lSet(String key, List<Object> value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) expire(key, time);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    
    public long lRemove(String key, long count, Object value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}

4.在controller中测试redis。

@RequestMapping("/testRedis")
    public String testRedis(Model model) throws Exception{
    redisUtil.set("myid","123456");
    Object object = redisUtil.get("myid");
    model.addAttribute("redisKey",String.valueOf(object));
    return "redis.html";
}

5.页面中显示redis的返回结果。

到此这篇关于Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现的文章就介绍到这了,更多相关SpringBoot使用RedisTemplate内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现

本文链接: https://www.lsjlt.com/news/147592.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现
    目录原理  搭建集群SpringBoot中使用RedisTemplate原理   Redis Cluster 一般由多个节点组成,节点数量至少...
    99+
    2024-04-02
  • SpringBoot中RabbitMQ集群的搭建详解
    目录1. 两种模式1.1 普通集群1.2 镜像集群1.3 节点类型2. 搭建普通集群2.1 预备知识2.2 开始搭建2.3 代码测试2.4 反向测试3. 搭建镜像集群3.1 网页配置...
    99+
    2024-04-02
  • 使用Lvs+Nginx集群搭建高并发架构的实现示例
    目录1. Lvs介绍2. Lvs 负载均衡模式2.1 NAT2.2 TUN2.3 DR模式3. Lvs DR模式配置3.1 Vip配置3.2 LVS集群管理工具安装3.3 地址解析协...
    99+
    2024-04-02
  • VirtualBox+Ubuntu16搭建Kubernetes集群的实现
    目录关于 Kubernetes基础环境准备安装 VirtualBox下载 Ubuntu 16 系统镜像虚拟机 x3虚拟机 IP配置主机名SSH 无密连接Kubernetes 集群搭建...
    99+
    2024-04-02
  • redis 哨兵集群搭建的实现
    目录前言为什么需要哨兵集群搭建前准备搭建步骤集群测试前言 在上一篇,我们了解了redis 复制集群的完整的搭建流程,本篇来分享一下如何搭建 redis 哨兵集群。 为什么需要哨兵集群...
    99+
    2022-11-13
    redis 哨兵集群搭建 redis 哨兵集群
  • docker搭建kafka集群的方法实现
    目录一、原生Docker命令二、镜像选择三、集群规划四、Zookeeper集群安装五、Kafka集群安装一、原生Docker命令 1. 删除所有dangling数据卷(即无用的Vol...
    99+
    2024-04-02
  • nginx搭建高可用集群的实现方法
    目录Keepalived+Nginx 高可用集群(主从模式)Keepalived+Nginx 高可用集群(主从模式) 集群架构图 1、准备两台装有Nginx虚拟机 2、都需安装Ke...
    99+
    2023-01-28
    nginx 高可用集群 nginx 高可用
  • Redis的Cluster集群搭建的实现步骤
    目录一、引言二、Redis的Cluster模式介绍1、Redis群集101 2、Redis群集TCP端口 3、Redis集群和Docker 4、Redis集群数据分片 5、Redis...
    99+
    2024-04-02
  • 如何使用Lvs+Nginx集群搭建高并发架构
    这篇文章主要介绍“如何使用Lvs+Nginx集群搭建高并发架构”,在日常操作中,相信很多人在如何使用Lvs+Nginx集群搭建高并发架构问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用Lvs+Nginx...
    99+
    2023-06-22
  • redis搭建哨兵集群的实现步骤
    目录redis安装部署redis集群架构配置redis主从测试主从搭建redis哨兵集群哨兵集群详解:哨兵集群原理哨兵集群redis安装部署 环境说明: redis使用的是6.2.6...
    99+
    2024-04-02
  • Docker-Compose搭建Spark集群的实现方法
    目录一、前言二、docker-compose.yml三、启动集群四、结合hdfs使用一、前言 在前文中,我们使用Docker-Compose完成了hdfs集群的构建。本文将继续使用D...
    99+
    2024-04-02
  • Docker-Compose搭建Redis集群的实现教程
    目录1. 集群配置2.编写Redis.conf3. 在每个redis-*文件夹下创建redis.conf文件,并写入如下内容:4.编写docker-compose.yml文件5.开启集群6.测试6.1 查看节点属性6.2...
    99+
    2023-03-24
    Docker-Compose搭建Redis集群
  • .Net Core中使用MongoDB搭建集群与项目实战
    目录安装MongoDBapt直接安装(方法1)apt仓库安装(方法2)方法1、2启动MongoDB通过二进制包安装(方法3)安装依赖deb安装MongoDBtgz安装MongoDB启...
    99+
    2024-04-02
  • docker-compose搭建etcd集群的实现(三节点)
    目录1. 环境说明2. 部署etcd1etcd2etcd33. 验证3.1 查看集群状态3.2 读写测试1. 环境说明 节点IPetcd110.10.239.31etcd210.10...
    99+
    2024-04-02
  • docker实现redis集群搭建的方法步骤
    目录一、创建redis docker基础镜像 二、制作redis节点镜像 三、运行redis集群  引用:摘要:接触docker以来,似乎养成了一种习惯,安装什么应用软件都...
    99+
    2024-04-02
  • 基于Docker搭建Redis主从集群的实现
    目录1、拉取 Redis 镜像2、创建 6 个 Redis 容器3、启动 Redis 容器4、组建 Redis 集群5、关于Redis集群搭建最近陆陆续续有不少园友加我好友咨询 re...
    99+
    2024-04-02
  • CentOS 7.0中怎么搭建与使用Redis集群
    CentOS 7.0中怎么搭建与使用Redis集群,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。安装 Redis下载,解压,编译:$ cd ...
    99+
    2023-06-16
  • Redis5之后版本的高可用集群搭建的实现
    一、安装redis 1、安装gcc yum install gcc 2、下载redis-5.0.8.tar.gz 3、把下载好的redis-5.0.8.tar.gz放在/gyu...
    99+
    2024-04-02
  • kubernetes实践中的Etcd3集群搭建是怎样的
    这篇文章给大家介绍kubernetes实践中的Etcd3集群搭建是怎样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一:Etcd简介ETCD是用于共享配置和服务发现的分布式,一致性的KV存储系统。类似于Zookeep...
    99+
    2023-06-04
  • 在spring时使用RedisTemplate如何实现连接并操作Redis
    在spring时使用RedisTemplate如何实现连接并操作Redis?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。事务需要开启enableTransact...
    99+
    2023-05-31
    spring redistemplate redis
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作