首先,说下Redis安装后的常用操作,安装后,主要有两种方式可以操作redis,一种是通过终端、一种是通过可视化图形界面,可视化图形界面的客户端工具,下载后,配置,直接操作即可,软件比较容易上手:

百度网盘地址:https://pan.baidu.com/s/1wdE8Xxku0ghM6xP77O0k9w

下面说下终端的使用:

redis一般都会开启,requirepass的设置,即密码设置,可以通过修改redis.conf配置文件

哨兵模式 增加redis 密码 redis哨兵设置密码_redis

通过redis-cli与redis建立连接

接下来尝试,set 一个信息到redis中,会提示NOAUTH

哨兵模式 增加redis 密码 redis哨兵设置密码_客户端_02

设置密码后,必须在客户端,输入密码,才能操作redis。通过auth + 字符串的形式即可验证成功,看到OK的结果提示

哨兵模式 增加redis 密码 redis哨兵设置密码_redis_03

客户端使用完后,可以通过exit的命令,退出客户端的connection。

redis的数据结构主要有5中,分别是STIRNG、LIST、SET、HASH、ZSET,每一种的应用场景均不一样,看下图,可以根据实际情况进行使用,后面也会对每一种数据结构的使用,,进行实例说明:

哨兵模式 增加redis 密码 redis哨兵设置密码_客户端_04

然后,说一下Redis的高可用配置,常用的是一主二从三哨兵的模式,可以无限扩展!

主从配置:

主:

#后台运行
daemonize yes
#pid文件
pidfile /var/run/redis_6379.pid
dbfilename dump-6379.rdb
#日志文件
logfile "6379.log"
requirepass 123456

从配置:

bind 0.0.0.0
port 6380
daemonize yes
pidfile /var/run/redis_6380.pid
dbfilename dump-6380.rdb
logfile "6380.log"
#slaveof表示作为从库的配置
slaveof 127.0.0.1 6379
#从库只能读操作
slave-read-only yes
requirepass 123456

配置过程中,用到linux的基本命令

复制命令:cp 文件1  新文件2 

删除命令:rm -r 文件1  文件2

重命名:   mv 文件1   新命名文件1

编辑conf文件: vim redis-6379.conf,在编辑过程中,搜索 /+搜索字符,如/port,跳转查看使用n键,跳到文件的最后用G,删除一行dd,返回修改的ctrl+u(windows的ctrl+z)

编辑后,的保存退:wq!、不保存退出:q!

缓存Redis用到了keys * ,flushall

三个sentinel配置,以其中一个为代表:

port 26380
daemonize yes
# sentinel announce-ip <ip>
# sentinel announce-port <port>
dir "/opt/redis-5.0.5/data"
logfile "26380.log"

sentinel monitor mymaster 127.0.0.1 6380 2
sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

然后可以通过spring boot于jedis集成来测试,哨兵的作用和主从同步的作用,如下代码

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.*;

/**
 * @ClassName RedisSentinelMasterSlaveTest
 * @Description TODO
 * @Author 邢庆
 * @Date 2019/9/14 13:46
 * @Version 1.0
 **/
@Slf4j
public class RedisSentinelMasterSlaveTest {

    @Test
    public void testRedisSentinelMasterSlave() {
        Assert.assertEquals("1", "1");
        //首先讲所有的sentinel放到Set集合中
        Set<String> sentinelSets = new HashSet<>();
        sentinelSets.add("114.215.127.153:26379");
        sentinelSets.add("114.215.127.153:26380");
        sentinelSets.add("114.215.127.153:26381");

        //创建Jedis的sentinel的池子
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("mymaster", sentinelSets, "123321123");

        while(true) {
            Jedis jedis = null;
            try {
                HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster();
                String host = currentHostMaster.getHost();
                int port = currentHostMaster.getPort();

                log.info("the host is : " + host + " and the port is : " + port);

                //保存值到Redis中,首先建立于主库master的链接
                jedis = jedisSentinelPool.getResource();
               
                String key = "key" + new Random().nextInt(10000);
                String value = "value" + new Random().nextDouble();
                //通过jedis将值保存到redis中,并可以实现主从同步
                jedis.set(key, value);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                if(jedis != null) {
                    jedis.close();
                }
            }
        }



    }

}

接下来一篇会对Redis的数据结构的操作进行说明和使用实例介绍