redis集群搭建

1、redis目录下创建目录6380~6385,每个目录下存放一个redis.conf配置,端口号唯一:

daemonize yes
pidfile redis_6380.pid
logfile redis_6380.log
appendonly yes
bind 192.168.20.200
port 6380
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

各目录下修改6380至唯一

2、启动各节点:src/redis-server 6380/redis.conf  ...

3、安装:brew install redis-3.2.2.gem

yum install ruby rubygems -y

cd /opt/

gem install redis-3.2.2.gem

4、配置集群(3主3从):

src/redis-trib.rb create --replicas 1 192.168.20.200:6380 192.168.20.200:6381 192.168.20.200:6382 192.168.20.200:6383 192.168.20.200:6384 192.168.20.200:6385

5、

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>

6、


import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.*;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

@Slf4j
public class JedisDemo {

    private static JedisCluster jedisCluster;

    static {
        jedisCluster = jedisCluster();
    }

    private static JedisCluster jedisCluster() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(5000);
        jedisPoolConfig.setMaxWaitMillis(5000);

        Set<HostAndPort> hostAndPort = new HashSet<>();
        hostAndPort.add(new HostAndPort("192.168.20.200", 6380));
        hostAndPort.add(new HostAndPort("192.168.20.200", 6381));
        hostAndPort.add(new HostAndPort("192.168.20.200", 6382));
        hostAndPort.add(new HostAndPort("192.168.20.200", 6383));
        hostAndPort.add(new HostAndPort("192.168.20.200", 6384));
        hostAndPort.add(new HostAndPort("192.168.20.200", 6385));

        return new JedisCluster(hostAndPort, jedisPoolConfig);
    }

    public static void main(String[] args) {

        jedisCluster.set("hi1", "hello1");
        System.out.println(jedisCluster.get("hi1"));

        jedisCluster.set("hi2", "hello2");
        System.out.println(jedisCluster.get("hi2"));

        jedisCluster.set("hi3", "hello3");
        System.out.println(jedisCluster.get("hi3"));

        jedisCluster.set("hi4", "hello4");
        System.out.println(jedisCluster.get("hi4"));

        System.out.println(keys("*"));


        try {
            jedisCluster.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static TreeSet<String> keys(String pattern) {
        log.debug("Start getting keys...");
        TreeSet<String> keys = new TreeSet<>();
        Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
        for (Map.Entry<String, JedisPool> k : clusterNodes.entrySet()) {
            log.debug("Getting keys from: {}", k);
            JedisPool jp = k.getValue();
            Jedis connection = jp.getResource();
            try {
                keys.addAll(connection.keys(pattern));
            } catch (Exception e) {
                log.error("Getting keys error: {}", e);
            } finally {
                log.debug("Connection closed.");
                connection.close();
            }
        }
        log.debug("Keys gotten!");
        return keys;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值