美文网首页
redis5集群搭建系列--完全分布式集群(3)

redis5集群搭建系列--完全分布式集群(3)

作者: lcjyzm | 来源:发表于2018-12-25 17:37 被阅读11次

7. 完全分布式(集群)安装及代码测试

7.1 实现
redis集群的节点个数是奇数个,最少有三个节点,为了保证集群的高可用性,对每台redis节点需要进行备份,因此redis集群需要6台服务器
7.2 安装
1. 下载redis5.0.2二进制安装包
    wget http://download.redis.io/releases/redis-5.0.2.tar.gz
    
2. 解压到/opt目录下
    tar -xzvf redis-5.0.2.tar.gz -C /opt
    
3. 编译
    cd /opt/redis-5.0.2  && make
    
4. 指定安装位置
    make install PREFIX=/usr/local/redis7000
    
5. 拷贝安装目录下配置文件到 /usr/local/redis7000/etc
    mkdir /usr/local/redis_cluster/redis7000/etc
    cp  /opt/redis-5.0.2/redis.conf  /usr/local/redis7000/etc
    
6. 修改配置文件 /usr/local/redis7000/etc/redis.conf
    vi /usr/local/redis7000/etc/redis.conf
    
    # 关闭保护模式
    protected-mode no
    
    # 以守护进程后台模式运行
    daemonize yes
    
    # 绑定本机ip
    bind 172.18.201.104
    
    # 修改端口
    port 7000
    
    # redis进程文件
    pidfile /usr/local/redis7000/redis_7000.pid
    
    # 日志文件
    logfile /usr/local/redis7000/log/redis_7000.log
    
    # 快照数据存放目录,一定是目录
    dir /usr/local/redis7000/data/
    
    # 启用集群
    cluster-enabled yes
7. 新建data和log目录
    mkdir -p data log
    
8. 安装redis7001
    8.1 拷贝安装目录
        cp -r /usr/local/redis7000 /usr/local/redis7001
        
    8.2 编辑配置文件
        vi  /usr/local/redis7001/etc/redis.conf 
        
        # 修改端口
        port 7001

        # redis进程文件
        pidfile /usr/local/redis7001/redis_7001.pid

        # 日志文件
        logfile /usr/local/redis7001/log/redis_7001.log

        # 快照数据存放目录,一定是目录
        dir /usr/local/redis7001/data/

9. 仿照步骤1-8分别在172.18.201.105,172.18.201.106上安装redis7000,redis7001节点
    
10. 启动每台node上的redis
    /usr/local/redis7000/bin/redis-server  /usr/local/redis7000/etc/redis.conf && /usr/local/redis7001/bin/redis-server  /usr/local/redis7001/etc/redis.conf
    
11. 查看是否启动成功
    ps aux | grep redis

    root      2127  0.1  0.0 156404  9828 ?        Ssl  Dec12  10:43 /usr/local/redis7001/bin/redis-server 172.18.201.104:7001 [cluster]
    root      2142  0.1  0.0 156916 10224 ?        Ssl  Dec12  10:57 /usr/local/redis7000/bin/redis-server 172.18.201.104:7000 [cluster]
    
12. 使用5.0新特性redis-cli命令创建集群
    /usr/local/redis7000/bin/redis-cli --cluster create 172.18.201.104:7000 172.18.201.104:7001 172.18.201.105:7000 172.18.201.105:7001 172.18.201.106:7000 172.18.201.106:7001 --cluster-replicas 1

13. 恭喜配置成功,提示如下
    [OK] All 16384 slots covered.
7.3 代码
7.3.1 Jedis cluster
pom依赖
<!--jedis 客户端-->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>

测试代码
public static void main(String[] args) {
    int port = 6379;
    // 创建set集合
    Set<HostAndPort> nodes = new HashSet<>();
    // 将每一个节点的ip地址和端口号添加到集合中
    nodes.add(new HostAndPort("172.18.201.104", 7000));
    nodes.add(new HostAndPort("172.18.201.104", 7001));
    nodes.add(new HostAndPort("172.18.201.105", 7000));
    nodes.add(new HostAndPort("172.18.201.105", 7001));
    nodes.add(new HostAndPort("172.18.201.106", 7000));
    nodes.add(new HostAndPort("172.18.201.106", 7001));
    // 创建jedisCluster对象,需要将创建的redis集合列表作为参数传递
    JedisCluster jedisCluster = new JedisCluster(nodes);
    // 通过集群对象操作redis
    jedisCluster.set("address", "北京市长安街");
    String value = jedisCluster.get("address");
    // 打印结果
    System.out.println(value);
    // 系统关闭前,关闭jedisCluster对象
    jedisCluster.close();
}
7.3.2 Lettuce cluster
pom依赖
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>

测试代码
public static void main(String[] args) {
    List<RedisURI> list = new ArrayList<>();
    list.add(RedisURI.create("redis://172.18.201.104:7000"));
    list.add(RedisURI.create("redis://172.18.201.104:7001"));
    list.add(RedisURI.create("redis://172.18.201.105:7000"));
    list.add(RedisURI.create("redis://172.18.201.105:7001"));
    list.add(RedisURI.create("redis://172.18.201.106:7000"));
    list.add(RedisURI.create("redis://172.18.201.106:7001"));
    RedisClusterClient client = RedisClusterClient.create(list);
    StatefulRedisClusterConnection<String, String> connect = client.connect();

    /**
     * 同步执行命令
     */
    RedisAdvancedClusterCommands<String, String> commands = connect.sync();
    commands.set("hello","hello world2");
    String str = commands.get("hello");
    System.out.println(str);

    /**
     * 异步执行命令
     */
    RedisAdvancedClusterAsyncCommands<String,String> asyncCommands = connect.async();
    RedisFuture<String> future = asyncCommands.get("hello");

    try {
        String str1 = future.get();
        System.out.println(str1);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    connect.close();
    client.shutdown();
}

相关文章

网友评论

      本文标题:redis5集群搭建系列--完全分布式集群(3)

      本文链接:https://www.haomeiwen.com/subject/oltqlqtx.html