• Redis的一些事一些情


    一、安装、配置

    *** 这些官网都有说的啊,所以一定要学会看官网,但是也要记笔记,因为官网很简略,坑很多
    *** 基于 redis-6.0.6

    1)到官网下载最新stable版,或者直接:wget
    2)解压  
       tar xzf redis-6.0.6.tar.gz  -C  ./redis-src/      ## 解压到指定目录
       tar xzf redis-6.0.6.tar.gz                        ## 未验证
    3)make  -- 在解压后的目录里面进行编译
       *** 如果报错提示缺少gcc(报错gcc: Command not found),则安装gcc :  yum install -y gcc
       *** 如果报错提示:Newer version of jemalloc required 则在make时加参数:make MALLOC=libc (实践中,我换了一个相对旧的redis版本:从5版换到了2版)
            make distclean
       *** redis-6.0.6 的安装 服务器默认的linux版本过低,需要升级gcc

    yum -y install centos-release-scl
            yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
            scl enable devtoolset-9 bash
            echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile   ##修改环境变量
            gcc -v
    参考文档:https://blog.csdn.net/u014539465/article/details/106650955?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
    View Code

    4)安装redis(实际上是安装可执行文件,即是那个bin客户端服务器启动目录啦)
       make install PREFIX=/usr/local/software/redis  指定目录安装(所以你就可以在/usr/local/redis  这个目录看到redis 的bin目录了)
       make install                                   不指定目录安装(千万千万不要不要用这种,看不到bin目录的)
    5) /etc/profile 设置全局变量(在哪里启动,很重要哦,好像慢慢理解了):
       ##set redis environment
        export REDIS_HOME=/home/richmail/redis/redis-conf
        export PATH=$PATH:$REDIS_HOME/bin
        事后记得:source /etc/profile
    6) 进入目录 utils : /home/richmail/redis/redis-6.0.8/utils
        然后:./install_server.sh  
        一开始会提示你用哪个端口的:然后一路下去
        例如:(当然后面的话就不是 6379 了)
            [root@www utils]# ./install_server.sh

    Welcome to the redis service installer
            This script will help you easily set up a running redis server
    
            Please select the redis port for this instance: [6379]
            Selecting default: 6379
            Please select the redis config file name [/etc/redis/6379.conf]
            Selected default - /etc/redis/6379.conf
            Please select the redis log file name [/var/log/redis_6379.log]
            Selected default - /var/log/redis_6379.log
            Please select the data directory for this instance [/var/lib/redis/6379]
            Selected default - /var/lib/redis/6379
            Please select the redis executable path [/usr/local/bin/redis-server]
            Selected config:
            Port           : 6379
            Config file    : /etc/redis/6379.conf
            Log file       : /var/log/redis_6379.log
            Data dir       : /var/lib/redis/6379
            Executable     : /usr/local/bin/redis-server
            Cli Executable : /usr/local/bin/redis-cli
            Is this ok? Then press ENTER to go on or Ctrl-C to abort.
            Copied /tmp/6379.conf => /etc/init.d/redis_6379
            Installing service...
            Successfully added to chkconfig!
            Successfully added to runlevels 345!
            Starting Redis server...
            Installation successful!    
    View Code

            一个物理机中可以有多个redis实例(进程),通过port区分    
            可执行程序就在一份目录里,但是内存中有多个实例需要各自的配置文件,持久化目录等资源
    6)启动关闭、查看服务进程:
        service redis_6379 status/stop/start
        ps -ef | grep redis
        redis-cli shutdown  ## 服务器安全关闭,没有密码
        redis-cli -a yourPassword shutdown    ## 服务器安全关闭,带密码
        kill -9 pid  ## 强制关闭,可能会造成Redis内存数据丢失。
        redis-server /home/xiaofeng01/redis-4.0.11/redis.conf   ## 启动服务器,一般都指定配置文件启动的啦
        redis-cli -h 10.203.238.198 -p 8080    ## 启动客户端,指定ip端口号启动,h表示host,p表示ip
        redis-cli -h 127.0.0.1 -a "1q2w3e"     ## 带密码启动客户端
        /home/xiaofeng01/redis-4.0.11/src/redis-sentinel sentinel.conf   ## 启动哨兵模式服务
    7)redis-conf配置:
          * 通用配置:
           daemonize yes      ## 守护线程
            protected-mode no  ## 保护模式,一般关闭,以保持同信
            bind:0.0.0.0       ## 0.0.0.0表示对外所有ip都开放
                port:6380          ## 修改端口号,默认6379启动
                requirepass yourPassword   ##  设置密码验证
          * 主从模式(redis6不是这个了,待修正)
            slaveof masterip masterport
           * 集群模式
          cluster-enabled yes  ## 开启集群 把注释#去掉
          cluster-config-file nodes.conf  ## 集群的配置 配置文件首次启动自动生成
          cluster-node-timeout 5000  ## 请求超时 设置5秒够了
               appendonly yes
               bind 127.0.0.1 192.168.34.63  ## 这个东西,官网没有,实际验证是需要的,待深入理解
       ** sentinel.conf(待验证)
          sentinel monitor mymaster 127.0.0.1 6379 1         ## 主节点名  主节点ip  主节点port  判断主服务器客观失效至少多少个哨兵(一般情况下主要改这个就可以了,其它很多时候都是默认配置)
          sentinel down-after-milliseconds mymaster 10000    ## Sentinel 认为服务器已经断线所需的毫秒数
          sentinel failover-timeout mymaster 60000           ## 当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel 将会认为此次failover失败
          sentinel parallel-syncs mymaster 1                 ## 指定了在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长。
    9)集群
      ## 开启集群,各个配置好后,各个服务器开启后:
         redis-cli --cluster create  192.168.34.63:6380 192.168.34.63:6381 192.168.34.63:6382 192.168.34.63:6383 192.168.34.63:6384 192.168.34.63:6385  --cluster-replicas 1
      ## 关闭集群,一个个关闭
         redis-cli -p 7000 shutdown
      ## 查看集群
       redis-cli -c -p 7000   开启客户端,测试集群是否正常,一定要-c才能同信
         redis-cli -p 6380 cluster nodes[| grep master/slave]  查看集群信息
      ## 模拟宕机
       redis-cli -p 7000 debug segfault

    10)客户端模式下:

    ## 帮助
    cluster help
    ## 查看集群状态信息
    cluster info
    查看集群节点信息:
    cluster nodes
    View Code

    11)脚本:

    启动

    #!/bin/bash
    
    ## redis start
    
    echo "redis is starting......"
    
    cd /etc/redis
    
    for port in {6380..6385}
    
    do
    
    redis-server ./$port.conf
    
    done
    
    ## 首次启动才需要这句
    #redis-cli --cluster create  192.168.34.63:6380 192.168.34.63:6381 192.168.34.63:6382 192.168.34.63:6383 192.168.34.63:6384 192.168.34.63:6385  --cluster-replicas 1
    
    echo "redis started......"
    View Code

    关闭

    #!/bin/bash
    
    ** redis cluster stop
    
    echo "redis cluster is stopping......"
    
    for port in {6380..6385}
    
    do
    
    redis-cli -p $port shutdown
    
    done
    
    echo "redis cluster stopped......"
    View Code

    二、概念

    1)持久化方式:RDB、AOF

    3)分布式高可用经典架构环节分析

    * 【客户端层】到【反向代理层】的高可用,是通过反向代理层的冗余来实现的。以nginx为例:有两台nginx,一台对线上提供服务,另一台冗余以保证高可用, 常见的实践是keepalived存活探测
    * 【反向代理层】到【web应用】的高可用,是通过站点层的冗余来实现的。假设反向代理层是nginx,nginx.conf里能够配置多个web后端,并且nginx能够探测到多个后端的存活性。
        * 自动故障转移:当web-server挂了的时候,nginx能够探测到,会自动的进行故障转移,将流量自动迁移到其他的web-server,整个过程由nginx自动完成,对调用方是透明的。
    * 【服务层】到【缓存层】的高可用,是通过缓存数据的冗余来实现的。 redis天然支持主从同步,redis官方也有sentinel哨兵机制,来做redis的存活性检测。
    * 【服务层】到【数据库层】的高可用,数据库层用“主从同步,读写分离”架构,所以数据库层的高可用,又分为“读库高可用”与“写库高可用”两类。
        * 读库采用冗余的方式实现高可用,写库采用keepalived存活探测 binlog进行同步
    View Code

     4)Sentinel三大工作任务,以及主客观下线:SDOWN、ODOWN

    ** 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
    ** 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
    ** 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
    View Code

    5)sentinel 整合 springboot

    引入yml配置文件:
        redis:
            sentinel:
                master: mymaster #主节点名字
                nodes: 172.16.244.133:26379
    pom文件:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    View Code

    6)Redis 集群的数据分片CRC16(key) % 16384(个哈希槽)

       *** 通过这篇文章:三张图秒懂Redis集群设计原理 终于看懂了redis集群,感谢!!!

         至少要记得记得,数据是要经过CRC16算法,然后分发到16385个哈希槽,这然后这16385个哈希槽平均分配到N个节点(如果是主从模式的话,就分配到N个主节点(就是读了嘛))

        知道了节点之后,就要考虑在增加节点、减少节点的情况是这么处理的。注意注意注意:这个是面试的时候问过的哦

    客户端命令:
    REPLICAOF 127.0.0.1 6379
    REPLICAOF no one

    参考:

    1)Redis学习笔记之五:redis高级应用(集群搭建、集群分区原理、集群操作)

  • 相关阅读:
    【java】i++与++i、i--运算
    配置ssh框架启动tomcat服务器报异常Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    jsp页面第一句话报这个错Syntax error, insert "}" to complete
    oracle忘记密码用户名被锁定_解决方案
    关于c#的单例模式,static 变量,下面一篇很不错
    Entity Framework 冲突检测,这一篇我看了比较明了
    关于lambda表达式与使用局部变量的作用域问题,下面这篇不错
    C# SelectMany 的使用
    UML类图 入门 (转载)
    VS code key shortcuts for windows
  • 原文地址:https://www.cnblogs.com/ericguoxiaofeng/p/9785458.html
Copyright © 2020-2023  润新知