windows 下搭建redis cluster集群及配置springboot2.3.x

原创
2020/09/22 10:32
阅读数 30

1.软件环境

Redis-x64-3.2.100.zip

Redis-trib.rb

rubyinstaller-2.3.3-x64.exe

2.解压redis

把下载的redis解压到D盘redis-cluster目录,然后在复制出来五份,端口分别是

6379,6380,6381,6382,6383,6384

3.修改每个redis文件夹下的redis.windows.conf配置文件

进入到每个文件夹下,找到redis.windows.conf,然后改动这些参数

port 6379

cluster-enabled yes

cluster-config-file nodes-6379.conf

cluster-node-timeout 15000

appendonly yes

在每个文件夹下新建start.bat文件

title redis-6379

redis-server.exe redis.windows.conf

4.安装 Ruby

redis的集群使用  ruby脚本编写,所以系统需要有 Ruby 环境 

5.打开cmd窗口执行

gem install redis

6.启动每个redis,安装集群脚本

把 redis-trib.rb拷贝到redis-cluster文件夹

redis-trib.rb create --replicas 1 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

--replicas 1 表示每个主数据库拥有从数据库个数为1。master节点不能少于3个,所以我们用了6个redis

集群启动脚本.bat中的命令如下

start /d "D:\redis-cluster\Redis-6379" start.bat
start /d "D:\redis-cluster\Redis-6380" start.bat
start /d "D:\redis-cluster\Redis-6381" start.bat
start /d "D:\redis-cluster\Redis-6382" start.bat
start /d "D:\redis-cluster\Redis-6383" start.bat
start /d "D:\redis-cluster\Redis-6384" start.bat
ping /n 3 127.1>nul
ruby redis-trib.rb create --replicas 1 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

此操作要在第6步执行完毕,集群创建好之后执行,不然集群创建失败,给集群设置密码,密码需要一致不然会失败

masterauth redispassword
requirepass redispassword

7.和springboot2.3.x集成   POM.XML

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> 
    </parent>        
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

application.yml配置

spring:
  redis:
    cluster:
      nodes: 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

 8.RedisUtil.java

package com.example.elasticsearchdemo.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除对应的value (带事务,业务代码中用到事务,则需用此方法)
     *
     * @param keys
     */
    public void removeTransactional(final String... keys) {
        for (String key : keys) {
            removeTransactional(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }



    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        return operations.get(key);
    }

    /**
     * 哈希 添加
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<Object> lRange(String k, long l, long l1) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k, l, l1);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void add(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<Object> setMembers(String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     */
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key, value, scoure);
    }

    /**
     * 有序集合获取
     *
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }


    /**
     * 加锁
     *
     * @param key
     * @return
     */
    public boolean tryLock(String key) {
        try {
            long currTime = System.currentTimeMillis();
            //加锁成功
            return redisTemplate.opsForValue().setIfAbsent(key, currTime);
        } finally {
            redisTemplate.expire(key, 5, TimeUnit.SECONDS);
        }
    }

}

9.RedisController

package com.test;

import com.test.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RedisController {

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/redis")
    public String redis(String key){
        redisUtils.set(key,"sdfsdfsdf");
        return "OK-"+key;
    }

}

用工具连接每个redis,可以看到每个redis里面都有key和value了。redis cluster环境搭建成功

【设置redis最大可使用内存】

maxmemory 100mb

【达到最大内存的策略】

maxmemory-policy noeviction  拒绝写入

【可能出现的坑】

在执行set操作的时候 【error】CLUSTERDOWN Hash slot not served

没有分配槽,因为redis集群要分配16384个槽来储存数据,那么没有分配槽则报如上错误

什么原因呢?

原因是最后使用ruby来搭建集群的时候错误操作

redis-trib.rb create --replicas 1 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

上面执行完时会出现提示

Can I set the above configuration? (type 'yes' to accept): 

你需要输入yes,而并非缩写 y

就是这个错误引起的分配槽失败。

展开阅读全文
打赏
0
0 收藏
分享
加载中
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部