最新 Redis 6 入门到精通 超详细 教程


redis 命令参考

  1. NoSQL数据库简介
  2. Redis6概述和安装
  3. 常用五大数据类型
  4. Redis配置文件详解
  5. Redis6的发布和订阅
  6. Redis新数据类型
  7. Jedis操作redis6
  8. Redis6和SpringBoot整合
  9. Redis6事务操作(并发,连接池)
  10. 持久化之RDB
  11. 持久化之AOF
  12. Redis6的主从复制
  13. Redis6的集群
  14. Redis6应用问题解决
  15. Redis6新功能

1. NoSQL数据库简介

1.1 技术发展

技术的分类:
1. 解决功能性的问题: Java,Jsp,Tomcat,HTML,JDBC,SVN.Git
2. 解决扩展性问题:Struts,Spring,SpringMVC,Hibernate,Mybatis
3. 解决性能问题,NoSQL,Java线程,Hadoop,Nginx,MQ,ElasticSearch

1.1.1 web1.0时代 :

(信息来自百度百科)

  • web1.0时代是一个群雄并起,逐鹿网络的时代
  • 虽然各个网站采用的手段和方法不同,但第一代互联网有诸多共同的特征,表现在技术创新主导模式、基于点击流量的盈利共通点、门户合流、明晰的主营兼营产业结构、动态网站
  • -在WEB1.0上做出巨大贡献的公司有Netscape,Yahoo和Google。 Netscape研发出第一个大规模商用的浏览器,Yahoo的杨致远提出了互联网黄页, 而Google后来居上,推出了大受欢迎的搜索服务。

访问数据量有限,用高性能的单点服务器可以解决大部分问题

#mermaid-svg-dHYgytnt0Bgx5oZZ .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .label text{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .node rect,#mermaid-svg-dHYgytnt0Bgx5oZZ .node circle,#mermaid-svg-dHYgytnt0Bgx5oZZ .node ellipse,#mermaid-svg-dHYgytnt0Bgx5oZZ .node polygon,#mermaid-svg-dHYgytnt0Bgx5oZZ .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-dHYgytnt0Bgx5oZZ .node .label{text-align:center;fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .node.clickable{cursor:pointer}#mermaid-svg-dHYgytnt0Bgx5oZZ .arrowheadPath{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-dHYgytnt0Bgx5oZZ .flowchart-link{stroke:#333;fill:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-dHYgytnt0Bgx5oZZ .edgeLabel rect{opacity:0.9}#mermaid-svg-dHYgytnt0Bgx5oZZ .edgeLabel span{color:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-dHYgytnt0Bgx5oZZ .cluster text{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-dHYgytnt0Bgx5oZZ .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-dHYgytnt0Bgx5oZZ text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .actor-line{stroke:grey}#mermaid-svg-dHYgytnt0Bgx5oZZ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .sequenceNumber{fill:#fff}#mermaid-svg-dHYgytnt0Bgx5oZZ #sequencenumber{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ #crosshead path{fill:#333;stroke:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .messageText{fill:#333;stroke:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-dHYgytnt0Bgx5oZZ .labelText,#mermaid-svg-dHYgytnt0Bgx5oZZ .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .loopText,#mermaid-svg-dHYgytnt0Bgx5oZZ .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-dHYgytnt0Bgx5oZZ .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-dHYgytnt0Bgx5oZZ .noteText,#mermaid-svg-dHYgytnt0Bgx5oZZ .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-dHYgytnt0Bgx5oZZ .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-dHYgytnt0Bgx5oZZ .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-dHYgytnt0Bgx5oZZ .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .section{stroke:none;opacity:0.2}#mermaid-svg-dHYgytnt0Bgx5oZZ .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-dHYgytnt0Bgx5oZZ .section2{fill:#fff400}#mermaid-svg-dHYgytnt0Bgx5oZZ .section1,#mermaid-svg-dHYgytnt0Bgx5oZZ .section3{fill:#fff;opacity:0.2}#mermaid-svg-dHYgytnt0Bgx5oZZ .sectionTitle0{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .sectionTitle1{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .sectionTitle2{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .sectionTitle3{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-dHYgytnt0Bgx5oZZ .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .grid path{stroke-width:0}#mermaid-svg-dHYgytnt0Bgx5oZZ .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-dHYgytnt0Bgx5oZZ .task{stroke-width:2}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText:not([font-size]){font-size:11px}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-dHYgytnt0Bgx5oZZ .task.clickable{cursor:pointer}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText0,#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText1,#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText2,#mermaid-svg-dHYgytnt0Bgx5oZZ .taskText3{fill:#fff}#mermaid-svg-dHYgytnt0Bgx5oZZ .task0,#mermaid-svg-dHYgytnt0Bgx5oZZ .task1,#mermaid-svg-dHYgytnt0Bgx5oZZ .task2,#mermaid-svg-dHYgytnt0Bgx5oZZ .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutside0,#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutside2{fill:#000}#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutside1,#mermaid-svg-dHYgytnt0Bgx5oZZ .taskTextOutside3{fill:#000}#mermaid-svg-dHYgytnt0Bgx5oZZ .active0,#mermaid-svg-dHYgytnt0Bgx5oZZ .active1,#mermaid-svg-dHYgytnt0Bgx5oZZ .active2,#mermaid-svg-dHYgytnt0Bgx5oZZ .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-dHYgytnt0Bgx5oZZ .activeText0,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeText1,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeText2,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeText3{fill:#000 !important}#mermaid-svg-dHYgytnt0Bgx5oZZ .done0,#mermaid-svg-dHYgytnt0Bgx5oZZ .done1,#mermaid-svg-dHYgytnt0Bgx5oZZ .done2,#mermaid-svg-dHYgytnt0Bgx5oZZ .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-dHYgytnt0Bgx5oZZ .doneText0,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneText1,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneText2,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneText3{fill:#000 !important}#mermaid-svg-dHYgytnt0Bgx5oZZ .crit0,#mermaid-svg-dHYgytnt0Bgx5oZZ .crit1,#mermaid-svg-dHYgytnt0Bgx5oZZ .crit2,#mermaid-svg-dHYgytnt0Bgx5oZZ .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCrit0,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCrit1,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCrit2,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCrit0,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCrit1,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCrit2,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-dHYgytnt0Bgx5oZZ .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-dHYgytnt0Bgx5oZZ .milestoneText{font-style:italic}#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCritText0,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCritText1,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCritText2,#mermaid-svg-dHYgytnt0Bgx5oZZ .doneCritText3{fill:#000 !important}#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCritText0,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCritText1,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCritText2,#mermaid-svg-dHYgytnt0Bgx5oZZ .activeCritText3{fill:#000 !important}#mermaid-svg-dHYgytnt0Bgx5oZZ .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-dHYgytnt0Bgx5oZZ g.classGroup text .title{font-weight:bolder}#mermaid-svg-dHYgytnt0Bgx5oZZ g.clickable{cursor:pointer}#mermaid-svg-dHYgytnt0Bgx5oZZ g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-dHYgytnt0Bgx5oZZ g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-dHYgytnt0Bgx5oZZ .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-dHYgytnt0Bgx5oZZ .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .dashed-line{stroke-dasharray:3}#mermaid-svg-dHYgytnt0Bgx5oZZ #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ .commit-id,#mermaid-svg-dHYgytnt0Bgx5oZZ .commit-msg,#mermaid-svg-dHYgytnt0Bgx5oZZ .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-dHYgytnt0Bgx5oZZ g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-dHYgytnt0Bgx5oZZ g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-dHYgytnt0Bgx5oZZ g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-dHYgytnt0Bgx5oZZ .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-dHYgytnt0Bgx5oZZ .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-dHYgytnt0Bgx5oZZ .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-dHYgytnt0Bgx5oZZ .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-dHYgytnt0Bgx5oZZ .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-dHYgytnt0Bgx5oZZ .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-dHYgytnt0Bgx5oZZ .edgeLabel text{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-dHYgytnt0Bgx5oZZ .node circle.state-start{fill:black;stroke:black}#mermaid-svg-dHYgytnt0Bgx5oZZ .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-dHYgytnt0Bgx5oZZ #statediagram-barbEnd{fill:#9370db}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-state .divider{stroke:#9370db}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-dHYgytnt0Bgx5oZZ .note-edge{stroke-dasharray:5}#mermaid-svg-dHYgytnt0Bgx5oZZ .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-dHYgytnt0Bgx5oZZ .error-icon{fill:#522}#mermaid-svg-dHYgytnt0Bgx5oZZ .error-text{fill:#522;stroke:#522}#mermaid-svg-dHYgytnt0Bgx5oZZ .edge-thickness-normal{stroke-width:2px}#mermaid-svg-dHYgytnt0Bgx5oZZ .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-dHYgytnt0Bgx5oZZ .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-dHYgytnt0Bgx5oZZ .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-dHYgytnt0Bgx5oZZ .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-dHYgytnt0Bgx5oZZ .marker{fill:#333}#mermaid-svg-dHYgytnt0Bgx5oZZ .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-dHYgytnt0Bgx5oZZ {color: rgba(0, 0, 0, 0.75);font: ;}

http:// 协议
web服务器
数据库服务

1.1.2 web2.0时代 :

(信息来自百度百科)

  • Web2.0是相对于Web1.0的新的时代。
  • 指的是一个利用Web的平台,由用户主导而生成的内容互联网产品模式,为了区别传统由网站雇员主导生成的内容而定义为第二代互联网,web2.0是一个新的时代。
  • 抛开纷繁芜杂的Web 2.0现象,进而将其放到科技发展与社会变革的大视野下来看,Web 2.0可以说是信息技术发展引发网络革命所带来的面向未来、以人为本的创新2.0模式在互联网领域的典型体现,是由专业人员织网到所有用户参与织网的创新民主化进程的生动注释。
#mermaid-svg-UdOoMcqNYdQd6Ihr .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .label text{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .node rect,#mermaid-svg-UdOoMcqNYdQd6Ihr .node circle,#mermaid-svg-UdOoMcqNYdQd6Ihr .node ellipse,#mermaid-svg-UdOoMcqNYdQd6Ihr .node polygon,#mermaid-svg-UdOoMcqNYdQd6Ihr .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-UdOoMcqNYdQd6Ihr .node .label{text-align:center;fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .node.clickable{cursor:pointer}#mermaid-svg-UdOoMcqNYdQd6Ihr .arrowheadPath{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-UdOoMcqNYdQd6Ihr .flowchart-link{stroke:#333;fill:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-UdOoMcqNYdQd6Ihr .edgeLabel rect{opacity:0.9}#mermaid-svg-UdOoMcqNYdQd6Ihr .edgeLabel span{color:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-UdOoMcqNYdQd6Ihr .cluster text{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-UdOoMcqNYdQd6Ihr .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-UdOoMcqNYdQd6Ihr text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .actor-line{stroke:grey}#mermaid-svg-UdOoMcqNYdQd6Ihr .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .sequenceNumber{fill:#fff}#mermaid-svg-UdOoMcqNYdQd6Ihr #sequencenumber{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr #crosshead path{fill:#333;stroke:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .messageText{fill:#333;stroke:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-UdOoMcqNYdQd6Ihr .labelText,#mermaid-svg-UdOoMcqNYdQd6Ihr .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .loopText,#mermaid-svg-UdOoMcqNYdQd6Ihr .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-UdOoMcqNYdQd6Ihr .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-UdOoMcqNYdQd6Ihr .noteText,#mermaid-svg-UdOoMcqNYdQd6Ihr .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-UdOoMcqNYdQd6Ihr .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-UdOoMcqNYdQd6Ihr .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-UdOoMcqNYdQd6Ihr .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .section{stroke:none;opacity:0.2}#mermaid-svg-UdOoMcqNYdQd6Ihr .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-UdOoMcqNYdQd6Ihr .section2{fill:#fff400}#mermaid-svg-UdOoMcqNYdQd6Ihr .section1,#mermaid-svg-UdOoMcqNYdQd6Ihr .section3{fill:#fff;opacity:0.2}#mermaid-svg-UdOoMcqNYdQd6Ihr .sectionTitle0{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .sectionTitle1{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .sectionTitle2{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .sectionTitle3{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-UdOoMcqNYdQd6Ihr .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .grid path{stroke-width:0}#mermaid-svg-UdOoMcqNYdQd6Ihr .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-UdOoMcqNYdQd6Ihr .task{stroke-width:2}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText:not([font-size]){font-size:11px}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-UdOoMcqNYdQd6Ihr .task.clickable{cursor:pointer}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText0,#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText1,#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText2,#mermaid-svg-UdOoMcqNYdQd6Ihr .taskText3{fill:#fff}#mermaid-svg-UdOoMcqNYdQd6Ihr .task0,#mermaid-svg-UdOoMcqNYdQd6Ihr .task1,#mermaid-svg-UdOoMcqNYdQd6Ihr .task2,#mermaid-svg-UdOoMcqNYdQd6Ihr .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutside0,#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutside2{fill:#000}#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutside1,#mermaid-svg-UdOoMcqNYdQd6Ihr .taskTextOutside3{fill:#000}#mermaid-svg-UdOoMcqNYdQd6Ihr .active0,#mermaid-svg-UdOoMcqNYdQd6Ihr .active1,#mermaid-svg-UdOoMcqNYdQd6Ihr .active2,#mermaid-svg-UdOoMcqNYdQd6Ihr .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-UdOoMcqNYdQd6Ihr .activeText0,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeText1,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeText2,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeText3{fill:#000 !important}#mermaid-svg-UdOoMcqNYdQd6Ihr .done0,#mermaid-svg-UdOoMcqNYdQd6Ihr .done1,#mermaid-svg-UdOoMcqNYdQd6Ihr .done2,#mermaid-svg-UdOoMcqNYdQd6Ihr .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-UdOoMcqNYdQd6Ihr .doneText0,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneText1,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneText2,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneText3{fill:#000 !important}#mermaid-svg-UdOoMcqNYdQd6Ihr .crit0,#mermaid-svg-UdOoMcqNYdQd6Ihr .crit1,#mermaid-svg-UdOoMcqNYdQd6Ihr .crit2,#mermaid-svg-UdOoMcqNYdQd6Ihr .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCrit0,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCrit1,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCrit2,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCrit0,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCrit1,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCrit2,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-UdOoMcqNYdQd6Ihr .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-UdOoMcqNYdQd6Ihr .milestoneText{font-style:italic}#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCritText0,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCritText1,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCritText2,#mermaid-svg-UdOoMcqNYdQd6Ihr .doneCritText3{fill:#000 !important}#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCritText0,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCritText1,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCritText2,#mermaid-svg-UdOoMcqNYdQd6Ihr .activeCritText3{fill:#000 !important}#mermaid-svg-UdOoMcqNYdQd6Ihr .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-UdOoMcqNYdQd6Ihr g.classGroup text .title{font-weight:bolder}#mermaid-svg-UdOoMcqNYdQd6Ihr g.clickable{cursor:pointer}#mermaid-svg-UdOoMcqNYdQd6Ihr g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-UdOoMcqNYdQd6Ihr g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-UdOoMcqNYdQd6Ihr .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-UdOoMcqNYdQd6Ihr .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .dashed-line{stroke-dasharray:3}#mermaid-svg-UdOoMcqNYdQd6Ihr #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr .commit-id,#mermaid-svg-UdOoMcqNYdQd6Ihr .commit-msg,#mermaid-svg-UdOoMcqNYdQd6Ihr .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-UdOoMcqNYdQd6Ihr g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-UdOoMcqNYdQd6Ihr g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-UdOoMcqNYdQd6Ihr g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-UdOoMcqNYdQd6Ihr .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-UdOoMcqNYdQd6Ihr .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-UdOoMcqNYdQd6Ihr .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-UdOoMcqNYdQd6Ihr .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-UdOoMcqNYdQd6Ihr .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-UdOoMcqNYdQd6Ihr .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-UdOoMcqNYdQd6Ihr .edgeLabel text{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-UdOoMcqNYdQd6Ihr .node circle.state-start{fill:black;stroke:black}#mermaid-svg-UdOoMcqNYdQd6Ihr .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-UdOoMcqNYdQd6Ihr #statediagram-barbEnd{fill:#9370db}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-state .divider{stroke:#9370db}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-UdOoMcqNYdQd6Ihr .note-edge{stroke-dasharray:5}#mermaid-svg-UdOoMcqNYdQd6Ihr .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-UdOoMcqNYdQd6Ihr .error-icon{fill:#522}#mermaid-svg-UdOoMcqNYdQd6Ihr .error-text{fill:#522;stroke:#522}#mermaid-svg-UdOoMcqNYdQd6Ihr .edge-thickness-normal{stroke-width:2px}#mermaid-svg-UdOoMcqNYdQd6Ihr .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-UdOoMcqNYdQd6Ihr .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-UdOoMcqNYdQd6Ihr .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-UdOoMcqNYdQd6Ihr .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-UdOoMcqNYdQd6Ihr .marker{fill:#333}#mermaid-svg-UdOoMcqNYdQd6Ihr .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-UdOoMcqNYdQd6Ihr {color: rgba(0, 0, 0, 0.75);font: ;}

http://
应用服务器
数据库服务
安卓
CPU及内存压力
IO压力

1.1.3 解决cpu及内存压力

很烦的一个问题: session存在哪里???

例如使用nginx布置集群,session存在哪?

方案一: 存在cookie中:

  1. 不安全
  2. 网络负担效率低

方案二:存在文件服务器或者数据库中

  1. 存在大量IO问题

方案三:session复制

  1. session数据冗余
  2. 节点越多浪费越大

方案四: 缓存数据库

  1. 完全在内存中
  2. 数据结构简单

看这张图:

1.2 NoSQL数据库

1.2.1 NoSQL概述

(来源百度百科)

  • NoSQL最常见的解释是“non-relational”, “Not Only SQL”也被很多人接受。

  • NoSQL,泛指非关系型的数据库。不依赖业务逻辑方式存储,而已Key-Value模式存储

  • 随着互联网web2.0网站的兴起,传统的关系数据库在处理web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,出现了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。

  • NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,特别是大数据应用难题。

  • NoSQL仅仅是一个概念,泛指非关系型的数据库,区别于关系数据库,它们不保证关系数据的ACID(原子,隔离,一致,持久)特性,不遵循SQL标准,远超SQL的性能。NoSQL是一项全新的数据库革命性运动,其拥护者们提倡运用非关系型的数据存储,相对于铺天盖地的关系型数据库运用,这一概念无疑是一种全新的思维的注入。

  • NoSQL有如下优点:易扩展,NoSQL数据库种类繁多,但是一个共同的特点都是去掉关系数据库的关系型特性。数据之间无关系,这样就非常容易扩展。无形之间也在架构的层面上带来了可扩展的能力。大数据量,高性能, NoSQL数据库都具有非常高的读写性能,尤其在大数据量下,同样表现优秀。这得益于它的无关系性,数据库的结构简单。

1.2.2 NoSQL适用场景

  • 对数据高并发的读写
  • 海量数据的读写
  • 对数据高扩展性的

1.2.3 NoSQL不适用场景

  • 需要事务支持
  • 基于sql的结构化查询存储,处理复杂的关系,需要即席查询
  • 用不着sql的和用了也不行的情况,请考虑用NoSQL

1.2.4 常见的NoSQL 数据库

  1. Memcache

    • 早出现的NoSQL数据库
    • 数据都在内存中,一般不持久化
    • 支持简单的K-V模式,支持类型单一
    • 一般作为缓存数据库辅助持久化数据库
  2. Redis 本次讲义

    • 几乎覆盖了Memcached的绝大部分功能
    • 数据都在内存中,支持持久化,主要作用域备份恢复
    • 除了支持简单的K-V模式,还支持多种数据结构的存储,比如:list,set,hash,zset等
    • 一般作为缓存数据库辅助持久化数据库
  3. MongoDB

    • 高性能,开源,模式自由(schema free)的文档型数据库
    • 数据都在内存中,如果内存不足,把不常用的数据保存到硬盘
    • 虽然是K-V模式,但是对value(尤其是JSON)提供了丰富的查询功能
    • 支持二进制数据及大型对象
    • 可以根据数据的特点代替RDBMS,成为独立的数据库,或者配合RDBMS,存储特定的数据

1.3 行式存储数据库(大数据时代)

1.3.1 行式数据库

每一 行 为整个数据的一部分

id name city age
1 张三 北京 20
2 李四 上海 45
3 王五 哈尔并 30

select * from user where id=3 快
select avg(age) form users 慢

1.3.2 列式数据库

每一 列 为整个数据的一部分

id name city age
1 张三 北京 20
2 李四 上海 45
3 王五 哈尔并 30

查询年龄平均值 快 OLAP 分析型处理
查询id为3的人员信息 慢 OLTP 事务型处理

1.3.2.1 Hbase

介绍: Hbase

  • HBase是Hadoop项目中的数据库,大量数据中进行随机,实时的读写操作场景
  • HBase目标就是处理数据量非常庞大的表,可以用普通计算机处理超过10亿行数据,还可以处理有数百万列元素的数据表
  • HBase – Hadoop Database,是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统
  • 利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。

1.3.2.2 Cassandre

介绍: Cassandre

  • Cassandra是一套开源分布式NoSQL数据库系统。
  • 它最初由Facebook开发,用于储存收件箱等简单格式数据,集GoogleBigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身Facebook于2008将 Cassandra 开源,此后,由于Cassandra良好的可扩展性,被Digg、Twitter等知名Web 2.0网站所采纳,成为了一种流行的分布式结构化数据存储方案。

1.4 图关系型数据库

如:Neo4j
主要应用:社会关系,公共交通网络,地图及网络拓扑(n*(n-1)/2)

1.5 DB-Engines数据库排名

DB-Engines数据库排名
点击↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


2. Redis6概述和安装

  • Redis是一个开源的K-V存储系统.
  • 和memcached类似,它支持存储的值类型相对更多,包括string(字符串)、List(链表)、set(集合)、zset(sorted set-有序集合)和Hash(哈希类型)。
  • 这些数据类型都支持Push/pop、add/remove及 取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。
  • 在此基础上,Redis支持各种不同方式的排序.
  • 与Memcached一样,为了保证效率,数据都是缓存在内存中.
  • 区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件.
    -并且在此基础上实现了**master-slave(主从)**同步。

2.1 应用场景

2.1.1 配合关系型数据库做高速缓存

  • 高频次,热门访问的数据,降低数据库IO
  • 分布式架构,做session共享

2.1.2 多样的数据结构存储持久化数据

场景 方法
最新N个数据 通过列表实现按自然时间排序的数据
排行榜,Top N 利用zset(有序集合)
时效性的数据,比如手机验证码 Expire过期
计数器,秒杀 原子性,自增方法incr、Decr
去除大量数据中的重复数据 利用set集合
构建队列 利用list集合
发布订阅消息系统 公共子模式

2.2 Redis安装

可以选择菜鸟教程:菜鸟教程有windows的版本在github上

菜鸟教程:https://www.runoob.com/redis/redis-install.html

或者: 官网的只有linux版本的

Redis官网 Redis中文网
http://redis.io http://redis.cn

进入官网后:正中间有一行Download it,在他的下面有下载链接Redis 6.2.2 is the latest stable version.

或者不去官网点击此处:Redis 6.2.2 linux版

2.2.1 版本

  • 6.2.2 for Linux(redis-6.2.2.tar.gz)
  • 不用考虑在windows对Redis的支持

2.2.2 传输到linux中

  • 使用xftp,xshell等工具,或者直接cmd命令put进去

  • 创建目录 opt

    mkdir opt

  • 传输

    put redis-6.2.2.tar.gz

2.2.3 准备工作:gcc编译器

记得联网!!!
记得联网!!!
记得联网!!!

可以只安装gcc环境: ubuntu等版本的Linux使用 apt ,不用yum

yum instarll gcc

或者全部安装!!!

安装C语言编译环境

yum install centos-release-scl scl-utils-build
yum install -y devtoolset-8-toolchain
scl enable devtoolset-8 base

测试gcc版本

gcc --version

2.2.4 安装

解压:

tar -zxvf redis-6.2.2.tar.gz

  • 切换目录

    cd redis-6.2.2

  • 编译: 变异成c文件

    make

    最终结果:

    如果没有准备好C语言编译环境,make会报错,:
    致命错误:jemalloc/jemalloc.h:没有那个文件或目录

    检查gcc是否安装成功

    gcc --version

    运行 :清空编译文件

    make distclean

    再次运行meike

    make

现在只是编译好了,并没有安装,现在进行安装

安装:
跳过make test 执行make install

make install

如果没有报错,则安装成功
默认安装路径为:/usr/local/bin

2.2.5 安装目录: /usr/local/bin

切换目录

cd /usr/local/bin
ls

文件如下:

目录名 用处
rredis-benchmark 性能测试工具,可以在自己本子运行,看看自己本子性能如何
redis-check-aof 修复有问题的AOF文件(持久化),rdb和aof后面讲·
redis-check-dump 修复有问题的dump.rdb文件(持久化)
redis-sentinel redis集群使用·
redis-server redis服务器启动命令
redis-cli 客户端,操作入口

2.3 Redis 启动

2.3.1 前台 启动(不推荐)

前台启动,命令行窗口不能关闭,否则服务停止

redis-server //没有空格

2.3.2 后台 启动(推荐)

缺点:无法看到控制台输出打印日志
优点:关闭窗口(例如xshell软件) 依旧可以使用

cd /opt
cd redis-6.2.2
ls

备份redis.conf 文件

cp redis.conf /etc/redis.conf
cd /etc
ls

修改etc文件夹下得redis.conf

vi redis.conf

daemonize 是否可以后台启动?(默认no)

搜索daemonize: 按下 / 键,输入daemonize
当前值为:

daemonize no
改为:
daemonize yes

退出:

:wq

完成修改

利用修改后的redis.conf后台启动redis:

cd /usr/local/bin
redis-server /etc/redis.conf

这样就成功启动了redis
查看进程:

ps -ef | grep redis

2.3.2.1 用客户端访问

redis-cli

查看链接:

ping
期望执行结果:
PONG

2.3.2.2 停止服务

  1. ps -ef | grep redis
    kill -9 进程号

  2. redis-cli shutdown

  3. redis-cli
    sjutdown

  4. 多实例关闭,指定端口关闭:

    redis-cli -p 端口号 shutdown

2.4 Redis介绍相关知识

端口 6379 从何而来???

女星:Alessia Merz 九键输入法

  • 默认16个数据库,类似数组下标从0开始,初始默认使0号库
  • 使用命令select <,dbid>来切换数据库。如: select 8
  • 统—密码管理,所有库同样密码。
  • bsize查看当前数据库的key的数量
  • flushdb清空当前库
  • flushall通杀全部库。

  • Redis是单线程+多路IO复用技术

  • 多路复用是指使用一个线程来检查多个文件描述符( Socket )的就绪状态,比如调用select和poll函数,传入多个文件描术符,如里有一个文件描犬符就省认态,如调A用阻塞直到超时。得到就绪状态后进行真正的操作可以在同一个线程里执行,也可以启动线程执行(比如使用线程池)

  • 串行vs多线程+锁( memcached ) vs单线程+多路IO复用(Redis)

总结:(与Memcache三点不同:支持多数据类型,支持持久化,单线程+多路Io复用)

3. Redis常用的五大数据类型

  • 字符串(String)
  • 列表(List)
  • 集合(Set)
  • 哈希(Hash)
  • 有序集合(Zset)

3.1 Redis 键(key)

  1. 查看当前库的所有Key(keys *1)

    keys *

  2. 判断某个key是否存在

    exists key

  3. 查看你的key是什么类型

    type key

  4. 删除指定的key数据

    del key

  5. 根据value选择非阻塞删除(仅将Keys从 keyspace 元数组中删除,真正的删除会在后续异步操作)

    unlink key

  6. 给定的key设置过期时间(秒s)

    expire kay 10

  7. 查看还有多少秒过期(-1永不过期,-2已过期)

    ttl key

  8. 切换数据库

    select (bdid)

  9. 查看当前数据库key数量

    bdsize

  10. 清空当前库(慎用)

    flushdb

  11. 通杀全部库(慎用)

    flushall

3.2 Redis 之 字符串String

3.2.1 简介

  • String是Redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value

  • String类型是二进制安全的。意味着Redis的string可以包含任何数据。比如jipg图片或者序列化的对象。·

  • String类型是Redis最基本的数据类型,一个Redis中字符串value最多可以是512M

3.2.2 常用命令

  • . set [key] [value] 添加键值对s

    127.0.0.1:6379> set

    1. NX: 当数据库中key不存在时,可以将key-value 添加数据库s
    2. XX: 当数据库中key存在时,可以将key-value添加数据库,与NX参数互斥v*
    3. EX: key 的超时秒数
    4. PX: key的超时毫秒数,与EX互斥v

    setnx
    setxx
    setex

  • get [key]查询对应键值

  • append [key] [value] 将给定的追加到原值的末尾

  • strlen [key]获得值的长度

  • setnx [key] [value] 只有在key 不存在时设置key的值

  • incr [key]
    将key中储存的数字值增1
    只能对数字值操作,如果为空,新增值为1

  • decr [key]
    将kev中储存的数字值减1
    只能对数字值操作,如果为空,新增值为-1

incrby / decrby [key] [步长]将key中储存的数字值增减。自定义步长.

原子性:

INCR key
对存储在指定key的数值执行原子的加1操作

起始版本:1.0.0
时间复杂度:O(1)

  • 所谓原子操作是指不会被线程调度机制打断的操作;
  • 这种操作一旦开始,就一直运行到结束中间不会有任何 contextswitch(切换到另一个线程)

在单线程中,能够在单条指令中完成的操作都可以认为是"原子操作”,因为中断只能发生于指令之间。
在多线程中,不能被其它进程(线程)打断的操作就叫原子操作。

Redis单命令的原子性主要得益于Redis的单线程。

  • mset

    mset【.key1】【value1】【key2】【value2】 … 同时设置一个或多个key-value对v
    mget 【key1】【key2】【key3】 … 同时获取一个或多个value
    msetnx 【key1】【value1】【key2】【value2】 … 同时设置一个或多个key-value对,当且仅当所有给定key都不存在,原子性,有一个失败则都失败

  • range
    getrange 【key】<起始位置>结束位置>
    获得值的范围,类似java中的substring,前包,后包
    setrange 【key】<起始位置>【value】
    用覆写【key】所储存的字符串值,从<起始位置>开始(索引从0开始)。

  • setex
    setex 【key】<过期时间>【value】
    设置键值的同时,设置过期时间,单位秒。
    vgetset 【key】【value】
    以新换旧,设置了新值同时获得旧值。

3.2.3 String 的数据结构

String 的数据结构为简单动态字符串(Simple Dynamic String,缩写SDS)。是可以修改的字符串,内部结构实现上类似于Java的 ArrayList,采用预分配冗余空间的方式来减少内存的频繁分配。

如图中所示,内部为当前字符串实际分配的空间capacity一般要高于实际字符串长度len。当字符串长度小于1M时,扩容都是加倍现有的空间,如果超过1M,扩容时一次只会多扩1M的空间。需要注意的是字符串最大长度为512M。

3.3 Redis 之 列表List

3.3.1 简介

  • 单键多值
  • Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。
  • 它的底层实际是个双向链表,对两端的操作性能很高,通过索引下标的操作中间的节点性能会较差。

3.3.2 常用命令

命令 解释
lpush/rpush【key】【value1】【value2】【value3】… 从左边/右边插入一个或多个值。
lpop/rpop 【key】 从左边/右边吐出一个值。值在键在,值光键亡。
rpoplpush 【key1】【key2】 从【key1】列表右边吐出一个值,插到【key2】列表左边。
lrange 【key】【start】【stop】 按照索引下标获得元素(从左到右)
lrange mylist 0 -1 0左边第一个,-1右边第一个,(0 -1表示获取所有)vlindex 【key】【index】按照索引下标获得元素(从左到右)
llen 【key】 获得列表长度
linsert 【key】 before 【value】【newvalue】 在【value】的后面插入【newvalue】插入值 (after)之前
lrem 【key】【n】【value】 从左边删除n个value(从左到右)

3.3.3 数据结构

  • List的数据结构为快速链表quickList。
  • 首先在列表元素较少的情况下会使用一块连续的内存存储,这个结构是ziplist,也即是压缩列表。
  • 它将所有的元素紧挨着一起存储,分配的是一块连续的内存。
  • 当数据量比较多的时候才会改成quicklist。
  • 因为普通的链表需要的附加指针空间太大,会比较浪费空间。比如这个列表里存的只是int类型的数据,结构上还需要两个额外的指针prev和next。

  • Redis将链表和ziplist结合起来组成了quicklist。也就是将多个ziplist使用双向指针串起来使用。这样既满足了快速的插入删除性能,又不会出现太大的空间冗余

3.4 Redis 之 集合Set

3.4.1 简介

  • Redis set对外提供的功能与list类似是一个列表的功能,特殊之处在于set是可以自动排重的,当你需要存储一个列表数据,又不希望出现重复数据时,set是一个很好的选择,并且set提供了判断某个成员是否在一个set集合内的重要接口,这个也是list所不能提供的。

  • Redis的set是string类型的无序集合。它底层其实是一个value为null的 hash表,所以添加,删除,查找的复杂度都是O(1)。

  • 一个算法,随着数据的增加,执行时间的长短,如果是O(1),数据增加,查找数据的时间不变,

3.4.2 常用命令

命令 解释
sadd 【key】【value1】【value2】… 将一个或多个member元素加入到集合key中,已经存在的member元素将被忽略;
smembers 【key】 取出该集合的所有值。
sismember 【key】【value】 判断集合【key】是否为含有该<value值,有1,没有0
scard【key】 返回该集合的元素个数。
srem【key】【value1】【value2】… .删除集合中的某个元素。
spop 【key】 随机从该集合中吐出一个值。
srandmember【key】【n】 随机从该集合中取出n个值。不会从集合中删除。
smove 【source】【destination】value 把集合中一个值从一个集合移动到另一个集合·
sinter【key1】【key2】 返回两个集合的交集元素。·
sunion 【key1】【key2】 返回两个集合的并集元素。
sdiff 【key1】【key2】 返回两个集合的差集元素(key1中的,不包含key2中的)

3.4.3 数据结构

  • set数据结构是dict字典,字典是用哈希表实现的。
  • Java中 HashSet的内部实现使用的是 HashMap,只不过所有的value都指向同一个对象。
  • Redis的set结构也是一样,它的内部也使用hash结构,所有的value都指向同一个内部值。

3.5 Redis 之 哈希Hash

3.5.1 简介

  • Redis hash是一个键值对集合:
  • Redis hash是一个string类型的fieldvalue的映射表,hash 特别适合用于存储对象。
  • 类似Java里面的Map<String,Object>
  • 用户D为查找的key,存储的value用户对象包含姓名,年龄,生日等信息,如果用普通的 key/value结构来存储

3.5.2 常用命令

命令 解释
hset 【key】【field】【value】 给【key】集合中的【field】键赋值【value】
hget 【key1】【field】 从【key1】集合【field】取出value
hmset【key1】【field1】【value1】【field2】【value2】… 批量设置hash 的值
hexists【key1】【field】 查看哈希表key中,给定域field是否存在。
hkeys 【key】 列出该hash集合的所有fieldv
hvals 【key】 列出该hash集合的所有valuel
hincrby【key】【field】【increment】 为哈希表key中的域field的值加上增量1 -1
hsetnx 【key】【field】【value】 将哈希表key中的域field 的值设置为value ,当且仅当域field不存在.

3.5.3 数据结构

Hash类型对应的数据结构是两种: ziplist(压缩列表),hashtable(哈希表)。当field-value长度较短且个数较少时,使用ziplist,否则使用hashtable,

3.6 Redis 之 有序集合Zset(sorted set)

3.6.1 简介

  • Redis有序集合zset 与普通集合set非常相似,是一个没有重复元素的字符串集合。
  • 不同之处是有序集合的每个成员都关联了一个评分( score) ,这个评分 ( score )被用来按照从最低分到最高分的方式排序集合中的成员。集合的成员是唯一的,但是评分可以是重复了。
  • 因为元素是有序的,所以你也可以很快的根据评分( score )或者次序( position )来获取一个范围的元素。
  • 访问有序集合的中间元素也是非常快的,因此你能够使用有序集合作为一个没有重复成员的智能列表。

3.6.2 常用命令

命令 解释
zadd【key】【score1】【value1】【score2】【value2】 … 将一个或多个member元素及其score值加入到有序集key当中。
zrange 【key】【start】【stop】[WITHSCORES] 返回有序集key 中,下标在【start】【stop】之间的元素,加上WITHSCORES,可以让分数一起和值返回到结果集。
zrangebyscore. key minmax [withscores] [limit offset count] 返回有序集 key中,所有score值介于min和max 之间(包括等于min 或max )的成员。
zrevrangebyscore key maxmin [withscores] [limit offset count] 同上,改为从大到小排列。
zincrby 【key】【increment】【value】 为元素的score加上增量
zrem 【key】【value】 删除该集合下,指定值的元素,
zcount 【key】【min】【max】 统计该集合,分数区间内的元素个数
zrank 【key】【value】 返回该值在集合中的排名,从О开始。

3.6.3 数据结构

  • SortedSet(zset)是Redis提供的一个非常特别的数据结构,一方面它等价于Java的数据结构Map<String,Double>,可以给每一个元素value赋予一个权重score,另一方面它又类似于TreeSet,内部的元素会按照权重score进行排序,可以得到每个元素的名次,还可以通过score的范围来获取元素的列表。
  • zset底层使用了两个数据结构。
    ( 1 ) hash , hash的作用就是关联元素value和权重score,保障元素value的唯—性,可以通过元素value找到相应的score值。
    (2)跳跃表,跳跃表的目的在于给元素value排序,根据score的范围获取元素列表。

3.6.4 跳跃表

  1. 简介·

    有序集合在生活中比较常见,例如根据成绩对学生排名,根据得分对玩家排名等。对于有序集合的底层实现,可以用数组、平衡树、链表等。数组不便元素的插入.删除;平衡树或红黑树虽然效率高但结构复杂;链表查询需要遍历所有效率低。Redis采用的是跳跃表。跳跃表效率堪比红黑树,实现远比红黑树简单。

  2. 实例
    对比有序链表和跳跃表,从链表中查询出51

    (1)有序链表

    #mermaid-svg-LwGX3TPNDqsZ1oaK .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .label text{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .node rect,#mermaid-svg-LwGX3TPNDqsZ1oaK .node circle,#mermaid-svg-LwGX3TPNDqsZ1oaK .node ellipse,#mermaid-svg-LwGX3TPNDqsZ1oaK .node polygon,#mermaid-svg-LwGX3TPNDqsZ1oaK .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-LwGX3TPNDqsZ1oaK .node .label{text-align:center;fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .node.clickable{cursor:pointer}#mermaid-svg-LwGX3TPNDqsZ1oaK .arrowheadPath{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-LwGX3TPNDqsZ1oaK .flowchart-link{stroke:#333;fill:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-LwGX3TPNDqsZ1oaK .edgeLabel rect{opacity:0.9}#mermaid-svg-LwGX3TPNDqsZ1oaK .edgeLabel span{color:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-LwGX3TPNDqsZ1oaK .cluster text{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-LwGX3TPNDqsZ1oaK .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-LwGX3TPNDqsZ1oaK text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .actor-line{stroke:grey}#mermaid-svg-LwGX3TPNDqsZ1oaK .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .sequenceNumber{fill:#fff}#mermaid-svg-LwGX3TPNDqsZ1oaK #sequencenumber{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK #crosshead path{fill:#333;stroke:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .messageText{fill:#333;stroke:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-LwGX3TPNDqsZ1oaK .labelText,#mermaid-svg-LwGX3TPNDqsZ1oaK .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .loopText,#mermaid-svg-LwGX3TPNDqsZ1oaK .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-LwGX3TPNDqsZ1oaK .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-LwGX3TPNDqsZ1oaK .noteText,#mermaid-svg-LwGX3TPNDqsZ1oaK .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-LwGX3TPNDqsZ1oaK .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-LwGX3TPNDqsZ1oaK .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-LwGX3TPNDqsZ1oaK .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .section{stroke:none;opacity:0.2}#mermaid-svg-LwGX3TPNDqsZ1oaK .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-LwGX3TPNDqsZ1oaK .section2{fill:#fff400}#mermaid-svg-LwGX3TPNDqsZ1oaK .section1,#mermaid-svg-LwGX3TPNDqsZ1oaK .section3{fill:#fff;opacity:0.2}#mermaid-svg-LwGX3TPNDqsZ1oaK .sectionTitle0{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .sectionTitle1{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .sectionTitle2{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .sectionTitle3{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-LwGX3TPNDqsZ1oaK .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .grid path{stroke-width:0}#mermaid-svg-LwGX3TPNDqsZ1oaK .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-LwGX3TPNDqsZ1oaK .task{stroke-width:2}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText:not([font-size]){font-size:11px}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-LwGX3TPNDqsZ1oaK .task.clickable{cursor:pointer}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText0,#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText1,#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText2,#mermaid-svg-LwGX3TPNDqsZ1oaK .taskText3{fill:#fff}#mermaid-svg-LwGX3TPNDqsZ1oaK .task0,#mermaid-svg-LwGX3TPNDqsZ1oaK .task1,#mermaid-svg-LwGX3TPNDqsZ1oaK .task2,#mermaid-svg-LwGX3TPNDqsZ1oaK .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutside0,#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutside2{fill:#000}#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutside1,#mermaid-svg-LwGX3TPNDqsZ1oaK .taskTextOutside3{fill:#000}#mermaid-svg-LwGX3TPNDqsZ1oaK .active0,#mermaid-svg-LwGX3TPNDqsZ1oaK .active1,#mermaid-svg-LwGX3TPNDqsZ1oaK .active2,#mermaid-svg-LwGX3TPNDqsZ1oaK .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-LwGX3TPNDqsZ1oaK .activeText0,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeText1,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeText2,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeText3{fill:#000 !important}#mermaid-svg-LwGX3TPNDqsZ1oaK .done0,#mermaid-svg-LwGX3TPNDqsZ1oaK .done1,#mermaid-svg-LwGX3TPNDqsZ1oaK .done2,#mermaid-svg-LwGX3TPNDqsZ1oaK .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-LwGX3TPNDqsZ1oaK .doneText0,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneText1,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneText2,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneText3{fill:#000 !important}#mermaid-svg-LwGX3TPNDqsZ1oaK .crit0,#mermaid-svg-LwGX3TPNDqsZ1oaK .crit1,#mermaid-svg-LwGX3TPNDqsZ1oaK .crit2,#mermaid-svg-LwGX3TPNDqsZ1oaK .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCrit0,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCrit1,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCrit2,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCrit0,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCrit1,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCrit2,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-LwGX3TPNDqsZ1oaK .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-LwGX3TPNDqsZ1oaK .milestoneText{font-style:italic}#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCritText0,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCritText1,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCritText2,#mermaid-svg-LwGX3TPNDqsZ1oaK .doneCritText3{fill:#000 !important}#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCritText0,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCritText1,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCritText2,#mermaid-svg-LwGX3TPNDqsZ1oaK .activeCritText3{fill:#000 !important}#mermaid-svg-LwGX3TPNDqsZ1oaK .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-LwGX3TPNDqsZ1oaK g.classGroup text .title{font-weight:bolder}#mermaid-svg-LwGX3TPNDqsZ1oaK g.clickable{cursor:pointer}#mermaid-svg-LwGX3TPNDqsZ1oaK g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-LwGX3TPNDqsZ1oaK g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-LwGX3TPNDqsZ1oaK .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-LwGX3TPNDqsZ1oaK .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .dashed-line{stroke-dasharray:3}#mermaid-svg-LwGX3TPNDqsZ1oaK #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK .commit-id,#mermaid-svg-LwGX3TPNDqsZ1oaK .commit-msg,#mermaid-svg-LwGX3TPNDqsZ1oaK .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-LwGX3TPNDqsZ1oaK g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-LwGX3TPNDqsZ1oaK g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-LwGX3TPNDqsZ1oaK g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-LwGX3TPNDqsZ1oaK .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-LwGX3TPNDqsZ1oaK .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-LwGX3TPNDqsZ1oaK .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-LwGX3TPNDqsZ1oaK .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-LwGX3TPNDqsZ1oaK .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-LwGX3TPNDqsZ1oaK .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-LwGX3TPNDqsZ1oaK .edgeLabel text{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-LwGX3TPNDqsZ1oaK .node circle.state-start{fill:black;stroke:black}#mermaid-svg-LwGX3TPNDqsZ1oaK .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-LwGX3TPNDqsZ1oaK #statediagram-barbEnd{fill:#9370db}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-state .divider{stroke:#9370db}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-LwGX3TPNDqsZ1oaK .note-edge{stroke-dasharray:5}#mermaid-svg-LwGX3TPNDqsZ1oaK .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-LwGX3TPNDqsZ1oaK .error-icon{fill:#522}#mermaid-svg-LwGX3TPNDqsZ1oaK .error-text{fill:#522;stroke:#522}#mermaid-svg-LwGX3TPNDqsZ1oaK .edge-thickness-normal{stroke-width:2px}#mermaid-svg-LwGX3TPNDqsZ1oaK .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-LwGX3TPNDqsZ1oaK .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-LwGX3TPNDqsZ1oaK .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-LwGX3TPNDqsZ1oaK .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-LwGX3TPNDqsZ1oaK .marker{fill:#333}#mermaid-svg-LwGX3TPNDqsZ1oaK .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-LwGX3TPNDqsZ1oaK {color: rgba(0, 0, 0, 0.75);font: ;}

    1
    11
    21
    31
    41
    51
    null

     要查找值为51的元素,需要从第一个元素开始依次查找、比较才能找到。共需要6次比较。
    

    (2)跳跃表

     从第⒉层开始,1节点比51节点小,向后比较。21节点比51节点小,继续向后比较,后面就是NULL了,所以从21节点向下到第1层·在第1层,41节点比51节点小,继续向后,61节点比51节点大,所以从41向下在第0层,51节点为要查找的节点,节点被找到,共查找4次。·
    

    · 从此可以看出跳跃表比有序链表效率更高

4. Redis配置文件介绍

4.1 Units 单位

配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit
大小写不敏感I

4.2 INCLUDES(引入配置文件)

引入其他的配置文件

4.3 NETWORK(网络)

4.3.1 bind

  • 默认情况bind=127.0.0.1只能接受本机的访问请求;
  • 不写的情况下,无限制接受任何ip 地址的访问
  • 生产环境肯定要写你应用服务器的地址﹔服务器是需要远程访问的,所以需要将其注释掉
  • 如果开启了protected-mode(保护模式),那么在没有设定bind ip且没有设密码的情况下,Redis只允许接受本机的响应

4.3.2 protected-mode(保护模式)

将本机访问保护模式设置为no

4.3.3 端口Port

默认 6379

4.3.4 tcp-backlog 511

  • 设置tcp的backlog ,backlog其实是一个连接队列,backlog队列总和=未完成三次握手队列+已经完成三次握手队列。
  • 在高并发环境下你需要一个高backlog(待办)值来避免慢客户端连接问题。
  • 注意Linux内核会将这个值减小到proc/sys/net/core/somaxconn的值 (128 ),所以需要确认增大/proc/sys/net/core/somaxconn和/proc/syshnet/ipv4/tcp_max_syn_backlog ( 128 )两个值来达到相要的效里

4.3.5 timeout 0 (过期时间)

默认值为0,(永不超时),以秒为单位

4.3.6 tcp-keepalive 300(心跳s)

每300秒检测一次链接是否使用

4.4 GENERAL 通用(一般通用)

4.4.1 daemonize(后台启动)

是否为后台进程。设置为 yes
守护进程,后台启动

4.4.2 pidfile(进程号)

存放pid文件的位置,每个实例会产生一个不同的pid文件

4.4.3 log(日志)

  1. loglevel 日志级别:
  2. logfile 日志位置
    默认为空,可以设置路径

4.4.4 dababases(数据源数量)

默认创建16个库,默认使用0号库

4.5 SECURITY(安全)

4.5.1 设置密码

  • 访问密码的查看、设置和取消
  • 在命令中设置密码,只是临时的。重启redis服务器,密码就还原了。
  • 永久设置,需要再配置文件中进行设置。

也可以使用命令设置密码:

config set requirepass “123456”

4.6 LIMITS (限制)

4.6.1 maxclients(最大连接数)

  • 设置redis同时可以与多少个客户端进行连接。
  • 默认情况下为10000个客户端。
  • 如果达到了此限制,redis则会拒绝新的连接请求,并且向这些连接请求方发出"max number of clients reached”以作回应。

4.6.2 maxmemeory(占用最大内存)

  • 建议必须设置,否则,将内存占满,造成服务器宕机
  • 设置redis可以使用的内存量。任-旦到达内存使用上限,redis将会试图移除内部数据,移除规则可以通过maxmemory-policy来指定。

4.6.3 maxmemory-policy(移除方式)

命令 解释
volatile-lru 使用LRU算法移除key,只对设置了过期时间的键;(最近最少使用)
allkeys-Iru: 在所有集合key中,使用LRU算法移除key
volatile-random :在过期集合中移除随机的key,只对设置了过期时间的键.
allkeys-random: 在所有集合key中,移除随机的key
volatile-ttl: 移除那些TTL值最小的key,即那些最近要过期的keywnoeviction :不进行移除。针对写操作,只是返回错误信息

4.6.4 maxmemory-samples

  • 设置样本数量ILRU算法和最小TTL算法都并非是精确的算法,而是估算值,所以你可以设置样本的大小,redis 默认会检查这么多个key并选择其中LRU的那个。
  • 一般设置3到7的数字,数值越小样本越不准确,但性能消耗越小。

5. Redis6 的发布和订阅

5.1 什么是发布和订阅?

  • Redis 发布订阅(pub/sub)是一种消息通信模式︰发送者(pub)发送消息,订阅者(sub)接收消息。
  • Redis客户端可以订阅任意数量的频道。

5.2 Redis的发布和订阅

  1. 客户端可以订阅频道如下图
#mermaid-svg-obW2v8eR3YaE9cGO .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-obW2v8eR3YaE9cGO .label text{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .node rect,#mermaid-svg-obW2v8eR3YaE9cGO .node circle,#mermaid-svg-obW2v8eR3YaE9cGO .node ellipse,#mermaid-svg-obW2v8eR3YaE9cGO .node polygon,#mermaid-svg-obW2v8eR3YaE9cGO .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-obW2v8eR3YaE9cGO .node .label{text-align:center;fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .node.clickable{cursor:pointer}#mermaid-svg-obW2v8eR3YaE9cGO .arrowheadPath{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-obW2v8eR3YaE9cGO .flowchart-link{stroke:#333;fill:none}#mermaid-svg-obW2v8eR3YaE9cGO .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-obW2v8eR3YaE9cGO .edgeLabel rect{opacity:0.9}#mermaid-svg-obW2v8eR3YaE9cGO .edgeLabel span{color:#333}#mermaid-svg-obW2v8eR3YaE9cGO .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-obW2v8eR3YaE9cGO .cluster text{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-obW2v8eR3YaE9cGO .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-obW2v8eR3YaE9cGO text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-obW2v8eR3YaE9cGO .actor-line{stroke:grey}#mermaid-svg-obW2v8eR3YaE9cGO .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-obW2v8eR3YaE9cGO .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-obW2v8eR3YaE9cGO #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-obW2v8eR3YaE9cGO .sequenceNumber{fill:#fff}#mermaid-svg-obW2v8eR3YaE9cGO #sequencenumber{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO #crosshead path{fill:#333;stroke:#333}#mermaid-svg-obW2v8eR3YaE9cGO .messageText{fill:#333;stroke:#333}#mermaid-svg-obW2v8eR3YaE9cGO .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-obW2v8eR3YaE9cGO .labelText,#mermaid-svg-obW2v8eR3YaE9cGO .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-obW2v8eR3YaE9cGO .loopText,#mermaid-svg-obW2v8eR3YaE9cGO .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-obW2v8eR3YaE9cGO .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-obW2v8eR3YaE9cGO .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-obW2v8eR3YaE9cGO .noteText,#mermaid-svg-obW2v8eR3YaE9cGO .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-obW2v8eR3YaE9cGO .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-obW2v8eR3YaE9cGO .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-obW2v8eR3YaE9cGO .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-obW2v8eR3YaE9cGO .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .section{stroke:none;opacity:0.2}#mermaid-svg-obW2v8eR3YaE9cGO .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-obW2v8eR3YaE9cGO .section2{fill:#fff400}#mermaid-svg-obW2v8eR3YaE9cGO .section1,#mermaid-svg-obW2v8eR3YaE9cGO .section3{fill:#fff;opacity:0.2}#mermaid-svg-obW2v8eR3YaE9cGO .sectionTitle0{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .sectionTitle1{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .sectionTitle2{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .sectionTitle3{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-obW2v8eR3YaE9cGO .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .grid path{stroke-width:0}#mermaid-svg-obW2v8eR3YaE9cGO .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-obW2v8eR3YaE9cGO .task{stroke-width:2}#mermaid-svg-obW2v8eR3YaE9cGO .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .taskText:not([font-size]){font-size:11px}#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-obW2v8eR3YaE9cGO .task.clickable{cursor:pointer}#mermaid-svg-obW2v8eR3YaE9cGO .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-obW2v8eR3YaE9cGO .taskText0,#mermaid-svg-obW2v8eR3YaE9cGO .taskText1,#mermaid-svg-obW2v8eR3YaE9cGO .taskText2,#mermaid-svg-obW2v8eR3YaE9cGO .taskText3{fill:#fff}#mermaid-svg-obW2v8eR3YaE9cGO .task0,#mermaid-svg-obW2v8eR3YaE9cGO .task1,#mermaid-svg-obW2v8eR3YaE9cGO .task2,#mermaid-svg-obW2v8eR3YaE9cGO .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutside0,#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutside2{fill:#000}#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutside1,#mermaid-svg-obW2v8eR3YaE9cGO .taskTextOutside3{fill:#000}#mermaid-svg-obW2v8eR3YaE9cGO .active0,#mermaid-svg-obW2v8eR3YaE9cGO .active1,#mermaid-svg-obW2v8eR3YaE9cGO .active2,#mermaid-svg-obW2v8eR3YaE9cGO .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-obW2v8eR3YaE9cGO .activeText0,#mermaid-svg-obW2v8eR3YaE9cGO .activeText1,#mermaid-svg-obW2v8eR3YaE9cGO .activeText2,#mermaid-svg-obW2v8eR3YaE9cGO .activeText3{fill:#000 !important}#mermaid-svg-obW2v8eR3YaE9cGO .done0,#mermaid-svg-obW2v8eR3YaE9cGO .done1,#mermaid-svg-obW2v8eR3YaE9cGO .done2,#mermaid-svg-obW2v8eR3YaE9cGO .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-obW2v8eR3YaE9cGO .doneText0,#mermaid-svg-obW2v8eR3YaE9cGO .doneText1,#mermaid-svg-obW2v8eR3YaE9cGO .doneText2,#mermaid-svg-obW2v8eR3YaE9cGO .doneText3{fill:#000 !important}#mermaid-svg-obW2v8eR3YaE9cGO .crit0,#mermaid-svg-obW2v8eR3YaE9cGO .crit1,#mermaid-svg-obW2v8eR3YaE9cGO .crit2,#mermaid-svg-obW2v8eR3YaE9cGO .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-obW2v8eR3YaE9cGO .activeCrit0,#mermaid-svg-obW2v8eR3YaE9cGO .activeCrit1,#mermaid-svg-obW2v8eR3YaE9cGO .activeCrit2,#mermaid-svg-obW2v8eR3YaE9cGO .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-obW2v8eR3YaE9cGO .doneCrit0,#mermaid-svg-obW2v8eR3YaE9cGO .doneCrit1,#mermaid-svg-obW2v8eR3YaE9cGO .doneCrit2,#mermaid-svg-obW2v8eR3YaE9cGO .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-obW2v8eR3YaE9cGO .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-obW2v8eR3YaE9cGO .milestoneText{font-style:italic}#mermaid-svg-obW2v8eR3YaE9cGO .doneCritText0,#mermaid-svg-obW2v8eR3YaE9cGO .doneCritText1,#mermaid-svg-obW2v8eR3YaE9cGO .doneCritText2,#mermaid-svg-obW2v8eR3YaE9cGO .doneCritText3{fill:#000 !important}#mermaid-svg-obW2v8eR3YaE9cGO .activeCritText0,#mermaid-svg-obW2v8eR3YaE9cGO .activeCritText1,#mermaid-svg-obW2v8eR3YaE9cGO .activeCritText2,#mermaid-svg-obW2v8eR3YaE9cGO .activeCritText3{fill:#000 !important}#mermaid-svg-obW2v8eR3YaE9cGO .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-obW2v8eR3YaE9cGO g.classGroup text .title{font-weight:bolder}#mermaid-svg-obW2v8eR3YaE9cGO g.clickable{cursor:pointer}#mermaid-svg-obW2v8eR3YaE9cGO g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-obW2v8eR3YaE9cGO g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-obW2v8eR3YaE9cGO .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-obW2v8eR3YaE9cGO .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-obW2v8eR3YaE9cGO .dashed-line{stroke-dasharray:3}#mermaid-svg-obW2v8eR3YaE9cGO #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO .commit-id,#mermaid-svg-obW2v8eR3YaE9cGO .commit-msg,#mermaid-svg-obW2v8eR3YaE9cGO .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-obW2v8eR3YaE9cGO g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-obW2v8eR3YaE9cGO g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-obW2v8eR3YaE9cGO g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-obW2v8eR3YaE9cGO .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-obW2v8eR3YaE9cGO .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-obW2v8eR3YaE9cGO .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-obW2v8eR3YaE9cGO .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-obW2v8eR3YaE9cGO .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-obW2v8eR3YaE9cGO .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-obW2v8eR3YaE9cGO .edgeLabel text{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-obW2v8eR3YaE9cGO .node circle.state-start{fill:black;stroke:black}#mermaid-svg-obW2v8eR3YaE9cGO .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-obW2v8eR3YaE9cGO #statediagram-barbEnd{fill:#9370db}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-state .divider{stroke:#9370db}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-obW2v8eR3YaE9cGO .note-edge{stroke-dasharray:5}#mermaid-svg-obW2v8eR3YaE9cGO .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-obW2v8eR3YaE9cGO .error-icon{fill:#522}#mermaid-svg-obW2v8eR3YaE9cGO .error-text{fill:#522;stroke:#522}#mermaid-svg-obW2v8eR3YaE9cGO .edge-thickness-normal{stroke-width:2px}#mermaid-svg-obW2v8eR3YaE9cGO .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-obW2v8eR3YaE9cGO .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-obW2v8eR3YaE9cGO .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-obW2v8eR3YaE9cGO .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-obW2v8eR3YaE9cGO .marker{fill:#333}#mermaid-svg-obW2v8eR3YaE9cGO .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-obW2v8eR3YaE9cGO {color: rgba(0, 0, 0, 0.75);font: ;}

订阅
订阅
订阅
客户端A,
channel1频道 ,,
客户端B.
客户端C,
  1. 当给这个频道发布消息后,消息就会发送给订阅的客户端
#mermaid-svg-wiFYjbbcrl9sLMIy .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .label text{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .node rect,#mermaid-svg-wiFYjbbcrl9sLMIy .node circle,#mermaid-svg-wiFYjbbcrl9sLMIy .node ellipse,#mermaid-svg-wiFYjbbcrl9sLMIy .node polygon,#mermaid-svg-wiFYjbbcrl9sLMIy .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-wiFYjbbcrl9sLMIy .node .label{text-align:center;fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .node.clickable{cursor:pointer}#mermaid-svg-wiFYjbbcrl9sLMIy .arrowheadPath{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-wiFYjbbcrl9sLMIy .flowchart-link{stroke:#333;fill:none}#mermaid-svg-wiFYjbbcrl9sLMIy .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-wiFYjbbcrl9sLMIy .edgeLabel rect{opacity:0.9}#mermaid-svg-wiFYjbbcrl9sLMIy .edgeLabel span{color:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-wiFYjbbcrl9sLMIy .cluster text{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-wiFYjbbcrl9sLMIy .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-wiFYjbbcrl9sLMIy text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-wiFYjbbcrl9sLMIy .actor-line{stroke:grey}#mermaid-svg-wiFYjbbcrl9sLMIy .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-wiFYjbbcrl9sLMIy #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .sequenceNumber{fill:#fff}#mermaid-svg-wiFYjbbcrl9sLMIy #sequencenumber{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy #crosshead path{fill:#333;stroke:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .messageText{fill:#333;stroke:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-wiFYjbbcrl9sLMIy .labelText,#mermaid-svg-wiFYjbbcrl9sLMIy .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-wiFYjbbcrl9sLMIy .loopText,#mermaid-svg-wiFYjbbcrl9sLMIy .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-wiFYjbbcrl9sLMIy .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-wiFYjbbcrl9sLMIy .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-wiFYjbbcrl9sLMIy .noteText,#mermaid-svg-wiFYjbbcrl9sLMIy .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-wiFYjbbcrl9sLMIy .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-wiFYjbbcrl9sLMIy .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-wiFYjbbcrl9sLMIy .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-wiFYjbbcrl9sLMIy .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .section{stroke:none;opacity:0.2}#mermaid-svg-wiFYjbbcrl9sLMIy .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-wiFYjbbcrl9sLMIy .section2{fill:#fff400}#mermaid-svg-wiFYjbbcrl9sLMIy .section1,#mermaid-svg-wiFYjbbcrl9sLMIy .section3{fill:#fff;opacity:0.2}#mermaid-svg-wiFYjbbcrl9sLMIy .sectionTitle0{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .sectionTitle1{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .sectionTitle2{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .sectionTitle3{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-wiFYjbbcrl9sLMIy .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .grid path{stroke-width:0}#mermaid-svg-wiFYjbbcrl9sLMIy .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-wiFYjbbcrl9sLMIy .task{stroke-width:2}#mermaid-svg-wiFYjbbcrl9sLMIy .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .taskText:not([font-size]){font-size:11px}#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-wiFYjbbcrl9sLMIy .task.clickable{cursor:pointer}#mermaid-svg-wiFYjbbcrl9sLMIy .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-wiFYjbbcrl9sLMIy .taskText0,#mermaid-svg-wiFYjbbcrl9sLMIy .taskText1,#mermaid-svg-wiFYjbbcrl9sLMIy .taskText2,#mermaid-svg-wiFYjbbcrl9sLMIy .taskText3{fill:#fff}#mermaid-svg-wiFYjbbcrl9sLMIy .task0,#mermaid-svg-wiFYjbbcrl9sLMIy .task1,#mermaid-svg-wiFYjbbcrl9sLMIy .task2,#mermaid-svg-wiFYjbbcrl9sLMIy .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutside0,#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutside2{fill:#000}#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutside1,#mermaid-svg-wiFYjbbcrl9sLMIy .taskTextOutside3{fill:#000}#mermaid-svg-wiFYjbbcrl9sLMIy .active0,#mermaid-svg-wiFYjbbcrl9sLMIy .active1,#mermaid-svg-wiFYjbbcrl9sLMIy .active2,#mermaid-svg-wiFYjbbcrl9sLMIy .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-wiFYjbbcrl9sLMIy .activeText0,#mermaid-svg-wiFYjbbcrl9sLMIy .activeText1,#mermaid-svg-wiFYjbbcrl9sLMIy .activeText2,#mermaid-svg-wiFYjbbcrl9sLMIy .activeText3{fill:#000 !important}#mermaid-svg-wiFYjbbcrl9sLMIy .done0,#mermaid-svg-wiFYjbbcrl9sLMIy .done1,#mermaid-svg-wiFYjbbcrl9sLMIy .done2,#mermaid-svg-wiFYjbbcrl9sLMIy .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-wiFYjbbcrl9sLMIy .doneText0,#mermaid-svg-wiFYjbbcrl9sLMIy .doneText1,#mermaid-svg-wiFYjbbcrl9sLMIy .doneText2,#mermaid-svg-wiFYjbbcrl9sLMIy .doneText3{fill:#000 !important}#mermaid-svg-wiFYjbbcrl9sLMIy .crit0,#mermaid-svg-wiFYjbbcrl9sLMIy .crit1,#mermaid-svg-wiFYjbbcrl9sLMIy .crit2,#mermaid-svg-wiFYjbbcrl9sLMIy .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-wiFYjbbcrl9sLMIy .activeCrit0,#mermaid-svg-wiFYjbbcrl9sLMIy .activeCrit1,#mermaid-svg-wiFYjbbcrl9sLMIy .activeCrit2,#mermaid-svg-wiFYjbbcrl9sLMIy .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-wiFYjbbcrl9sLMIy .doneCrit0,#mermaid-svg-wiFYjbbcrl9sLMIy .doneCrit1,#mermaid-svg-wiFYjbbcrl9sLMIy .doneCrit2,#mermaid-svg-wiFYjbbcrl9sLMIy .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-wiFYjbbcrl9sLMIy .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-wiFYjbbcrl9sLMIy .milestoneText{font-style:italic}#mermaid-svg-wiFYjbbcrl9sLMIy .doneCritText0,#mermaid-svg-wiFYjbbcrl9sLMIy .doneCritText1,#mermaid-svg-wiFYjbbcrl9sLMIy .doneCritText2,#mermaid-svg-wiFYjbbcrl9sLMIy .doneCritText3{fill:#000 !important}#mermaid-svg-wiFYjbbcrl9sLMIy .activeCritText0,#mermaid-svg-wiFYjbbcrl9sLMIy .activeCritText1,#mermaid-svg-wiFYjbbcrl9sLMIy .activeCritText2,#mermaid-svg-wiFYjbbcrl9sLMIy .activeCritText3{fill:#000 !important}#mermaid-svg-wiFYjbbcrl9sLMIy .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-wiFYjbbcrl9sLMIy g.classGroup text .title{font-weight:bolder}#mermaid-svg-wiFYjbbcrl9sLMIy g.clickable{cursor:pointer}#mermaid-svg-wiFYjbbcrl9sLMIy g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-wiFYjbbcrl9sLMIy g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-wiFYjbbcrl9sLMIy .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-wiFYjbbcrl9sLMIy .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-wiFYjbbcrl9sLMIy .dashed-line{stroke-dasharray:3}#mermaid-svg-wiFYjbbcrl9sLMIy #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy .commit-id,#mermaid-svg-wiFYjbbcrl9sLMIy .commit-msg,#mermaid-svg-wiFYjbbcrl9sLMIy .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-wiFYjbbcrl9sLMIy g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-wiFYjbbcrl9sLMIy g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-wiFYjbbcrl9sLMIy g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-wiFYjbbcrl9sLMIy .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-wiFYjbbcrl9sLMIy .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-wiFYjbbcrl9sLMIy .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-wiFYjbbcrl9sLMIy .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-wiFYjbbcrl9sLMIy .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-wiFYjbbcrl9sLMIy .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-wiFYjbbcrl9sLMIy .edgeLabel text{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-wiFYjbbcrl9sLMIy .node circle.state-start{fill:black;stroke:black}#mermaid-svg-wiFYjbbcrl9sLMIy .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-wiFYjbbcrl9sLMIy #statediagram-barbEnd{fill:#9370db}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-state .divider{stroke:#9370db}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-wiFYjbbcrl9sLMIy .note-edge{stroke-dasharray:5}#mermaid-svg-wiFYjbbcrl9sLMIy .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-wiFYjbbcrl9sLMIy .error-icon{fill:#522}#mermaid-svg-wiFYjbbcrl9sLMIy .error-text{fill:#522;stroke:#522}#mermaid-svg-wiFYjbbcrl9sLMIy .edge-thickness-normal{stroke-width:2px}#mermaid-svg-wiFYjbbcrl9sLMIy .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-wiFYjbbcrl9sLMIy .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-wiFYjbbcrl9sLMIy .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-wiFYjbbcrl9sLMIy .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-wiFYjbbcrl9sLMIy .marker{fill:#333}#mermaid-svg-wiFYjbbcrl9sLMIy .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-wiFYjbbcrl9sLMIy {color: rgba(0, 0, 0, 0.75);font: ;}

hello
hello
hello
PUBLISH channel1 hello ,,
channel1频道 ,,
客户端A,
客户端B,
客户端C,

5.3 发布和订阅命令行实现

  1. 打开一个客户端订阅channel1
    SUBSCRIBE channel1

  2. 打开另一个客户端,给channel1发布消息hello
    publish channel1 hello

    返回的1是订阅者的数量

  3. 打开第一个客户端可以看到发送的消息

6. Redis6 新数据类型

  1. Bitmaps
  2. HyperLogLog
  3. Geospatial

6.1 Bitmaps

6.1.1 简介

现代计算机用二进制(位)作为信息的基础单位,1个字节等于8位,例如“abc"字符串是由3个字节组成,但实际在计算机存储时将其用二进制表示,“abc"分别对应的 ASCII 码分别是97、98、I 99,对应的二进制分别是01100001、01100010和01100011,如下图。


合理地使用操作位能够有效地提高内存使用率和开发效率。

  • Redis提供了Bitmaps这个“数据类型”可以实现对位的操作∶
    (1) Bitmaps 本身不是一种数据类型,实际上它就是字符串( key-value ) ,但是它可以对字符串的位进行操作。
    ( 2) Bitmaps单独提供了一套命令,所以在Redis 中使用Bitmaps和使用字符串的方法不太相同。可以把 Bitmaps想象成一个以位为单位的数组,数组的每个单元只能存储0和1,数组的下标在Bitmaps 中叫做偏移量。

6.1.2 命令

  1. setbit
    (1). 格式:
    setbit[key][offset][value]设置Bitmaps中某个偏移量的值(0或1)

    127.0.0.1:6379> setbit //[key offset value]

    *offset:偏移量从0开始
    (2). 示例:

    • 每个独立用户是否访问过网站存放在Bitmaps 中,将访问的用户记做1,没有
      访问的用户记做0,用偏移量作为用户的id。
    • 设置键的第 offset个位的值(从О算起),假设现在有20个用户,userid=1,6, 11,15,19的用户对网站进行了访问,那么当前Bitmaps初始化结果如图

      unique:users:20201106代表2020-11-06这天的独立访问用户的 Bitmaps

    注∶

    • 很多应用的用户id以一个指定数字(例如10000 )开头,直接将用户id和Bitmaps的偏移量对应势必会造成一定的浪费,通常的做法是每次做setbit操作时将用户id减去这个指定数字。↓
    • 在第一次初始化 Bitmaps时,假如偏移量非常大,那么整个初始化过程执行会比较慢,可能会造成 Redis的阻塞。·
  2. getbit
    (1). 格式:
    getbit[key][offset]获取Bitmaps中某个偏移量的值

    127.0.0.1:6379> getbit key //[key offset ]

    *offset:偏移量从0开始
    (2)实例
    获取id=8的用户是否在2020-11-06这天访问过,返回0说明没有访问过:

    注: 因为超过19的都不存在,所以返回的也是0

  3. bitcount
    统计字符串被设置为1的 bit数。一般情况下,给定的整个字符串都会被进行计数,通过指定额外的 start或end 参数,可以让计数只在特定的位上进行。start和end参数的设置,都可以使用负数值:比如-1表示最后一个位,而-2表示倒数第二个位,start、end是指bit组的字节的下标数,二者皆包含。
    (1).格式
    bitcount《key》[start end] 统计字符串从start字节到end字节bit值为1的数量

    127.0.0.1:6379> bitcount //key[start end]

    (2)实例
    计算2022-11-06这天的独立访问用户数量

    127.0.0.1:6379> bitcount unique:users:20201106
    (integer) 5

    start和end代表起始和结束字节数,下面操作计算用户id在第1个字节到第3个字节之间的独立访问用户数,对应的用户id是11,15, 19。

    127.0.0.1:6379> bitcount unique:users:20201106 1 3
    (integer) 3

  4. bitop
    (1).格式
    bitop and(or/not/xor)<destkey,>[key…]

    127.0.0.1:6379> bitop //operation destkey key [key…]

    bitop是一个复合操作,它可以做多个Bitmaps的and (交集)、or (并集)、not(非) 、 xor (异或)操作并将结果保存在destkey中。
    (2)实例
    2020-11-04日访问网站的userid=1,2,5,9。

    setbit unique:users:20201104 1 1
    setbit unique:users:20201104 2 1
    setbit unique:users:20201104 5 1
    setbit unique:users:20201104 9 1

    2020-11-03日访问网站的userid=0,1,4,9。

    setbit unique:users:20201103 0 1
    setbit unique:users:20201103 1 1
    setbit unique:users:20201103 4 1
    setbit unique:users:20201103 9 1

    计算出两大都访问过网站的用户数量
    bitop and unique:users:and:20201104_03
    unique:users:20201103unique:users:20201104


    计算出任意一天都访问过网站的用户数量(例如月活跃就是类似这种),可以使用or 求并集,

6.1.3 Bitmaps 与 set 对比

假设网站有1亿用户,每天独立访问的用户有5千万,如果每天用集合类型和,Bitmaps分别存储活跃用户可以得到表

  • set 和 Bitmaps 存储一天活跃用户对比:
数据类型 每个id占用空间 需要存储用户量 全部内存量
集合类型 64位 50000000 64位*50000000 = 400MB
Bitmaps 1位 10000000 1位*100000000 = 12.5MB

很明显,这种情况下使用Bitmaps能节省很多的内存空间,尤其是随着时间推移节省的内存还是非常可观的

  • set 和 Bitmaps 存储独立用户空间对比:
数据类型 一天 一月 一年
集合类型 400MB 12GB 144GB
Bitmaps 12.5MB 375MB 4.5GB

但 Bitmaps并不是万金油,假如该网站每天的独立访问用户很少,例如只有10万(大量的僵尸用户),那么两者的对比如下表所示,很显然,这时候使用Bitmaps就不太合适了,因为基本上大部分位都是0。

  • set和 Bitmaps存储一天活跃用户对比(独立用户比较少)
数据类型 每个userid占用空间 需要存储的用户量 全部内存量
集合类型 64位 100000 64位*100000=800KB
Bitmaps 1位 100000000 1位*100000000 = 12.5MB

6.2 HyperLogLog

6.2.1 简介

  • 在工作当中,我们经常会遇到与统计相关的功能需求,比如统计网站PV( PageView页面访问量),可以使用Redis 的 incr、incrby 轻松实现。

  • 但像UV ( UniqueVisitor,独立访客)、独立IP数、搜索记录数等需要去重和计数的问题如何解决﹖这种求集合中不重复元素个数的问题称为基数问题。

  • 解决基数问题有很多种方案:
    ( 1 )数据存储在MySQL表中,使用distinct count计算不重复个数。
    ( 2 ) 使用Redis提供的 hash、set、bitmaps等数据结构来处理
    以上的方案结果精确,但随着数据不断增加,导致占用空间越来越大,对于非常大的数据集是不切实际的。

  • 能否能够降低一定的精度来平衡存储空间?
    Redis推出了HyperLogLoge

    1. Redis HyperLogLog是用来做基数统计的算法,HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定的、并且是很小的。
    2. 在Redis里面,每个HyperLogLog键只需要花费12 KB内存,就可以计算接
    3. 近2^64个不同元素的基数。这和计算基数时,几系越多种贺内1她未口鲜明对比。
    4. 但是,因为 HyperLogLog只会根据输入元素来计算基数,而不会储存输入元素本身,所以 HyperLogLog不能像集合那样,返回输入的各个元素。
  • 什么是基数?
    比如数据集{1,3,5,7,5,7,8},那么这个数据集的基数集为{(1,3,5 ,7,8},基数(不重复元素)为5。基数估计就是在误差可接受的范围内,快速计算基数。

6.2.2 命令

  1. pfadd 添加
    (1) 格式
    pfadd<key.><element.>[element …] 添加指定元素到HyperLogLog中:

    127.0.0.1:6379> pfadd //key element[element …]

    (2) 实例:

    将所有元素添加到指定 HyperLogLog数据结构中。如果执行命令后HLL估计的近似基数发生变化,则返回1,否则返回0。

2、pfcount 统计
( 1 ) 格式
pfcount<key.> [key …]计算HLL的近似基数,可以计算多个HLL,比如用HLL存储每天的UV,计算一周的UV可以使用7天的UV合并计算即可;

127.0.0.1:6379> pfcount //key [key …]

( 2 ) 实例:

3. pfmerge 合并
( 1 ) 格式
pfmergesdestkey> [sourcekey …]将一个或多个HLL合并后的结果存储在另一个HLL中,比如每月活跃用户可以使用每天的活跃用户来合并计算可得.

127.0.0.1:6379> pfmerge//distkey sourcekey[sourcekey …]

( 2 )实例

6.3 Geospatial

6.3.1 简介

Redis 3.2中增加了对GEO类型的支持。GEO,Geographic,地理信息的缩写。该类型,就是元素的2维坐标,在地图上就是经纬度。redis 基于该类型,提供了经纬度设置,查询,范围查询,距离查询,经纬度Hash 等常见操作。

6.3.2 命令

  1. gepadd
    (1) 格式
    geoadd<key.>< longitude><latitude.><member.> [longitude latitude member.]添加地理位置(经度,纬度,名称)·

    127.0.0.1:6379> geoadd //key longitude latitude member [longitude latitude member]

    (2) 格式
    geoadd china:city 121.47 31.23 shanghai
    geoadd china:city 106.50 29.53 chongqing 114.0522.52 shenzhen 116.38 39.9obeijing
    两极无法直接添加,一般会下载城市数据,直接通过Java程序一次性导入。
    有效的经度从-180度到180度。有效的纬度从-85.05112878度到85.05112878度。
    当坐标位置超出指定范围时,该命令将会返回一个错误。
    已经添加的数据,是无法再次往里面添加的。

  2. geopos
    (1) 格式
    geopos<key.><member.> [member …]获得指定地区的坐标值

    127.0.0.1:6379> geopos //key member [member …]

    ( 2 ) 实例

  3. geodist
    (1) 格式
    geodist<key.><member1.><member2.> [m|km|ft|mi] 获取两个位置之间的直线距离

    127.0.0.1:6379> geodist //key member1 member2 [m|km|ft|mi]

    (2) 实例

    单位:
    m表示单位为米[默认值]。
    km表示单位为千米。
    mi表示单位为英里。
    ft表示单位为英尺。
    如果用户没有显式地指定单位参数,那么GEODIST 默认使用米作为单位。

  4. georadius
    ( 1 ) 格式
    georadius<key.><longitude.><latitude.>radius m|km|ft|mi 以给定的经纬度为中心找出某一半径内的元素

    127.0.0.1:6379> georadius //key longitude latitude radius m|km|ft|mi

    经度 纬度 距离 单位
    ( 2 ) 实例

7. Jedis 操作 Redis6

jdbc 通过 java 操作数据库
jedis 通过 java 操作数据库

7.1 Jedis 所需jar包

版本请自行选择

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

7.2 Jedis 链接 Redis

import redis.clients.jedis.Jedis;
Jedis jedis = new Jedis("host","port");
//测试是否连接成功
String ping = jedis.ping();

执行结果:PONG为连接成功

PONG

Jedis 初始化有很多方法,具体请去看源码

7.3 链接Redis注意事项

链接失败…
链接失败…
链接失败…

  1. 解决掉linux 防火墙(有的不用关可以也可以链接)
  2. 请看上面目录 4.3.1 和 4.3.2

7.4 Key 的操作

注意:用完之后记得关闭 jedis

jedis.close();

包含但不仅限于这里展示的,很多方法重jedis写过了,具体使用看自己

7.4.1 Jedis-API:String

具体方法说明请看 目录:3.2

//遍历所有key
Set<String> keys = jedis.keys("*");
for (String key:keys) {System.out.println(key);
}
//key的数量
keys.size();
//key 是否存在
jedis.exists("key");
//写入redis
jedis.set("key","value");
//设置多个key-value
jedis.mset("k1","v1","k2","v2","k3","v3"....);
//获取key的值
jedis.get("key");
//获取多个key的值
jedis.mget("k1","k2","k3"....);
//看过期时间 -1永不过期, -2 已经过期/不存在
jedis.ttl("key");

7.4.2 Jedis-API:List

具体方法说明请看 目录:3.3

Long lpush = jedis.lpush("list1", "v1", "v2", "v3");
System.out.println(lpush);
List<String> list1 = jedis.lrange("list1", 0, -1);
System.out.println(list1);
jedis.lrem("list1",1,"v1");
List<String> list2 = jedis.lrange("list1", 0, -1);
System.out.println(list2);
jedis.lpop("list1",2);
List<String> list3 = jedis.lrange("list1", 0, -1);
System.out.println(list3);

7.4.3 Jedis-API: set

具体方法说明请看 目录:3.4

jedis.sadd("set1","lucy","jack");
Set<String> name = jedis.smembers("set1");
System.out.println(name);
Long srem = jedis.srem("set1", "lucy");
System.out.println(srem);
Set<String> name = jedis.smembers("set1");
System.out.println(name);

7.4.4 Jedis-API: hash

具体方法说明请看 目录:3.5

jedis.hset("users","id","1");
jedis.hset("users","age","20");
String hget = jedis.hget("users", "id");
System.out.println(hget);

7.4.5 Jedis-API: zset

具体方法说明请看 目录:3.6

Long zadd = jedis.zadd("school", 10d, "北大");
System.out.println(zadd);
Set<String> zrange = jedis.zrange(String.valueOf("school"), 0, -1);
System.out.println(zrange);
Long school = jedis.zcount("school", 0, 1000);
System.out.println(school);

7.5 Jedis实例: 手机验证码

只写java代码模拟,不写页面,不写真实发送信息的代码

要求:

  1. 输入手机号,点击发送后随机生成6位数字码,2分钟有效,
  2. 输入验证码,点击验证,返回成功或失败
  3. 每个手机号每天只能输入3次

思路:

  1. 生成随机6位验证码
    ** Random
  2. 验证码两分钟有效
    ** 验证码放到redis中, 设置过期时间120秒
  3. 判断验证码是否一致
    ** 从redis中获取和输入的验证码进行比较
  4. 每个手机每天只能发送3次验证码
    ** incr 每次发送后+1
    ** 大于2时,提交不能发送
 //1, 生成6位验证码public static String getCode(){Random random = new Random();StringJoiner code = new StringJoiner("");for (int i = 0; i < 6; i++) {int rand = random.nextInt(10);code.add(String.valueOf(rand));};return code.toString();}//2, 每个手机每天只能发送三次,验证码放到Redis中,设置过期时间120秒public static void verifyCode(String phone){//链接redisJedis jedis = new Jedis("192.168.230.138",6379);//拼接key//手机发送次数key,保证key是唯一的String countKey = "VerifyCode"+phone+":count";//验证码keyString codeKey = "VerifyCode"+phone+":code";//生成验证码String vcode = getCode();//每个手机每天只能发送三次String count = jedis.get(countKey);if(count == null){//没有发送次数,第一次发送,设置发送次数为一jedis.setex(countKey,24*60*60L,"1");}else if(Integer.parseInt(count)<=2){//发送次数加1jedis.incr(countKey);}else if(Integer.parseInt(count)>2){//发送三次,不能再发送System.out.println("今天的发送次数已经超过三次");jedis.close();}//发送的验证码放到Redis中jedis.setex(codeKey,120L,vcode);jedis.close();}//3, 验证码校验public static Boolean getRedisCode(String phone,String code){//从Redis中获取验证码Jedis jedis = new Jedis("192.168.230.138",6379);String codeKey = "VerifyCode"+phone+":code";String redisCode = jedis.get(codeKey);//判断if(redisCode.equals(code)){System.out.println("成功");jedis.close();return true;}else{System.out.println("失败");jedis.close();return false;}}

验证:

@Test
public void test(){String phone = "13611111121";//模拟验证码的发送,verifyCode(phone);Jedis jedis = new Jedis("192.168.230.138",6379);String codeKey = "VerifyCode"+phone+":code";String code = jedis.get(codeKey);System.out.println(code);getRedisCode(phone,code);jedis.close();
}

执行结果:

938712
成功

8. Redis6 与SpringBoot整合

  1. 在pom.xml中添加引入相关依赖

    <!--   redis     -->
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.4.5</version>
    </dependency>
    <!--    spring2.x集成redis所需common-pool2 连接池   -->
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.8.0</version>
    </dependency>
    
  2. application.properties 配置 redis

    #Redis服务器地址
    spring.redis.host=192.168.230.138
    #Redis服务器连接端口
    spring.redis.port=6379
    #Redis数据库索引(默认为0)
    spring.redis.database= 0
    # 连接超时时间(毫秒)
    spring.redis.timeout=1800000
    #连接池最大连接数(使用负值表示没有限制)
    spring.redis.lettuce.pool.max-active=20
    #最大阻塞等待时间(负数表示没限制)
    spring.redis.lettuce.pool.max-wait=-1
    #连接池中的最大空闲连接
    spring.redis.lettuce.pool.mxn-idle=5
    #连接池中的最小空闲连接
    spring.redis.lettuce.pool.min-idle=0
    
  3. 添加配置类

    package redis6.redis.conf;import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@EnableCaching
    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){RedisTemplate<String,Object> template = new RedisTemplate<>() ;RedisSerializer<String> redisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper() ;om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect. Visibility.ANY);//enableDefaultTyping是过期的,可以用activateDefaultTyping//om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory) ;//key 列化方式template.setKeySerializer(redisSerializer) ;//value hashmap序列化template.setHashValueSerializer(jackson2JsonRedisSerializer);return template;}@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory){RedisSerializer<String> redisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);//解决查询缓存转换异常问题ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(om);//配置序列化,解决乱码问题,过期时间600秒RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(600)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).disableCachingNullValues();RedisCacheManager cacheManager = RedisCacheManager.builder(factory).cacheDefaults(config).build();return cacheManager;}
    }
    

    没有包的导一下maven依赖:

    <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.11.4</version>
    </dependency>
    <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.3.5</version>
    </dependency>
    

    编写controller测试一下

    package redis6.redis.Controller;import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;@RestController
    @RequestMapping("/redisTest")
    public class TestController {@Autowiredprivate RedisTemplate redisTemplate;@GetMapping()public String testRedis(){//设置值到redis中redisTemplate.opsForValue().set("name","luck");//获取值Object name = redisTemplate.opsForValue().get("name");return String.valueOf(name);}
    }
    

    由于没有设置端口和项目名,所以直接访问即可:

    如果进不来,显示

    看一下pom.xml是否是一个web项目

    <dependency><groupId>org.springframework.boot</groupId><!--这里加上-web--><artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

9. Redis6 的事务操作(并发,连接池)

  • Redis的事务定义
  • Multi,Exec,discard
  • 事务的错误处理
  • 事务的冲突问题
  • Redis事务三特性
  • Redis事务秒杀案例

9.1 Redis的事务定义

  • Redis事务是一个单独的隔离操作∶事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。
  • Redis事务的主要作用就是串联多个命令防止别的命令插队。

9.2 Multi,Exec,discard

multi — 开启
exec — 结束
discard — 回滚

  • 从输入Multi命令开始,输入的命令都会依次进入命令队列中,但不会执行,直到输入Exec后,Redis 会将之前的命令队列中的命令依次执行。
  • 组队的过程中可以通过discard来放弃组队。

    开启事务,组队模式,关闭执行或关闭事务

9.3 事务的错误处理

组队中某个命令出现了报告错误,执行时整个队列都会被取消

如果执行阶段某个命令报出了错误,则只有报错的命令不会被执行,而其他的命令都会执行,不会回滚。

9.4 事务的冲突问题

9.4.1 例子

想象一下:有很多人有你的账户同时参加双十一抢购…

  • 一个请求想给金额减8000
  • 一个请求想给金额减5000
  • 一个请求想给金额减1000

资本家亏了!!!
银行看了都流泪!!!

9.4.2 悲观锁


悲观锁(Pessimistic Lock),顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁。传统的关系型数据库里边就用到了很多这种锁机制,比如行锁,表锁等,读锁,写锁等,都是在做操作之前先上锁。

9.4.3 乐观锁


乐观锁(Optimistic Lock) ,顾名思义,就是很乐观,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号等机制。乐观锁适用于多读的应用类型,这样可以提高吞吐量。Redis 就是利用这种check-and-set机制实现事务的。

9.5 WATCH key [key…] (监视)

在执行multi之前,先执行watch key1 [key2],可以监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务将被打断。

  1. 打开第一个命令窗口:,先执行第一个命令窗口,监视age先不提交
  2. 打开第二个命令窗口, age+20;
  3. 第一个窗口,提交:

    发现提交结果为(nil),类似java的null,反正就是失败了
    他其中有乐观锁的机制

9.6 unwatch (取消监视)

  • 取消WATCH命令对所有key 的监视。
  • 如果在执行WATCH命令之后,EXEC命令或DISCARD命令先被执行了的话,那么就不需要再执行UNWATCH 了。

Transaction(事务)解释:
http://doc.redisfans.com/transaction/index.html

9.7 Redis 事务三特性

  • 单独的隔离操作﹐
    事务中的所有命令都会序列化、按顺序地执行。事务在执行的过程中,不会被其他客户端发送来的命令请求所打断。

  • 没有隔离级别的概念·
    队列中的命令没有提交之前都不会实际被执行,因为事务提交前任何指令都不会被实际执行,

  • 不保证原子性
    事务中如果有一条命令执行失败,其后的命令仍然会被执行,没有回滚

9.8 Redis 事务事务秒杀案例

9.8.1 解决计数器和人员记录的事务操作

9.8.2 Redis 秒杀案例 — 1.简单版

思路步骤:

  1. uid和prodid非空判断
  2. 链接redis
  3. 拼接key
    库存key
    秒杀成功用户key
  4. 获取库存,如果库存null,秒杀还没开始
  5. 判断用户是否重复秒杀操作
  6. 判断如果商品数量,库存数量<1,秒杀结束
  7. 秒杀过程
    库存-1
    秒杀成功用户添加清单中

代码:

数据准备,初始化:

    String redisHost = "192.168.230.138";Integer redisPort = 6379;String userid = "123456";private String[] userids = {"111111","222222","333333","444444","555555","666666","777777","888888","999999"};String prodid = "111";//设置库存@Testpublic void editkc(){Jedis jedis = new Jedis(redisHost,redisPort);jedis.set("sk:"+prodid+":qt","10");//清空数据库//jedis.flushDB();String s = jedis.get("sk:" + prodid + ":qt");System.out.println(s);jedis.close();}
public boolean secKill(String uid,String prodid) {//1.uid和prodid非空判断if(uid==null||prodid==null){return false;}//2. 链接redisJedis jedis = new Jedis(redisHost, redisPort);if(!getRedisIsOk(jedis)){return false;}//3. 拼接key//3.1 库存keyString kcKey = "sk:"+prodid+":qt";//3.2 秒杀成功用户keyString user= "sk:"+prodid+":user";//4. 获取库存,如果库存null,秒杀还没开始String kc = jedis.get(kcKey);if(kc==null){System.out.println(uid+"秒杀还未开始");jedis.close();return false;}//5. 判断用户是否重复秒杀操作if(jedis.sismember(userKey,uid)){System.out.println(uid+"你已经抢到过了");jedis.close();return false;}//6. 判断如果商品数量,库存数量<1,秒杀结束if(Integer.parseInt(kc)<=0){System.out.println(uid+"商品已经被抢光了");jedis.close();return false;}//7. 秒杀过程//7.1 库存-1jedis.decr(kcKey);//7.2 秒杀成功用户添加清单中jedis.sadd(userKey,uid);System.out.println(uid+"秒杀成功了");jedis.close();return true;}

检查redis是否存活

    /*** 检查redis是否存活* @param jedis  jedis对象* @return boolean*/public static Boolean getRedisIsOk(Jedis jedis) {boolean result = false;try {//连接本地Redis服务String ping = jedis.ping();//jedis.auth(password);//密码if (ping.equalsIgnoreCase("PONG")) {//System.out.println("redis缓存有效!" + ping);result = true;}} catch (Exception e) {System.out.println("redis缓存失败!");result = false;}return result;}

编写测试:

    //模拟请求@Testpublic void addsk(){for (int i = 0; i < userids.length; i++) {secKill(userids[i],prodid);}}

执行结果:没有并发:

000000秒杀成功了
111111你已经抢到过了
222222你已经抢到过了
333333你已经抢到过了
444444你已经抢到过了
555555你已经抢到过了
666666你已经抢到过了
777777你已经抢到过了
888888你已经抢到过了
999999你已经抢到过了
10101010商品已经被抢光了
123123商品已经被抢光了

9.9 Redis 事务–秒杀并发模拟

编写:Controller

@RestController
@RequestMapping("/redisTest")
public class TestController {@Resourceprivate SeckillService seckillService;@RequestMapping("doseckill")public String seckill(String uid, String prodid) {//uid随机生成uid = new Random().nextInt(50000)+"";seckillService.secKill(uid, prodid);return uid+" --- "+prodid;}
}

9.9.1 ab (linux测试工具)

  • 使用工具 ab 模拟测试

    centOS6 默认安装
    cendOS7 需手动安装

    1. 联网:
      centOS等系统执行:

      yum install httpd-tools

      ubuntu等系统执行:

      apt install apache2-utils

      查看是否安装成功:

      ab --help

      命令格式:

       Usage: ab [options.] [http[s]://]hostname[:port]/path
      
    2. 无网络:

      ( 1)进入cd /run/media/root/CentOS 7 x86_64/Packages (路径跟centos6不同)
      ( 2)顺序安装:

      apr-1.4.8.3.el7.x86.64.rpe
      apr-m.util-1.5.2-6.e17.x86_64rpm
      httpd-tools-2.4.6-67.e17.centos.x8664.rpm

  • 测试及结果:

    1. 通过浏览器测试:

      ab -n 1000 -c 100 http://192.168.230.138:8080/sckill

    2. 在通过ab测试
      模拟表单提交参数,以&符号结尾;存放当前目录

      vim postfile

      如: 内容: prodid=0101&

      执行

      ab -n 1000 -c 100 -p postfile -T application/x-www-form-urlencoded http://192.168.0.177:8080/redisTest/doseckill

      解释:
      ab -n 请求数量 -c 并发数量 -p 提交参数(需要创建文件) -T 类型
      x-www-form-urlencoded http://192.168.0.177/seckill/doseckill

    3. 结果:
      一个1000个请求,每次并发请求100个:
      结果就不给看了因为太多了,占地,看一下库存还剩多少吧:

      -90…这…资本家流泪,程序猿的锅

  • 请求过多,并发过多可能会出现连接超时问题

**

现在有两个问题,一个是超卖,一个是连接超时问题

**

9.9.1 Redis 秒杀案例 — 2. 连接池解决连接超时

编写连接池帮助类:

package com.xyy.redis6.redis.util;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class JedisPoolUtil {private static volatile JedisPool jedisPool= null;private static String redisHost = "192.168.230.138";private static Integer redisPort = 6379;private static Integer timeout = 6000;private  JedisPoolUtil(){}//获取连接池public static  JedisPool getJedisPoolInstance(){if(null==jedisPool){synchronized (JedisPoolUtil.class){if (null==jedisPool){JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(200);poolConfig.setMaxIdle(32);poolConfig.setMaxWaitMillis(100*1000);poolConfig.setTestOnBorrow(true); //ping PONGjedisPool = new JedisPool(poolConfig,redisHost,redisPort,timeout);}}}return jedisPool;}
//释放资源public static void release(JedisPool jedisPool, Jedis jedis){if (jedis != null) {jedisPool.returnResource(jedis);}}
}

修改抢购代码中第二步,获取jedis对象:

//2. 链接redis
//Jedis jedis = new Jedis(redisHost, redisPort);
//通过连接池得到jedis对象
JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();
Jedis jedis = jedisPoolInstance.getResource();

9.9.2 Redis 秒杀案例 — 3. 加事务解决商品超卖

可以返回去看9.4.1(例子)

9.9.3 乐观锁解决超卖

加个版本号…

监视库存(watch)(可查看 9.5 监视):

修改抢购代码中第七步,秒杀过程:

        //7. 秒杀过程//开启事务Transaction multi = jedis.multi();//组队操作multi.decr(kcKey);multi.sadd(userKey,uid);//执行List<Object> results = multi.exec();if (results == null || results.size()==0) {System.out.println(uid + "秒杀失败了");jedis.close();return false;}//7.1 库存-1//jedis.decr(kcKey);//7.2 秒杀成功用户添加清单中//jedis.sadd(userKey, uid);

执行结果:

嘟嘟嘟嘟~ 没有超卖yes!!!

9.10 Redis 秒杀案例 — 4. 解决库存依赖问题,LUA脚本

  • 将库存设置为500,
  • ab 模拟2000请求,并发300

执行后库存遗留::449(不一定)
乐观锁而造成!!!

2000人同时购买(版本假设:1.0),第一个人买到之后库存 -1(版本号更新为 1.1) ,版本号被修改,其余1999人全部版本号(1.0)不对,不执行。。。太极端了。。。大概就这么个意思

  • Lua是一个小巧的脚本语言,Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C++的函数,Lua并没有提供强大的库,一个完整的Lua解释器不过200k ,所以Lua不适合作为开发独立应用程序的语言,而是作为嵌入式脚本语言。

  • 很多应用程序、游戏使用LUA作为自己的嵌入式脚本语言,以此来实现可配置性、可扩展性。

  • 这其中包括魔兽争霸地图、魔兽世界、博德之门、愤怒的小鸟等众多游戏插件或外挂.

w3cschool 教程地址:https://www.w3cschool.cn/lua/

9.10.1 LUA 在 Redis 的优势

  1. 将复杂的或者多步的redis操作,写为一个脚本,一次提交给redis执行,减少反复连接redis 的次数。提升性能
  2. LUA脚本是类似redis事务,有一定的原子性,不会被其他命令插队,可以完成一些redis事务性的操作。
  3. 但是注意redis的lua脚本功能,只有在Redis 2.6以上的版本才可以使用。(本文章使用 Redis 6)
  4. 利用lua脚本淘汰用户,解决超卖问题。
  5. redis 2.6版本以后,通过lua脚本解决争抢问题,实际上是redis 利用其单线程的特性,用任务队列的方式解决多任务并发问题。

9.10.2 LUA 脚本 优化库存问题

开发者自行约定返回内容

local userid=KEYS[1];
local prodid=KEYS[2];
local qtKey=“sk:”…prodid…":qt";
local usersKey=“sk:”…prodid…":usr";
local userExists=redis.call(“sismember”,usersKey,userid);
if tonumber(userExists)==1 then
return 2;
end
local num = redis.call(“get”,qtKey);
if tonumber(num)<=0 then
return 0;
else
redis.call(“decr”,qtKey);
redis.call(“sadd”,usersKey,userid);
end
return 1;

创建java文件:Seckill_RedisByScript

package com.xyy.redis6.redis.Service.impl;import com.xyy.redis6.redis.util.JedisPoolUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;public class Seckill_RedisByScript {static  String secKillScript="local userid=KEYS[1];\n" +"local prodid=KEYS[2];\n" +"local qtKey=\"sk:\"..prodid..\":qt\";\n" +"local usersKey=\"sk:\"..prodid..\":usr\";\n" +"local userExists=redis.call(\"sismember\",usersKey,userid);\n" +"if tonumber(userExists)==1 then\n" +"\treturn 2;\n" +"end\n" +"local num = redis.call(\"get\",qtKey);\n" +"if tonumber(num)<=0 then\n" +"\treturn 0;\n" +"else\n" +"\tredis.call(\"decr\",qtKey);\n" +"\tredis.call(\"sadd\",usersKey,userid);\n" +"end\n" +"\treturn 1;";public static boolean doSecKill(String userid ,String prodid){JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();Jedis jedis = jedisPoolInstance.getResource();//scriptLoad()加载LUA脚本字符串String sha1 = jedis.scriptLoad(secKillScript);//获取执行结果    //参数为:脚本字符串,keyCount键数量,String... params 可变参数,//可以看源码,就知道了Object result = jedis.evalsha(sha1, 2, userid, prodid);String reString = String.valueOf(result);if ("0".equals(reString)) {System.err.println(userid+"以抢空");}else if ("1".equals(reString)){System.out.println(userid+"抢购成功...");}else if ("2".equals(reString)){System.err.println(userid+"你已经抢过");}else{System.err.println(userid+"抢购异常");}jedis.close();return true;}
}

将controller中的调用修改一下

    @RequestMapping("doseckill")public String seckill(String uid, String prodid) {uid = new Random().nextInt(50000)+"";//seckillService.secKill(uid, prodid);Seckill_RedisByScript.doSecKill(uid, prodid) ;return uid+" --- "+prodid;}

先执行 设置库存方法:editkc()
在linux中再次执行 ab 命令:(解释请看:9.9.1)
2000人,每次300并发模拟:

ab -n 2000 -c 300 -p postfile -T application/x-www-form-urlencoded http://192.168.0.177:8080/redisTest/doseckill

可以看到控制台中前500名强到了,重复的二次抢购将被直接返回

----
库存为0,并发保护

10. Redis6 持久化之RDB(数据文件)

10.1 总体介绍

官网介绍:http://www.redis.io/

Redis 持久化

  • Redis provides a different range of persistence options:
    Redis提供了一系列不同的持久性选项:

  • The RDB persistence performs point-in-time snapshots of your dataset at specified intervals.
    RDB持久性以指定的时间间隔执行数据集的时间点快照。

  • The AOF persistence logs every write operation received by the server, that will be played again at server startup,reconstructing the original dataset.Commands are logged using the same format as the Redis protocol itself, in anappend-only fashion.Redis is able to rewrite the log in the background when it gets too big.
    AOF持久化记录服务器接收到的每个写操作,这些操作将在服务器启动时再次播放,重建原始数据集。命令记录使用与Redis协议本身相同的格式,以仅附加的方式。当日志变得太大时,Redis能够在后台重写日志。

  • lf you wish, you can disable persistence completek if you want your data to just exist as long as the server isrunning.
    如果您愿意,您可以禁用持久性完成,如果您希望您的数据只是存在,只要服务器正在运行。

  • lt is possible to combine both AOF and RDB in the same instance. Notice that, in this case, when Redis restarts theAOF file will be used to reconstruct the original dataset since it is guaranteed to be the most complete.
    可以将AOF和RDB合并在同一个实例中。注意,在这种情况下,当Redis重新启动时,AOF文件将被用于重构原始数据集,因为它保证是最完整的。

要理解的最重要的事情是RDB和AOF持久性之间的不同权衡。让我们从RDB开始:

Redis 提供了两个不同形式的持久化方式

  • RDB(Redis DataBase)直接写文件
  • AOF(Append Of File) 追加方式

10.2 RDB是什么?

在指定的时间间隔内将内存:的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里

10.3 备份是如何执行的

Redis 会单独创建( fork )一个子进程来进行持久化,会先将数据写入到一个临时文件中,待持久化过程都结束了,再用这个临时文件替换上次持久化好的文件。整个过程中,主进程是不进行任何IO操作的,这就确保了极高的性能如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那RDB方式要比AOF方式更加的高效。RDB的缺点是最后一次持久化后的数据可能丢失.

10.4 fork 子进程

  • Fork的作用是复制一个与当前进程一样的进程。新进程的所有数据(变量、环境变量、程序计数器等)数值都和原进程一致,但是是一个全新的进程,并作为原进程的子进程,

  • 在Linux程序中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考,Linux中引入了“写时复制技术”

  • 一般情况父进程和子进程会共用同一段物理内存,只有进程空间的各段的内容要发生变化时,才会将父进程的内容复制一份给子进程。

10.5 RDB持久化流程

10.6 dump.rdb文件

在redis.conf中配置文件名称,默认为dump.rdb

在那个目录下启动的redis就在那个目录下生成rdb文件:

10.6.1 dump.rdb文件配置位置

rdb文件的保存路径,也可以修改。默认为Redis启动时命令行所在的目录下

dri /home/xyy/redisrdb

修改文件名字:

dbfilename dumpDB.rdb

10.6.2 持久化间隔设置


Unless specified otherwise,by default Redis will save the DB:
除非另有规定,默认情况下Redis将保存DB:

  • After 3600 seconds(an hour) if at least 1 key changed
    3600秒(一小时)如果至少1键改变
  • After 300 seconds (5 minutes) if at least 100 keys changed
    300秒内(5分钟)如果至少100个键改变
  • After 60seconds if at least 10000 keys changed
    60秒内如果至少10000键改变

10.6.3 save VS bgsava

  • save : save时只管保存,其它不管,全部阻塞。手动保存。不建议。
  • bgsave : Redis 会在后台异步进行快照操作,快照同时还可以响应客户端请求。

    ** 可以通过LastSave命令获取最后一次成功执行快照的时间.

10.6.4 flushall命令

执行flushall命令,也会产生dump.rdb文件,但里面是空的,无意义

10.6.5 stop-writes-on-bgsave-error

当Redis无法写入磁盘的话,直接关掉Redis的写操作。推荐yes

10.6.4 rdbcompression 压缩文件

  • 对于存储到磁盘中的快照,可以设置是否进行压缩存储。如果是的话,redis会采用LZF算法进行压缩。
  • 如果你不想消耗CPU来进行压缩的话,可以设置为关闭此功能。推荐yes.

10.6.5 rdbchecksum 检查完整性

  • 在存储快照后,还可以让redis使用CRC64算法来进行数据校验,
  • 但是这样做会增加大约10%的性能消耗,如果希望获取到最大的性能提升,可以关闭此功能
  • 推荐yes

10.6.6 rdb的备份

在redis运行过程中,备份dump.rdb文件

cp dump.rdb dump.rdb.bak

如果redis忽然关闭,部分数据丢失,或者原来的dump.rdb被恶意删除

关闭redis,将dump.rdb.bak改回来,

cp dump.rdb.bak dump.rdb

再次启动redis,所有已经持久化的数据都将恢复

10.7 优势

  • 适合大规模的数据恢复
  • 对数据完整性和一致性要求不高更适合使用
  • 节省磁盘空间
  • 恢复速度快
#mermaid-svg-tKjqynBtV9PTEQa1 .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .label text{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .node rect,#mermaid-svg-tKjqynBtV9PTEQa1 .node circle,#mermaid-svg-tKjqynBtV9PTEQa1 .node ellipse,#mermaid-svg-tKjqynBtV9PTEQa1 .node polygon,#mermaid-svg-tKjqynBtV9PTEQa1 .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-tKjqynBtV9PTEQa1 .node .label{text-align:center;fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .node.clickable{cursor:pointer}#mermaid-svg-tKjqynBtV9PTEQa1 .arrowheadPath{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-tKjqynBtV9PTEQa1 .flowchart-link{stroke:#333;fill:none}#mermaid-svg-tKjqynBtV9PTEQa1 .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-tKjqynBtV9PTEQa1 .edgeLabel rect{opacity:0.9}#mermaid-svg-tKjqynBtV9PTEQa1 .edgeLabel span{color:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-tKjqynBtV9PTEQa1 .cluster text{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-tKjqynBtV9PTEQa1 .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-tKjqynBtV9PTEQa1 text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-tKjqynBtV9PTEQa1 .actor-line{stroke:grey}#mermaid-svg-tKjqynBtV9PTEQa1 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-tKjqynBtV9PTEQa1 #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .sequenceNumber{fill:#fff}#mermaid-svg-tKjqynBtV9PTEQa1 #sequencenumber{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 #crosshead path{fill:#333;stroke:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .messageText{fill:#333;stroke:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-tKjqynBtV9PTEQa1 .labelText,#mermaid-svg-tKjqynBtV9PTEQa1 .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-tKjqynBtV9PTEQa1 .loopText,#mermaid-svg-tKjqynBtV9PTEQa1 .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-tKjqynBtV9PTEQa1 .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-tKjqynBtV9PTEQa1 .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-tKjqynBtV9PTEQa1 .noteText,#mermaid-svg-tKjqynBtV9PTEQa1 .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-tKjqynBtV9PTEQa1 .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-tKjqynBtV9PTEQa1 .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-tKjqynBtV9PTEQa1 .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-tKjqynBtV9PTEQa1 .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .section{stroke:none;opacity:0.2}#mermaid-svg-tKjqynBtV9PTEQa1 .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-tKjqynBtV9PTEQa1 .section2{fill:#fff400}#mermaid-svg-tKjqynBtV9PTEQa1 .section1,#mermaid-svg-tKjqynBtV9PTEQa1 .section3{fill:#fff;opacity:0.2}#mermaid-svg-tKjqynBtV9PTEQa1 .sectionTitle0{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .sectionTitle1{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .sectionTitle2{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .sectionTitle3{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-tKjqynBtV9PTEQa1 .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .grid path{stroke-width:0}#mermaid-svg-tKjqynBtV9PTEQa1 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-tKjqynBtV9PTEQa1 .task{stroke-width:2}#mermaid-svg-tKjqynBtV9PTEQa1 .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .taskText:not([font-size]){font-size:11px}#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-tKjqynBtV9PTEQa1 .task.clickable{cursor:pointer}#mermaid-svg-tKjqynBtV9PTEQa1 .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-tKjqynBtV9PTEQa1 .taskText0,#mermaid-svg-tKjqynBtV9PTEQa1 .taskText1,#mermaid-svg-tKjqynBtV9PTEQa1 .taskText2,#mermaid-svg-tKjqynBtV9PTEQa1 .taskText3{fill:#fff}#mermaid-svg-tKjqynBtV9PTEQa1 .task0,#mermaid-svg-tKjqynBtV9PTEQa1 .task1,#mermaid-svg-tKjqynBtV9PTEQa1 .task2,#mermaid-svg-tKjqynBtV9PTEQa1 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutside0,#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutside2{fill:#000}#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutside1,#mermaid-svg-tKjqynBtV9PTEQa1 .taskTextOutside3{fill:#000}#mermaid-svg-tKjqynBtV9PTEQa1 .active0,#mermaid-svg-tKjqynBtV9PTEQa1 .active1,#mermaid-svg-tKjqynBtV9PTEQa1 .active2,#mermaid-svg-tKjqynBtV9PTEQa1 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-tKjqynBtV9PTEQa1 .activeText0,#mermaid-svg-tKjqynBtV9PTEQa1 .activeText1,#mermaid-svg-tKjqynBtV9PTEQa1 .activeText2,#mermaid-svg-tKjqynBtV9PTEQa1 .activeText3{fill:#000 !important}#mermaid-svg-tKjqynBtV9PTEQa1 .done0,#mermaid-svg-tKjqynBtV9PTEQa1 .done1,#mermaid-svg-tKjqynBtV9PTEQa1 .done2,#mermaid-svg-tKjqynBtV9PTEQa1 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-tKjqynBtV9PTEQa1 .doneText0,#mermaid-svg-tKjqynBtV9PTEQa1 .doneText1,#mermaid-svg-tKjqynBtV9PTEQa1 .doneText2,#mermaid-svg-tKjqynBtV9PTEQa1 .doneText3{fill:#000 !important}#mermaid-svg-tKjqynBtV9PTEQa1 .crit0,#mermaid-svg-tKjqynBtV9PTEQa1 .crit1,#mermaid-svg-tKjqynBtV9PTEQa1 .crit2,#mermaid-svg-tKjqynBtV9PTEQa1 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-tKjqynBtV9PTEQa1 .activeCrit0,#mermaid-svg-tKjqynBtV9PTEQa1 .activeCrit1,#mermaid-svg-tKjqynBtV9PTEQa1 .activeCrit2,#mermaid-svg-tKjqynBtV9PTEQa1 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-tKjqynBtV9PTEQa1 .doneCrit0,#mermaid-svg-tKjqynBtV9PTEQa1 .doneCrit1,#mermaid-svg-tKjqynBtV9PTEQa1 .doneCrit2,#mermaid-svg-tKjqynBtV9PTEQa1 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-tKjqynBtV9PTEQa1 .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-tKjqynBtV9PTEQa1 .milestoneText{font-style:italic}#mermaid-svg-tKjqynBtV9PTEQa1 .doneCritText0,#mermaid-svg-tKjqynBtV9PTEQa1 .doneCritText1,#mermaid-svg-tKjqynBtV9PTEQa1 .doneCritText2,#mermaid-svg-tKjqynBtV9PTEQa1 .doneCritText3{fill:#000 !important}#mermaid-svg-tKjqynBtV9PTEQa1 .activeCritText0,#mermaid-svg-tKjqynBtV9PTEQa1 .activeCritText1,#mermaid-svg-tKjqynBtV9PTEQa1 .activeCritText2,#mermaid-svg-tKjqynBtV9PTEQa1 .activeCritText3{fill:#000 !important}#mermaid-svg-tKjqynBtV9PTEQa1 .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-tKjqynBtV9PTEQa1 g.classGroup text .title{font-weight:bolder}#mermaid-svg-tKjqynBtV9PTEQa1 g.clickable{cursor:pointer}#mermaid-svg-tKjqynBtV9PTEQa1 g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-tKjqynBtV9PTEQa1 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-tKjqynBtV9PTEQa1 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-tKjqynBtV9PTEQa1 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-tKjqynBtV9PTEQa1 .dashed-line{stroke-dasharray:3}#mermaid-svg-tKjqynBtV9PTEQa1 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 .commit-id,#mermaid-svg-tKjqynBtV9PTEQa1 .commit-msg,#mermaid-svg-tKjqynBtV9PTEQa1 .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-tKjqynBtV9PTEQa1 g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-tKjqynBtV9PTEQa1 g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-tKjqynBtV9PTEQa1 g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-tKjqynBtV9PTEQa1 .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-tKjqynBtV9PTEQa1 .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-tKjqynBtV9PTEQa1 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-tKjqynBtV9PTEQa1 .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-tKjqynBtV9PTEQa1 .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-tKjqynBtV9PTEQa1 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-tKjqynBtV9PTEQa1 .edgeLabel text{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-tKjqynBtV9PTEQa1 .node circle.state-start{fill:black;stroke:black}#mermaid-svg-tKjqynBtV9PTEQa1 .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-tKjqynBtV9PTEQa1 #statediagram-barbEnd{fill:#9370db}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-state .divider{stroke:#9370db}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-tKjqynBtV9PTEQa1 .note-edge{stroke-dasharray:5}#mermaid-svg-tKjqynBtV9PTEQa1 .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-tKjqynBtV9PTEQa1 .error-icon{fill:#522}#mermaid-svg-tKjqynBtV9PTEQa1 .error-text{fill:#522;stroke:#522}#mermaid-svg-tKjqynBtV9PTEQa1 .edge-thickness-normal{stroke-width:2px}#mermaid-svg-tKjqynBtV9PTEQa1 .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-tKjqynBtV9PTEQa1 .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-tKjqynBtV9PTEQa1 .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-tKjqynBtV9PTEQa1 .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-tKjqynBtV9PTEQa1 .marker{fill:#333}#mermaid-svg-tKjqynBtV9PTEQa1 .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-tKjqynBtV9PTEQa1 {color: rgba(0, 0, 0, 0.75);font: ;}

rdbSave,
rdbLoad,
内存中的数据对象,,
磁盘中的RDB文件,,

10.7 劣势

  • Fork的时候,内存中的数据被克隆了一份,大致2倍的膨胀性需要考虑
  • 虽然Redis在fork时使用了写时拷贝技术,但是如果数据庞大时还是比较消耗性能。
  • 在备份周期在一定间隔时间做一次备份,所以如果 Redis意外down掉的话就会丢失最后一次快照后的所有修改。

10.8 如何停止

动态停止RDB

redis-cli config set save “”

save后给空值,标识禁用保存策略

10.9 总结

11 持久化之AOF

11.1 AOF (Append Only File) .

11.1.1是什么

以日志的形式来记录每个写操作(增量保存),将Redis执行过的所有写指令记录下来(读操作不记录),只许追加文件但不可以改写文件,redis启动之初会读取该文件重新构建数据,换言之,redis重启的话就根据日志文件的内容将写指令从前到后执行一次以完成数据的恢复工作

11.1.2 持久化流程

  1. 客户端的请求写命令会被append追加到AOF缓冲区内;
  2. AOF缓冲区根据AOF持久化策略[always,everysec,no]将操作sync同步到磁盘的AOF文件中;
  3. AOF文件大小超过重写策略或手动重写时,会对 AOF文件 rewrite重写,压缩AOF文件容量
  4. Redis服务重启时,会重新load 加载 AOF 文件中的写操作达到数据恢复的目的
#mermaid-svg-zYp1jdKTSj3Aiv5o .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .label text{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .node rect,#mermaid-svg-zYp1jdKTSj3Aiv5o .node circle,#mermaid-svg-zYp1jdKTSj3Aiv5o .node ellipse,#mermaid-svg-zYp1jdKTSj3Aiv5o .node polygon,#mermaid-svg-zYp1jdKTSj3Aiv5o .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-zYp1jdKTSj3Aiv5o .node .label{text-align:center;fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .node.clickable{cursor:pointer}#mermaid-svg-zYp1jdKTSj3Aiv5o .arrowheadPath{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-zYp1jdKTSj3Aiv5o .flowchart-link{stroke:#333;fill:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-zYp1jdKTSj3Aiv5o .edgeLabel rect{opacity:0.9}#mermaid-svg-zYp1jdKTSj3Aiv5o .edgeLabel span{color:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-zYp1jdKTSj3Aiv5o .cluster text{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-zYp1jdKTSj3Aiv5o .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-zYp1jdKTSj3Aiv5o text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .actor-line{stroke:grey}#mermaid-svg-zYp1jdKTSj3Aiv5o .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .sequenceNumber{fill:#fff}#mermaid-svg-zYp1jdKTSj3Aiv5o #sequencenumber{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o #crosshead path{fill:#333;stroke:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .messageText{fill:#333;stroke:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-zYp1jdKTSj3Aiv5o .labelText,#mermaid-svg-zYp1jdKTSj3Aiv5o .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .loopText,#mermaid-svg-zYp1jdKTSj3Aiv5o .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-zYp1jdKTSj3Aiv5o .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-zYp1jdKTSj3Aiv5o .noteText,#mermaid-svg-zYp1jdKTSj3Aiv5o .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-zYp1jdKTSj3Aiv5o .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-zYp1jdKTSj3Aiv5o .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-zYp1jdKTSj3Aiv5o .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .section{stroke:none;opacity:0.2}#mermaid-svg-zYp1jdKTSj3Aiv5o .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-zYp1jdKTSj3Aiv5o .section2{fill:#fff400}#mermaid-svg-zYp1jdKTSj3Aiv5o .section1,#mermaid-svg-zYp1jdKTSj3Aiv5o .section3{fill:#fff;opacity:0.2}#mermaid-svg-zYp1jdKTSj3Aiv5o .sectionTitle0{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .sectionTitle1{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .sectionTitle2{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .sectionTitle3{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-zYp1jdKTSj3Aiv5o .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .grid path{stroke-width:0}#mermaid-svg-zYp1jdKTSj3Aiv5o .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-zYp1jdKTSj3Aiv5o .task{stroke-width:2}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText:not([font-size]){font-size:11px}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-zYp1jdKTSj3Aiv5o .task.clickable{cursor:pointer}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText0,#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText1,#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText2,#mermaid-svg-zYp1jdKTSj3Aiv5o .taskText3{fill:#fff}#mermaid-svg-zYp1jdKTSj3Aiv5o .task0,#mermaid-svg-zYp1jdKTSj3Aiv5o .task1,#mermaid-svg-zYp1jdKTSj3Aiv5o .task2,#mermaid-svg-zYp1jdKTSj3Aiv5o .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutside0,#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutside2{fill:#000}#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutside1,#mermaid-svg-zYp1jdKTSj3Aiv5o .taskTextOutside3{fill:#000}#mermaid-svg-zYp1jdKTSj3Aiv5o .active0,#mermaid-svg-zYp1jdKTSj3Aiv5o .active1,#mermaid-svg-zYp1jdKTSj3Aiv5o .active2,#mermaid-svg-zYp1jdKTSj3Aiv5o .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-zYp1jdKTSj3Aiv5o .activeText0,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeText1,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeText2,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeText3{fill:#000 !important}#mermaid-svg-zYp1jdKTSj3Aiv5o .done0,#mermaid-svg-zYp1jdKTSj3Aiv5o .done1,#mermaid-svg-zYp1jdKTSj3Aiv5o .done2,#mermaid-svg-zYp1jdKTSj3Aiv5o .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-zYp1jdKTSj3Aiv5o .doneText0,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneText1,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneText2,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneText3{fill:#000 !important}#mermaid-svg-zYp1jdKTSj3Aiv5o .crit0,#mermaid-svg-zYp1jdKTSj3Aiv5o .crit1,#mermaid-svg-zYp1jdKTSj3Aiv5o .crit2,#mermaid-svg-zYp1jdKTSj3Aiv5o .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCrit0,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCrit1,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCrit2,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCrit0,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCrit1,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCrit2,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-zYp1jdKTSj3Aiv5o .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-zYp1jdKTSj3Aiv5o .milestoneText{font-style:italic}#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCritText0,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCritText1,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCritText2,#mermaid-svg-zYp1jdKTSj3Aiv5o .doneCritText3{fill:#000 !important}#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCritText0,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCritText1,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCritText2,#mermaid-svg-zYp1jdKTSj3Aiv5o .activeCritText3{fill:#000 !important}#mermaid-svg-zYp1jdKTSj3Aiv5o .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-zYp1jdKTSj3Aiv5o g.classGroup text .title{font-weight:bolder}#mermaid-svg-zYp1jdKTSj3Aiv5o g.clickable{cursor:pointer}#mermaid-svg-zYp1jdKTSj3Aiv5o g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-zYp1jdKTSj3Aiv5o g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-zYp1jdKTSj3Aiv5o .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-zYp1jdKTSj3Aiv5o .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .dashed-line{stroke-dasharray:3}#mermaid-svg-zYp1jdKTSj3Aiv5o #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o .commit-id,#mermaid-svg-zYp1jdKTSj3Aiv5o .commit-msg,#mermaid-svg-zYp1jdKTSj3Aiv5o .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-zYp1jdKTSj3Aiv5o g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-zYp1jdKTSj3Aiv5o g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-zYp1jdKTSj3Aiv5o g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-zYp1jdKTSj3Aiv5o .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-zYp1jdKTSj3Aiv5o .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-zYp1jdKTSj3Aiv5o .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-zYp1jdKTSj3Aiv5o .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-zYp1jdKTSj3Aiv5o .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-zYp1jdKTSj3Aiv5o .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-zYp1jdKTSj3Aiv5o .edgeLabel text{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-zYp1jdKTSj3Aiv5o .node circle.state-start{fill:black;stroke:black}#mermaid-svg-zYp1jdKTSj3Aiv5o .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-zYp1jdKTSj3Aiv5o #statediagram-barbEnd{fill:#9370db}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-state .divider{stroke:#9370db}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-zYp1jdKTSj3Aiv5o .note-edge{stroke-dasharray:5}#mermaid-svg-zYp1jdKTSj3Aiv5o .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-zYp1jdKTSj3Aiv5o .error-icon{fill:#522}#mermaid-svg-zYp1jdKTSj3Aiv5o .error-text{fill:#522;stroke:#522}#mermaid-svg-zYp1jdKTSj3Aiv5o .edge-thickness-normal{stroke-width:2px}#mermaid-svg-zYp1jdKTSj3Aiv5o .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-zYp1jdKTSj3Aiv5o .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-zYp1jdKTSj3Aiv5o .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-zYp1jdKTSj3Aiv5o .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-zYp1jdKTSj3Aiv5o .marker{fill:#333}#mermaid-svg-zYp1jdKTSj3Aiv5o .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-zYp1jdKTSj3Aiv5o {color: rgba(0, 0, 0, 0.75);font: ;}

append,
sync,
load,
rewrite,
命令写入
AOF缓冲,
AOF文件,
重启,

11.1.3 AOF默认不开启

  • 可以在redis.conf 中配置文件名称,默认为 appendonly.aof
  • AOF文件的保存路径,同RDB的路径一致。

11.1.4 AOF和RDB同时开启,redis听谁的?

AOF和RDB同时开启,系统默认取AOF的数据(数据不会存在丢失)

11.1.4 AOF启动/修复/恢复

  • 启动
    redis配置文件

    appendonly yes

    修改文件名字

    appendfilename “appendonly.aof”

    修改文件路径:
    AOF跟着RDB的目录走,相同路径下

  • 备份
    AOF的备份机制和性能虽然和RDB不同,但是备份和恢复的操作同RDB一样,都是拷贝备份文件,需要恢复时再拷贝到Redis工作目录下,启动系统即加载。

  • 正常恢复
    修改默认的appendonly no 改为 yes
    将有数据的aof文件复制一份保存到对应目录(查看目录:conifg get dir)

  • 异常恢复
    Could not connect to Redis at 127.0.0.1:6379:Connection refused(无法连接到Redis 127.0.0.1:6379:连接被拒绝)

    1. 修改默认的appendonly no,改为yes-
    2. 如遇到AOF文件损坏,通过进行恢复

      /usr/local/bin/redis-check-aof --fix appendonly.aof

    3. 备份被写坏的AOF文件
    4. 恢复:重启redis,然后重新加载·

11.1.5 AOF同步频率设置

  • appendfsync always
    始终同步,每次Redis的写入都会立刻记入日志;性能较差但数据完整性比较好.
  • appendfsync everysec
    每秒同步,每秒记入日志一次,如果宕机,本秒的数据可能丢失。
  • appendfsync no
    redis不主动进行同步,把同步时机交给操作系统。

11.1.6 Rewrite(重写) 压缩

  1. 这是什么?
    AOF采用文件追加方式,文件会越来越大为避免出现此种情况,新增了重写机制,当AOF文件的大小超过所设定的阈值时,Redis就会启动AOF文件的内容压缩,只保留可以恢复数据的最小指令集.可以使用命令: bgrewriteaof

  2. 重写原理,如何实现重写
    AOF文件持续增长而过大时,会fork 出一条新进程来将文件重写(也是先写临时文件最后再rename) , redis4.0版本后的重写,是指上就是把rdb 的快照,以二级制的形式附在新的aof头部,作为已有的历史数据,替换掉原来的流水账操作。

noappendfsync-on-rewrite:

  • 如果no-appendfsync-on-rewrite=yes ,不写入aof文件只写入缓存,用户请求不会阻塞,但是在这段时间如果宕机会丢失这段时间的缓存数据。(降低数据安全性,提高性能)·

  • 如果no-appendfsync-on-rewrite=no,还是会把数据往磁盘里刷,但是遇到重写操作,可能会发生阻塞。(数据安全,但是性能降低)

触发机制,何时重写?

  • Redis 会记录上次重写时的AOF大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且文件大于64M时触发

  • 重写虽然可以节约大量磁盘空间,减少恢复时间。但是每次重写还是有一定的负担的,因此设定 Redis要满足一定条件才会进行重写。

  • auto-aof-rewrite-percentage:设置重写的基准值,文件达到100%时开始重写(文件是原来重写后文件的2倍时触发),

  • auto-aof-rewrite-min-size:设置重写的基准值,最小文件64MB。达到这个值开始重写。

例如∶文件达到7OMB开始重写,降到50MB,下次什么时候开始重写?100MB.
系统载入时或者上次重写完毕时,Redis 会记录此时AOF大小,设为 base_size,

如果Redis 的 AOF当前大小>= base_size +base_size*100%(默认)且当前大小>=64mb(默认)的情况下,Redis 会对AOF进行重写。

重写流程:

  1. bgrewriteaof触发重写,判断是否当前有bgsave或 bgrewriteaof在运行,如果有,则等待该命令结束后再继续执行。
  2. 主进程fork 出子进程执行重写操作,保证主进程不会阻塞。
  3. 子进程遍历redis 内存中数据到临时文件,客户端的写请求同时写入aof_buf缓冲区和aof_rewrite_buf重写缓冲区保证原AOF文件完整以及新AOF文件生成期间的新的数据修改动作不会丢失。
  4. 1). 子进程写完新的AOF文件后,向主进程发信号,父进程更新统计信息。2). 主进程把aof_rewrite_buf中的数据写入到新的AOF文件。
  5. 使用新的AOF文件覆盖旧的AOF文件,完成AOF重写。
#mermaid-svg-463enD0roHJWmk9j .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-463enD0roHJWmk9j .label text{fill:#333}#mermaid-svg-463enD0roHJWmk9j .node rect,#mermaid-svg-463enD0roHJWmk9j .node circle,#mermaid-svg-463enD0roHJWmk9j .node ellipse,#mermaid-svg-463enD0roHJWmk9j .node polygon,#mermaid-svg-463enD0roHJWmk9j .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-463enD0roHJWmk9j .node .label{text-align:center;fill:#333}#mermaid-svg-463enD0roHJWmk9j .node.clickable{cursor:pointer}#mermaid-svg-463enD0roHJWmk9j .arrowheadPath{fill:#333}#mermaid-svg-463enD0roHJWmk9j .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-463enD0roHJWmk9j .flowchart-link{stroke:#333;fill:none}#mermaid-svg-463enD0roHJWmk9j .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-463enD0roHJWmk9j .edgeLabel rect{opacity:0.9}#mermaid-svg-463enD0roHJWmk9j .edgeLabel span{color:#333}#mermaid-svg-463enD0roHJWmk9j .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-463enD0roHJWmk9j .cluster text{fill:#333}#mermaid-svg-463enD0roHJWmk9j div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-463enD0roHJWmk9j .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-463enD0roHJWmk9j text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-463enD0roHJWmk9j .actor-line{stroke:grey}#mermaid-svg-463enD0roHJWmk9j .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-463enD0roHJWmk9j .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-463enD0roHJWmk9j #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-463enD0roHJWmk9j .sequenceNumber{fill:#fff}#mermaid-svg-463enD0roHJWmk9j #sequencenumber{fill:#333}#mermaid-svg-463enD0roHJWmk9j #crosshead path{fill:#333;stroke:#333}#mermaid-svg-463enD0roHJWmk9j .messageText{fill:#333;stroke:#333}#mermaid-svg-463enD0roHJWmk9j .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-463enD0roHJWmk9j .labelText,#mermaid-svg-463enD0roHJWmk9j .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-463enD0roHJWmk9j .loopText,#mermaid-svg-463enD0roHJWmk9j .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-463enD0roHJWmk9j .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-463enD0roHJWmk9j .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-463enD0roHJWmk9j .noteText,#mermaid-svg-463enD0roHJWmk9j .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-463enD0roHJWmk9j .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-463enD0roHJWmk9j .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-463enD0roHJWmk9j .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-463enD0roHJWmk9j .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .section{stroke:none;opacity:0.2}#mermaid-svg-463enD0roHJWmk9j .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-463enD0roHJWmk9j .section2{fill:#fff400}#mermaid-svg-463enD0roHJWmk9j .section1,#mermaid-svg-463enD0roHJWmk9j .section3{fill:#fff;opacity:0.2}#mermaid-svg-463enD0roHJWmk9j .sectionTitle0{fill:#333}#mermaid-svg-463enD0roHJWmk9j .sectionTitle1{fill:#333}#mermaid-svg-463enD0roHJWmk9j .sectionTitle2{fill:#333}#mermaid-svg-463enD0roHJWmk9j .sectionTitle3{fill:#333}#mermaid-svg-463enD0roHJWmk9j .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-463enD0roHJWmk9j .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .grid path{stroke-width:0}#mermaid-svg-463enD0roHJWmk9j .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-463enD0roHJWmk9j .task{stroke-width:2}#mermaid-svg-463enD0roHJWmk9j .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .taskText:not([font-size]){font-size:11px}#mermaid-svg-463enD0roHJWmk9j .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-463enD0roHJWmk9j .task.clickable{cursor:pointer}#mermaid-svg-463enD0roHJWmk9j .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-463enD0roHJWmk9j .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-463enD0roHJWmk9j .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-463enD0roHJWmk9j .taskText0,#mermaid-svg-463enD0roHJWmk9j .taskText1,#mermaid-svg-463enD0roHJWmk9j .taskText2,#mermaid-svg-463enD0roHJWmk9j .taskText3{fill:#fff}#mermaid-svg-463enD0roHJWmk9j .task0,#mermaid-svg-463enD0roHJWmk9j .task1,#mermaid-svg-463enD0roHJWmk9j .task2,#mermaid-svg-463enD0roHJWmk9j .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-463enD0roHJWmk9j .taskTextOutside0,#mermaid-svg-463enD0roHJWmk9j .taskTextOutside2{fill:#000}#mermaid-svg-463enD0roHJWmk9j .taskTextOutside1,#mermaid-svg-463enD0roHJWmk9j .taskTextOutside3{fill:#000}#mermaid-svg-463enD0roHJWmk9j .active0,#mermaid-svg-463enD0roHJWmk9j .active1,#mermaid-svg-463enD0roHJWmk9j .active2,#mermaid-svg-463enD0roHJWmk9j .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-463enD0roHJWmk9j .activeText0,#mermaid-svg-463enD0roHJWmk9j .activeText1,#mermaid-svg-463enD0roHJWmk9j .activeText2,#mermaid-svg-463enD0roHJWmk9j .activeText3{fill:#000 !important}#mermaid-svg-463enD0roHJWmk9j .done0,#mermaid-svg-463enD0roHJWmk9j .done1,#mermaid-svg-463enD0roHJWmk9j .done2,#mermaid-svg-463enD0roHJWmk9j .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-463enD0roHJWmk9j .doneText0,#mermaid-svg-463enD0roHJWmk9j .doneText1,#mermaid-svg-463enD0roHJWmk9j .doneText2,#mermaid-svg-463enD0roHJWmk9j .doneText3{fill:#000 !important}#mermaid-svg-463enD0roHJWmk9j .crit0,#mermaid-svg-463enD0roHJWmk9j .crit1,#mermaid-svg-463enD0roHJWmk9j .crit2,#mermaid-svg-463enD0roHJWmk9j .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-463enD0roHJWmk9j .activeCrit0,#mermaid-svg-463enD0roHJWmk9j .activeCrit1,#mermaid-svg-463enD0roHJWmk9j .activeCrit2,#mermaid-svg-463enD0roHJWmk9j .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-463enD0roHJWmk9j .doneCrit0,#mermaid-svg-463enD0roHJWmk9j .doneCrit1,#mermaid-svg-463enD0roHJWmk9j .doneCrit2,#mermaid-svg-463enD0roHJWmk9j .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-463enD0roHJWmk9j .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-463enD0roHJWmk9j .milestoneText{font-style:italic}#mermaid-svg-463enD0roHJWmk9j .doneCritText0,#mermaid-svg-463enD0roHJWmk9j .doneCritText1,#mermaid-svg-463enD0roHJWmk9j .doneCritText2,#mermaid-svg-463enD0roHJWmk9j .doneCritText3{fill:#000 !important}#mermaid-svg-463enD0roHJWmk9j .activeCritText0,#mermaid-svg-463enD0roHJWmk9j .activeCritText1,#mermaid-svg-463enD0roHJWmk9j .activeCritText2,#mermaid-svg-463enD0roHJWmk9j .activeCritText3{fill:#000 !important}#mermaid-svg-463enD0roHJWmk9j .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-463enD0roHJWmk9j g.classGroup text .title{font-weight:bolder}#mermaid-svg-463enD0roHJWmk9j g.clickable{cursor:pointer}#mermaid-svg-463enD0roHJWmk9j g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-463enD0roHJWmk9j g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-463enD0roHJWmk9j .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-463enD0roHJWmk9j .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-463enD0roHJWmk9j .dashed-line{stroke-dasharray:3}#mermaid-svg-463enD0roHJWmk9j #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j .commit-id,#mermaid-svg-463enD0roHJWmk9j .commit-msg,#mermaid-svg-463enD0roHJWmk9j .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-463enD0roHJWmk9j g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-463enD0roHJWmk9j g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-463enD0roHJWmk9j g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-463enD0roHJWmk9j g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-463enD0roHJWmk9j .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-463enD0roHJWmk9j .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-463enD0roHJWmk9j .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-463enD0roHJWmk9j .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-463enD0roHJWmk9j .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-463enD0roHJWmk9j .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-463enD0roHJWmk9j .edgeLabel text{fill:#333}#mermaid-svg-463enD0roHJWmk9j .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-463enD0roHJWmk9j .node circle.state-start{fill:black;stroke:black}#mermaid-svg-463enD0roHJWmk9j .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-463enD0roHJWmk9j #statediagram-barbEnd{fill:#9370db}#mermaid-svg-463enD0roHJWmk9j .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-463enD0roHJWmk9j .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-463enD0roHJWmk9j .statediagram-state .divider{stroke:#9370db}#mermaid-svg-463enD0roHJWmk9j .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-463enD0roHJWmk9j .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-463enD0roHJWmk9j .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-463enD0roHJWmk9j .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-463enD0roHJWmk9j .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-463enD0roHJWmk9j .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-463enD0roHJWmk9j .note-edge{stroke-dasharray:5}#mermaid-svg-463enD0roHJWmk9j .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-463enD0roHJWmk9j .error-icon{fill:#522}#mermaid-svg-463enD0roHJWmk9j .error-text{fill:#522;stroke:#522}#mermaid-svg-463enD0roHJWmk9j .edge-thickness-normal{stroke-width:2px}#mermaid-svg-463enD0roHJWmk9j .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-463enD0roHJWmk9j .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-463enD0roHJWmk9j .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-463enD0roHJWmk9j .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-463enD0roHJWmk9j .marker{fill:#333}#mermaid-svg-463enD0roHJWmk9j .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-463enD0roHJWmk9j {color: rgba(0, 0, 0, 0.75);font: ;}

1,
2,
4.1,通知主进程,,
3
4.2
5,覆盖
bgrewriteaof..
redis主进程..
fork.
子进程,
aof_rewrite_buf,,
写命令,
aof_buf,
新AOF文件,
旧AOF文件,

11.2 优势

  • 备份机制更稳健,丢失数据概率更低。
  • 可读的日志文本,通过操作AOF稳健,可以处理误操作。

11.3 劣势

  • 比起RDB占用更多的磁盘空间。
  • 恢复备份速度要慢。
  • 每次读写都同步的话,有一定的性能压力。
  • 存在个别Bug,造成恢复不能。

11.4 总结

11.5 AOF和RDB总结:

11.5.1 用哪个好?

  • 官方推荐两个都启用。
  • 如果对数据不敏感,可以选单独用RDB。
  • 不建议单独用AOF,因为可能会出现Bug。
  • 如果只是做纯内存缓存,可以都不用。

官网建议:

  • RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储
  • AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,AOF命令以redis协议追加保存每次写的操作到文件末尾.
  • Redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大
  • 只做缓存∶如果你只希望你的数据在服务器运行的时候存在,你也可以不使用任何持久化方式.
  • 同时开启两种持久化方式
  • 在这种情况下,当redis重启的时候会优先载入AOF文件来恢复原始的数据,因为在通常情况下AOF文件保存的数据集要比 RDB文件保存的数据集要完整
  • RDB的数据不实时,同时使用两者时服务器重启也只会找AOF文件。那要不要只使用AOF呢?
  • 建议不要,因为RDB更适合用于备份数据库(AOF在不断变化不好备份),快速重启,而且不会有AOF可能潜在的bug,留着作为一个万一的手段。
  • 性能建议:
    1. 因为RDB文件只用作后备用途,建议只在Slave上持久化RDB文件,而且只要15分钟备份一次就够了,只保留save 900 1这条规则。
    2. 如果使用AOF,好处是在最恶劣情况下也只会丢失不超过两秒数据,启动脚本较简单只load自己的AOF文件就可以了。
    3. 代价,一是带来了持续的IO,二是 AOF rewrite的最后将rewrite过程中产生的新数据写到新文件造成的阻塞几乎是不可避免的。
    4. 只要硬盘许可,应该尽量减少AOF rewrite的频率,AOF重写的基础大小默认值64M太小了,可以设到5G以上。
    5. 默认超过原大小100%大小时重写可以改到适当的数值。

12 Redis6 的主从复制

12.1 是什么?

主机数据更新后根据配置和策略,自动同步到备机的 master/slaver机制,Master以写为主,Slave以读为主

12.2 能干嘛?

  • 读写分离,性能扩展
  • 容灾快速恢复

    主服务器只做写操作,然后复制一份到从服务器,应用从 从服务器中获取数据
    如果一台从服务器挂了,可以快速切换到其他从服务器获取数据

12.3 怎么用?

这是在一台服务器模拟的,真实情况每个redis占用一台linux服务器!!!

  1. 创建 /myredis文件在(根目录)

  2. 复制redis.conf配置文件到myredis文件夹中

  3. 配置一主两从,创建三个配置文件:
    redis6379.conf
    redis6380.conf
    redis6381.conf

  4. 修改redis.conf中的内容
    appendonly no (aof是否启动)
    关闭aof,在配置中就不必再写aof的配置了
    否则要加上appendfilename appendonly6380.aof

  5. 在三个配置文件中写入内容

    include /myredis/redis.conf
    pidfile /var/run/redis_6380.pid
    port 6380
    dbfilename dump6380.rdb
    #aof文件路径,
    #appendfilename appendonly6380.aof

    各个文件中的配置要跟名称对应(这咋么对应不要我说了吧,端口号!!!)

12.4 查看三台主机运行情况

打印主从复制的相关信息

info replication

进入redis:
根据端口号,其他两个也是一样的方式

redis-cli -p 6379

执行结果:

127.0.0.1:6381> info replication
#Replication
role:master
connected_slaves:0

rooe:master 主服务器
connected_slaves:0 从服务器有多少个

12.5 配从(库)不配主(库)

slaveof <ip,> <port,>

成为某个实例的从服务器

  1. 在6380和6381上执行:

    slaveof 127.0.0.1 6379

  2. 查看从机的角色:

  3. 查看主机角色:

12.6 主从测试

这是一主两从的配置方式,如果要三从,四从等,按照上面的步骤来就行

  1. 在主(6379)上 添加

    127.0.0.1:6379> set k1 v1
    OK
    127.0.0.1:6379> get k1
    “v1”
    127.0.0.1:6379>

  2. 在从(6380,6381)上 查看

    127.0.0.1:6380> get k1
    “v1”
    127.0.0.1:6381> get k1
    “v1”

    都能查看

  3. 在从(6380,6381)上 添加

    127.0.0.1:6380> set k2 v2
    (error) READONLY You can’t write against a read only replica.
    127.0.0.1:6381> set k2 v2
    (error) READONLY You can’t write against a read only replica.

    报错,read only 只能读

12.7 常用三招

12.7.1 一主二仆

  • 切入点问题? slave1、slave2是从头开始复制还是从切入点开始复制?比如从k4进来,那之前的k1,k2,k3是否也可以复制?
  • 从机是否可以写? set可否?
  • 主机 shutdown后情况如何?从机是上位还是原地待命?
  1. 一台从服务器挂了

    假如6381挂了,主服务器中添加:

    set k2 v2

    再启动,链接6381

    redis-server redis6381.conf
    redis-cli -p 6381

    特点一:从服务器挂掉重启后,自动变为主服务器
    重新设置6381为6379的从服务器

    127.0.0.1:6381> slaveof 127.0.0.1 6379


    发现6381也有在其关闭期间主(6379)添加的数据

    重新启动,切点为头,全部复制过来

  2. 主服务器挂了

    特点二:主服务器挂了,从服务器还是从服务器,主服务器重新启动,还是主服务器

    大哥永远是大哥
    小弟永远是小弟
    小弟如果想上位
    不 ! 可 ! ! 能 ! ! !

12.7.2 薪火相传

上一个slave可以是下一个slave的master,slave同样可以接收其他slaves的连接和同步请求,那么该的连接和同步请求,那么该作为了链条中下一个的主人,可以有效减轻master的写压力,去中心化降低风险. (java的继承???)
用 slaveof<IP,><prot,>
中途变更转向:会清除之前的数据,重新建立拷贝最新的

让6381直接继承6380:

127.0.0.1:6381> slaveof 127.0.0.1 6380
OK

6379的服务器只有一台从服务器(6380)
6380的服务器只有一台从服务器(6381),一台主服务器(6379)

12.7.3 反客为主

slaveof no one
当主机直接暴毙,需要一个新老大,你是上帝,手动指派:

127.0.0.1:6380> slaveof no one

这样,6380就是新的老大哥

这个还需要手动指定,能不能全自动那 ?
请看 12.9 :哨兵模式!!!

12.8 主从复制原理

  1. Slave启动成功连接到master后会发送一个sync命令,
  2. 主服务器接到从服务器发送过来同步消息,把主服务器数据进行持久化,RDB文件,把RDB文件发送从服务器,从服务器拿到RDB进行读取(只有这个是从服务器主动进行)
  3. 每次主服务器进行写操作之后,和从服务器进行数据同步(主服务器)
  4. 全量复制:而slave服务在接收到数据库文件数据后,将其存盘并加载到内存中.
  5. 增量复制:master继续将新的所有收集到的修改命令依次传给,完成同步
  6. 但是只要是重新连接master,一次完全同步(全量复制)将被自动执行

12.9 哨兵模式

12.9.1 是什么?

反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库

12.9.2 使用步骤

  1. 调整为一主二仆模式
    6379带着6380和6381玩

  2. 自定义的myredis目录下,新建sentinel.conf文件,名字绝不能错!!!

  3. 配置哨兵,填写内容

    sentinel monitor mymaster 127.0.0.1 6379 1
    #做哨兵,监控,自己起的代号 ip port 至少有多少个哨兵同意迁移的数量

  4. 启动哨兵
    /usr/local/bin
    redis做压测可以用自带的redis-benchmark 工具
    执行

    redis-sentinel /myredis/sentinel.conf

    可以看到基本信息

    Port: 26379
    PID: 12122
    +monitor master mymaster 127.0.0.1 6379 quorum 1
    //意思是监视了一台名字为mymaster的主服务器,ip端口为127.0.0.1 6379
    //有两台从机,分别为:
    12122:X 21 May 2021 07:10:53.230 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
    12122:X 21 May 2021 07:10:53.239 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379

  5. 当主机挂掉,从机选举新的主机
    (大概10秒左右可以看到哨兵窗口日志,切换了新的主机)
    哪个 从机会被选举为主机呢﹖根据优先级别:slaxe-priority
    原主机重启后会变为从机。

12.9.3 故障恢复

  • 优先级在redis.conf中默认: replica-priority 100(6.2.1版本之后好像,之前好像是slave-priority),值越小优先级越高
  • 偏移量是指获得原主机数据最全的
  • 每个redis 实例启动后都会随机生成一个40位的runid

12.9.4 java代码

    private static JedisSentinelPool jedisSentinelPool = null;public static Jedis getJedisFromSentinel(){if(jedisSentinelPool == null){HashSet<String> sentinelSet = new HashSet<>();sentinelSet.add("192.168.230.138:26379");JedisPoolConfig poolConfig = new JedisPoolConfig();poolConfig.setMaxTotal(10); //最大可用连接数poolConfig.setMaxIdle(5); //最大闲置链接poolConfig.setMinIdle(5); //最小闲置链接poolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待poolConfig.setMaxWaitMillis(2000); //等待时间poolConfig.setTestOnBorrow(true); //去连接的时候进行以下测试pingjedisSentinelPool = new JedisSentinelPool("mymaster",sentinelSet,poolConfig);return jedisSentinelPool.getResource();}return jedisSentinelPool.getResource();}

13 Redis集群

13.1 问题

  • 容量不够,redis如何进行扩容?
  • 并发写操作,redis 如何分摊?
  • 另外,主从模式,薪火相传模式,主机宕机,导致ip地址发生变化,应用程序中配置需要修改对应的主机地址、端口等信息。
  • 之前通过代理主机来解决,但是redis3.0中提供了解决方案。就是无中心化集群配置。

13.2 什么是集群?

Redis集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
Redis集群通过分区( partition )来提供一定程度的可用性( availability ) ︰即使集群中有一部分节点失效或者无法进行通讯,集群也可以继续处理命令请求。

13.3 无中心化集群


每台服务器存储不同的数据,请求随机访问一台服务器,如果该服务器中没有将转发到有的那台服务器中,每个服务器各有一台备用服务器,这样,减少了风险

13.4 无中心化集群模拟

13.4.1 删除持久化数据

在 /myredis 文件夹中将aof,rdb文件都删除掉

13.4.2 制作6个实例

6379,6380,8381,6389,6390,6391

  1. 配置基本信息

    include /myredis/redis.conf
    pidfile /var/run/redis_6379.pid
    port 6379
    dbfilename dump6379.rdb

  2. redis cluster 配置修改

    cluster-enabled yes //打开集群模式
    cluster-config-file nodes-6379.conf //设定节点配置文件名
    cluster-node-timeout 15000 //设定节点失联时间,超过该时间(毫秒),集群自动进行主从切换

  3. 删除redis6380,8381.conf 两个文件

  4. 拷贝redis6379.conf, 复制五份,分别为redis6380,8381,6389,6390,6391.conf

  5. 修改其中内容

    vi 6380.conf
    :%s/6379/6380

  6. 启动6个redis服务:

    root@xyy:/myredis# redis-server redis6379.conf
    root@xyy:/myredis# redis-server redis6380.conf
    root@xyy:/myredis# redis-server redis6381.conf
    root@xyy:/myredis# redis-server redis6389.conf
    root@xyy:/myredis# redis-server redis6390.conf
    root@xyy:/myredis# redis-server redis6391.conf
    root@xyy:/myredis# ps aux|grep redis
    root 2268 0.1 0.1 64040 5328 ? Ssl 01:38 0:00 redis-server *:6379 [cluster]
    root 2278 0.1 0.1 64040 4296 ? Ssl 01:40 0:00 redis-server *:6380 [cluster]
    root 2284 0.1 0.1 64040 4308 ? Ssl 01:40 0:00 redis-server *:6381 [cluster]
    root 2290 0.1 0.1 64040 4392 ? Ssl 01:40 0:00 redis-server *:6389 [cluster]
    root 2296 0.1 0.1 64040 4376 ? Ssl 01:40 0:00 redis-server *:6390 [cluster]
    root 2302 0.1 0.1 64040 4372 ? Ssl 01:40 0:00 redis-server *:6391 [cluster]

    启动之后,将会在当前目录下生成notes-xxx.conf文件

  7. 将六个节点合成一个集群
    组合之前,请确保所有redis实例启动后,nodes-xxx.conf文件都生成正常
    进入 redis安装目录中:

    cd /opt/redis-6.2.2/src
    集群环境只能在这儿执行,不能在其他地方

    执行:

    redis-cli --cluster create --cluster-replicas 1 192.168.230.138:6379 192.168.230.138:6380 192.168.230.138:6381 192.168.230.138:6389 192.168.230.138:6390 192.168.230.138:6391

    此处不要用127.0.0.1 ,请用真实Ip地址
    –replicas 1 采用最简单的方式配置集群,一台主机,一台从机,正好三组


    这里会给你默认分配主从关系,问你是否接受,接受就好了
    最后有一个All 16384 slots covered 复制一下

这样集群就完成了搭建

13.4.3 -c 策略链接

之前都是redis-cli -p port的方式链接redis,当写入操作时,会出现MOVED重定向操作,应该用集群方式登陆

采用-c 集群策略链接,设置的数据会自动切换到相应的写主机

redis-cli -c -p 6379

使用cluster nodes命令查看集群信息

13.4.4 分配这六个节点

redis cluster 如何分配这六个节点?

  • 一个集群至少要有三个主节点。
  • 选项–cluster-replicas 1表示我们希望为集群中的每个主节点创建一个从节点。
  • 分配原则尽量保证每个主数据库运行在不同的IP地址,每个从库和主库不在一个IP地址上。(因为这里是虚拟机,模拟的,没办法)

13.4.5 slots 是什么?

  • 一个Redis 集群包含16384个插槽(hash slot ),数据库中的每个键都属于这16384个插槽的其中一个,
  • 集群使用公式 CRC16(key) % 16384来计算键key属于哪个槽,其中 CRC16(key)语句用于计算键key的 CRC16校验和。
  • 集群中的每个节点负责处理一部分插槽。举个例子,如果一个集群可以有主节点,其中:
    1. 节点A负责处理0号至5460号插槽。
    2. 节点B负责处理5461号至10922号插槽。
    3. 节点C负责处理10923号至16383号插槽。

13.5 在集群中录入值

  • 在redis-cli每次录入、查询键值,redis都会计算出该key应该送往的插槽,如果不是该客户端对应服务器的插槽,redis 会报错,并告知应前往的redis实例地址和端口。

  • redis-cli客户端提供了 -c 参数实现自动重定向。

  • redis-cli-e -p 6379 登入后,再录入、查询键值对可以自动重定向。

  • 不在一个slot下的键值,是不能使用mget,mset等多健操作

  • 可以通过{cust}来定义组的概念,从而使key中手内相同内容的键值对放到一个slot中去。

    {大括号中的内容是自定义的}
    这时,用get k2是获取不到内容的,要使用get k2{cust}

13.6 查询集群中的值

cluster keyslot [key]

cluster keyslot [key] 获取key的插槽位置
cluster countkeysinslot [keyindex] 根据插槽位置看插槽中有多少个数据 (可以看到是0,可是之前测试的确填进去了,要不然cluster keyslot也不会查得到,那为啥没得那?端口,6379,填进去的在6381中:)

会自动切换到6381并且获取数据

CLUSTER GETKEYSINSLOT <slot,> <count,>返回count个slot槽中的键。(不常用,过期了)

13.7 故障恢复

  • 如果主节点下线?从节点能否自动升为主节点?注意:15秒超时

  • 主节点恢复后,主从关系会如何?主节点回来变成从机

  • 如果所有某一段插槽的主从节点都宕掉,redis服务是否还能继续?

    可以看到,6379失败了,6391成为了主服务器
    如果6379重新启动,6379的身份为?

    redis-server reids6379.conf


    可以看到6379变成了小弟

  • 如果某一段插槽的主从都挂掉,而cluster-require-full-coverage为yes,那么,整个集群都挂掉

  • 如果某一段插槽的主从都挂掉,而cluster-require-full-coverage为no,那么,该插槽数据全都不能使用,也无法存储。

  • redis.conf中的参数cluster-require-full-coverage.

13.8 Jedis 操作redis集群

  • 即使连接的不是主机,集群会自动切换主机存储。主机写,从机读。
  • 无中心化主从集群。无论从哪台主机写的数据,其他主机上都能读到数据。
    @Testvoid test() {//创建对象//端口写集群中的那个主服务器都行,会自动切换添加获取数据HostAndPort hostAndPort = new HostAndPort("192.168.230.138", 6379);JedisCluster jedisCluster = new JedisCluster(hostAndPort);//进行操作jedisCluster.set("a1","b1");String a1 = jedisCluster.get("a1");System.out.println(a1);jedisCluster.close();}

也可以new一个HashSet<HostAndPort,>();add多个,JedisCluster(set),不过是无中心化集群的方式,一个就够了

13.9 redis 集群优缺点

  • 优点

实现扩容,分摊压力,无中心化配置相对简单

  • 不足

多键操作是不被支持的·需要分组{自定义}
多键的Redis事务是不被支持的。lua脚本不被支持
由于集群方案出现较晚,很多公司已经采用了其他的集群方案,而代理或者客户端分片的方案想要迁移至redis cluster,需要整体迁移而不是逐步过渡,复杂度较大。

14 Redis应用解决问题

14.1 缓存穿透

  1. 问题描述
    key对应的数据在数据源并不存在,每次针对此 key的请求从缓存获取不到,请求都会压到数据源,从而可能压垮数据源。比如用一个不存在的用户id获取用户信息,不论缓存还是数据库都没有,若黑客利用此漏洞进行攻击可能压垮数据库。

  • 应用服务器压力变大
  • redis命中率降低
  • 一直查询数据库
  1. 解决方案
    一个一定不存在缓存及查询不到的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义。

    • 对空值缓存:如果一个查询返回的数据为空(不管是数据是否不存在),我们仍然把这个空结果( null )进行缓存,设置空结果的过期时间会很短,最长不超过五分钟;
    • 设置可访问的名单(白名单)∶
      使用bitmaps类型定义一个可以访问的名单,名单id作为bitmaps的偏移量,每次访问和 bitmap里面的id进行比较,如果访问id不在bitmaps里面,进行拦截,不允许访问。
    • 采用布隆过滤器∶(布隆过滤器(Bloom Filter )是1970年由布隆提出的。它实际上是一个很长的二进制向量(位图)和一系列随机映射函数(哈希函数)。
      布隆过滤器可以用于检索一个元素是否在一个集合中。它的优点是空间效率和查询时间都远选超过一般的算法,缺点是有一定的误识别率和删除困难。)
      将所有可能存在的数据哈希到一个足够大的bitmaps中,一个一定不存在的数据会被这个 bitmaps拦截掉,从而避免了对底层存储系统的查询压力。
    • 进行实时监控:当发现 Redis的命中率开始急速降低,需要排查访问对象和方问的数据,和运维人员配合,可以设置黑名单限制服务

14.2 缓存击穿

  1. 问题描述
    key对应的数据存在,但在redis 中过期,此时若有大量并发请求过来,这些请求发现缓存过期一般都会从后端DB加载数据并回设到缓存,这个时候大并发的请求可能会瞬间把后端DB压垮。
  2. 解决方案
    key可能会在某些时间点被超高并发地访问,是一种非常“热点”的数据。这个时候,需要考虑一个问题:缓存被“击穿”的问题。
    解决问题:

    • 预先设置热门数据:在redis高峰访问之前,把一些热门数据提前存入到redis里面,加大这些热门数据key的时长
    • 实时调整:现场监控哪些数据热门,实时调整key的过期时长
    • 使用锁:
      (1)就是在缓存失效的时候(判断拿出来的值为空),不是立即去 load db。
      (2)先使用缓存工具的某些带成功操作返回值的操作(比如Redis 的 SETNX )去set一个mutex key;
      (3)当操作返回成功时,再进行load db 的操作,并回设缓存,最后删除 mutexkey ;
      (4)当操作返回失败,证明有线程在 load db,当前线程睡眠一段时间再重试整个get缓存的方法。
#mermaid-svg-bRa9CKEXoNViraQ8 .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .label text{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .node rect,#mermaid-svg-bRa9CKEXoNViraQ8 .node circle,#mermaid-svg-bRa9CKEXoNViraQ8 .node ellipse,#mermaid-svg-bRa9CKEXoNViraQ8 .node polygon,#mermaid-svg-bRa9CKEXoNViraQ8 .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-bRa9CKEXoNViraQ8 .node .label{text-align:center;fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .node.clickable{cursor:pointer}#mermaid-svg-bRa9CKEXoNViraQ8 .arrowheadPath{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-bRa9CKEXoNViraQ8 .flowchart-link{stroke:#333;fill:none}#mermaid-svg-bRa9CKEXoNViraQ8 .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-bRa9CKEXoNViraQ8 .edgeLabel rect{opacity:0.9}#mermaid-svg-bRa9CKEXoNViraQ8 .edgeLabel span{color:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-bRa9CKEXoNViraQ8 .cluster text{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-bRa9CKEXoNViraQ8 .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-bRa9CKEXoNViraQ8 text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-bRa9CKEXoNViraQ8 .actor-line{stroke:grey}#mermaid-svg-bRa9CKEXoNViraQ8 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-bRa9CKEXoNViraQ8 #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .sequenceNumber{fill:#fff}#mermaid-svg-bRa9CKEXoNViraQ8 #sequencenumber{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 #crosshead path{fill:#333;stroke:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .messageText{fill:#333;stroke:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-bRa9CKEXoNViraQ8 .labelText,#mermaid-svg-bRa9CKEXoNViraQ8 .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-bRa9CKEXoNViraQ8 .loopText,#mermaid-svg-bRa9CKEXoNViraQ8 .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-bRa9CKEXoNViraQ8 .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-bRa9CKEXoNViraQ8 .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-bRa9CKEXoNViraQ8 .noteText,#mermaid-svg-bRa9CKEXoNViraQ8 .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-bRa9CKEXoNViraQ8 .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-bRa9CKEXoNViraQ8 .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-bRa9CKEXoNViraQ8 .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-bRa9CKEXoNViraQ8 .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .section{stroke:none;opacity:0.2}#mermaid-svg-bRa9CKEXoNViraQ8 .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-bRa9CKEXoNViraQ8 .section2{fill:#fff400}#mermaid-svg-bRa9CKEXoNViraQ8 .section1,#mermaid-svg-bRa9CKEXoNViraQ8 .section3{fill:#fff;opacity:0.2}#mermaid-svg-bRa9CKEXoNViraQ8 .sectionTitle0{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .sectionTitle1{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .sectionTitle2{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .sectionTitle3{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-bRa9CKEXoNViraQ8 .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .grid path{stroke-width:0}#mermaid-svg-bRa9CKEXoNViraQ8 .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-bRa9CKEXoNViraQ8 .task{stroke-width:2}#mermaid-svg-bRa9CKEXoNViraQ8 .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .taskText:not([font-size]){font-size:11px}#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-bRa9CKEXoNViraQ8 .task.clickable{cursor:pointer}#mermaid-svg-bRa9CKEXoNViraQ8 .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-bRa9CKEXoNViraQ8 .taskText0,#mermaid-svg-bRa9CKEXoNViraQ8 .taskText1,#mermaid-svg-bRa9CKEXoNViraQ8 .taskText2,#mermaid-svg-bRa9CKEXoNViraQ8 .taskText3{fill:#fff}#mermaid-svg-bRa9CKEXoNViraQ8 .task0,#mermaid-svg-bRa9CKEXoNViraQ8 .task1,#mermaid-svg-bRa9CKEXoNViraQ8 .task2,#mermaid-svg-bRa9CKEXoNViraQ8 .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutside0,#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutside2{fill:#000}#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutside1,#mermaid-svg-bRa9CKEXoNViraQ8 .taskTextOutside3{fill:#000}#mermaid-svg-bRa9CKEXoNViraQ8 .active0,#mermaid-svg-bRa9CKEXoNViraQ8 .active1,#mermaid-svg-bRa9CKEXoNViraQ8 .active2,#mermaid-svg-bRa9CKEXoNViraQ8 .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-bRa9CKEXoNViraQ8 .activeText0,#mermaid-svg-bRa9CKEXoNViraQ8 .activeText1,#mermaid-svg-bRa9CKEXoNViraQ8 .activeText2,#mermaid-svg-bRa9CKEXoNViraQ8 .activeText3{fill:#000 !important}#mermaid-svg-bRa9CKEXoNViraQ8 .done0,#mermaid-svg-bRa9CKEXoNViraQ8 .done1,#mermaid-svg-bRa9CKEXoNViraQ8 .done2,#mermaid-svg-bRa9CKEXoNViraQ8 .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-bRa9CKEXoNViraQ8 .doneText0,#mermaid-svg-bRa9CKEXoNViraQ8 .doneText1,#mermaid-svg-bRa9CKEXoNViraQ8 .doneText2,#mermaid-svg-bRa9CKEXoNViraQ8 .doneText3{fill:#000 !important}#mermaid-svg-bRa9CKEXoNViraQ8 .crit0,#mermaid-svg-bRa9CKEXoNViraQ8 .crit1,#mermaid-svg-bRa9CKEXoNViraQ8 .crit2,#mermaid-svg-bRa9CKEXoNViraQ8 .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-bRa9CKEXoNViraQ8 .activeCrit0,#mermaid-svg-bRa9CKEXoNViraQ8 .activeCrit1,#mermaid-svg-bRa9CKEXoNViraQ8 .activeCrit2,#mermaid-svg-bRa9CKEXoNViraQ8 .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-bRa9CKEXoNViraQ8 .doneCrit0,#mermaid-svg-bRa9CKEXoNViraQ8 .doneCrit1,#mermaid-svg-bRa9CKEXoNViraQ8 .doneCrit2,#mermaid-svg-bRa9CKEXoNViraQ8 .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-bRa9CKEXoNViraQ8 .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-bRa9CKEXoNViraQ8 .milestoneText{font-style:italic}#mermaid-svg-bRa9CKEXoNViraQ8 .doneCritText0,#mermaid-svg-bRa9CKEXoNViraQ8 .doneCritText1,#mermaid-svg-bRa9CKEXoNViraQ8 .doneCritText2,#mermaid-svg-bRa9CKEXoNViraQ8 .doneCritText3{fill:#000 !important}#mermaid-svg-bRa9CKEXoNViraQ8 .activeCritText0,#mermaid-svg-bRa9CKEXoNViraQ8 .activeCritText1,#mermaid-svg-bRa9CKEXoNViraQ8 .activeCritText2,#mermaid-svg-bRa9CKEXoNViraQ8 .activeCritText3{fill:#000 !important}#mermaid-svg-bRa9CKEXoNViraQ8 .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-bRa9CKEXoNViraQ8 g.classGroup text .title{font-weight:bolder}#mermaid-svg-bRa9CKEXoNViraQ8 g.clickable{cursor:pointer}#mermaid-svg-bRa9CKEXoNViraQ8 g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-bRa9CKEXoNViraQ8 g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-bRa9CKEXoNViraQ8 .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-bRa9CKEXoNViraQ8 .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-bRa9CKEXoNViraQ8 .dashed-line{stroke-dasharray:3}#mermaid-svg-bRa9CKEXoNViraQ8 #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 .commit-id,#mermaid-svg-bRa9CKEXoNViraQ8 .commit-msg,#mermaid-svg-bRa9CKEXoNViraQ8 .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-bRa9CKEXoNViraQ8 g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-bRa9CKEXoNViraQ8 g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-bRa9CKEXoNViraQ8 g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-bRa9CKEXoNViraQ8 .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-bRa9CKEXoNViraQ8 .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-bRa9CKEXoNViraQ8 .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-bRa9CKEXoNViraQ8 .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-bRa9CKEXoNViraQ8 .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-bRa9CKEXoNViraQ8 .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-bRa9CKEXoNViraQ8 .edgeLabel text{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-bRa9CKEXoNViraQ8 .node circle.state-start{fill:black;stroke:black}#mermaid-svg-bRa9CKEXoNViraQ8 .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-bRa9CKEXoNViraQ8 #statediagram-barbEnd{fill:#9370db}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-state .divider{stroke:#9370db}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-bRa9CKEXoNViraQ8 .note-edge{stroke-dasharray:5}#mermaid-svg-bRa9CKEXoNViraQ8 .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-bRa9CKEXoNViraQ8 .error-icon{fill:#522}#mermaid-svg-bRa9CKEXoNViraQ8 .error-text{fill:#522;stroke:#522}#mermaid-svg-bRa9CKEXoNViraQ8 .edge-thickness-normal{stroke-width:2px}#mermaid-svg-bRa9CKEXoNViraQ8 .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-bRa9CKEXoNViraQ8 .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-bRa9CKEXoNViraQ8 .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-bRa9CKEXoNViraQ8 .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-bRa9CKEXoNViraQ8 .marker{fill:#333}#mermaid-svg-bRa9CKEXoNViraQ8 .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-bRa9CKEXoNViraQ8 {color: rgba(0, 0, 0, 0.75);font: ;}

失败
睡眠一段时间再次查询,,,
成功
查询Redis get k1,,
返回结果为空,,
设置排它锁,set key_mutex 1 Ex 180 NX,,,,,
排它锁存在别的线程已经在查数据库,同步缓存了,,,,,
查询数据库,同步环村,删除排它锁,set k1 v1 del key_mutex,,,,,,,

14.3 缓存雪崩

  1. 问题描述:

    • key对应的数据存在,但在redis 中过期,此时若有大量并发请求过来,这些请求发现缓存过期一般都会从后端DB加载数据并回设到缓存,这个时候大并发的请求可能会瞬间把后端DB压垮。
    • 缓存雪崩与缓存击穿的区别在于这里针对很多key缓存,前者则是某一个key.
  2. 解决方案
    缓存失效时的雪崩效应对底层系统的冲击非常可怕!
    解决方案:
    (1) 构建多级缓存架构: nginx缓存+ redis缓存+其他缓存( ehcache等 ) ,
    (2) 使用锁或队列:
    用加锁或者队列的方式保证来保证不会有大量的线程对数据库一次性进行读写,从而避免失效时大量的并发请求落到底层存储系统上。不适用高并发情况
    (3) 设置过期标志更新缓存∶
    记录缓存数据是否过期(设置提前量),如果过期会触发通知另外的线程在后台去更新实际 key的缓存。
    ( 4) 将缓存失效时间分散开:
    比如我们可以在原有的失效时间基础上增加一个随机值,比如1-5分钟随机,这样每一个缓存的过期时间的重复率就会降低,就很难引发集体失效的事件。

14.4 分布式锁

14.4.1 问题描述

随着业务发展的需要,原单体单机部署的系统被演化成分布式集群系统后,由于分布式系统多线程、多进程并且分布在不同机器上,这将使原单机部署情况下的并发控制锁策略失效,单纯的Java API并不能提供分布式锁的能力。为了解决这个问题就需要一种跨JVM的互斥机制来控制共享资源的访问,这就是分布式锁要解决的问题!

分布式锁主流的实现方案:

  1. 基于数据库实现分布式锁
  2. 基于缓存(Redis等)
  3. 基于Zookeeper

每一种分布式锁解决方案都有自己的优缺点

  1. 性能:redis最高
  2. 可靠性:zookeeper最高
    这里,我们基于redis实现分布锁

14.4.2 解决方案:使用redis实现分布式锁

redis命令:
set sku:1:info “OK” NX PX 10000

  • EX second:设置键的过期时间为second秒
    SET key value EX second 效果等同于SETEX key second value
  • PX millisecond :设置键的过期时间为millisecond毫秒。SET key value - PX millisecond效果等同于SETEX key millisecond value 。
  • NX:只在键不存在时,才对键进行设置操作。SET key value NX效果等同于SETNX key value 。
  • XX∶只在键已经存在时,才对键进行设置操作。

del key释放锁

14.4.3 编写代码

lua脚本(可以不用,很少会出现这种,集群不能用lua)
a判断锁的uuid相同时删除前,锁到期了
b获取锁,a删除了b的锁,c又进来了
可以用lua脚本支持

    public void testLock() {String uuid = UUID.randomUUID().toString();//1. 获取锁Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid,3, TimeUnit.SECONDS);//2. 获取锁成功,查询num的值if (lock) {/* lua脚本String script = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";DefaultRedisScript<Long> objectDefaultRedisScript = new DefaultRedisScript<>();objectDefaultRedisScript.setScriptText(script);*/Object value = redisTemplate.opsForValue().get("num");//2.1 判断numm为空returnif (StringUtils.isEmpty(value)) {return;}//2,2 有值就转换成intint num = Integer.parseInt(value + "");//2.3 把redis的num加1redisTemplate.opsForValue().set("num", ++num);//2.4 释放锁//判断uuid是否相同if(uuid.equals(redisTemplate.opsForValue().get("lock"))){redisTemplate.delete("lock");}/*  lua脚本  用lua脚本,上面的if就删掉          redisTemplate.execute(objectDefaultRedisScript, Arrays.asList(lock),uuid);*/} else {try {Thread.sleep(100);testLock();} catch (InterruptedException e) {e.printStackTrace();}}}

14.4.4 四个条件

为了确保分布式锁可用,我们至少要确保锁的实现同时满足以下四个条件:

  • 互斥性。在任意时刻,只有一个客户端能持有锁。
  • 不会发生死锁。即使有一个客户端在持有锁的期间崩溃而没有主动解锁,也能保证后续其他客户端能加锁。
  • 解铃还须系铃人。加锁和解锁必须是同一个客户端,客户端自己不能把别人加的锁给解了。
  • 加锁和解锁必须具有原子性。

15 Redis6 新功能

15.1 ACL 访问控制列表

15.1.1 简介

Redis ACL是Access Control List(访问控制列表)的缩写,该功能允许根据可以执行的命令和可以访问的键来限制某些连接。
在 Redis 5版本之前,Redis安全规则只有密码控制还有通过rename来调整高危命令比如flushdb , KEYS*, shutdown等。Redis 6则提供ACL的功能对用户进行更细粒度的权限控制:

( 1)接入权限:用户名和密码
( 2)可以执行的命令
( 3)可以操作的KEY

参考官网: https://redis.io/topics/acl

15.1.2 命令

  1. 使用acl list命令展现用户权限列表
    1> 数据说明

    2> 使用acl cat命令

    • 查看添加权限指令类别
    • acl cat string/list/set/hash/hash 查看各个类别的命令

    3> 使用acl whoami命令查看当前用户

    4> 使用aclsetuser命令创建和编辑用户ACL
    ( 1 )ACL规则
    下面是有效ACL规则的列表。某些规则只是用于激活或删除标志,或对用户ACL执行给定更改的单个单词。其他规则是字符前缀,它们与命令或类别名称、键模式等连接在一起

ACL规则
类型 参数 说明
启动和禁用用户 on 激活某用户账号
off 禁用某用户账号。注意,已验证的连接仍然可以工作。如果默认用户被标记为 off ,则新连接将在未进行身份验证的情况下启动,并要求用户使用AUTH选项发送 AUTH 或HELLO,以便以某种方式进行身份验证。
权限的添加删除 +<command,> 将指令添加到用户可以调用的指令列表中
-<command,> 从用户可执行指令列表移除指令
+@<category,> 添加该类别中用户要调用的所有指合,有效类别为@admin、@set、@sortedset…等,通过调用ACLCAT命合查看完整列表。特殊类别@all表示所有命合,包括当前存在于服务器中的命合,以及将来将通过模块加载的命合。
-@<actegory,> 从用户可调用指令中移除类别
allcommands +@all的别名
nocommands -@all的别名
可操作键的添加或删除 ~<pattem,> 添加可作为用户可操作的键的模式.例如~*允许所有键

( 2 )通过命令创建新用户默认权限
acl setuser user1

在上面的示例中,我根本没有指定任何规则。如果用户不存在,这将使用just created的默认属性来创建用户。如果用户已经存在,则上面的命令将不执行任何操作。

( 3 )设置有用户名、密码、ACL权限、并启用的用户
acl setuser user2 on>password ~cached:* +get

15.2 IO 多线程

15.2.1 简介

Redis6终于支撑多线程了,告别单线程了吗?
I0 多线程其实指客户端交互部分的网络I0交互处理模块多线程,而非执行命令多线程。Redis6执行命令依然是单线程。

15.2.2 原理架构

Redis 6 加入多线程,但跟Memcached 这种从IO处理到数据访问多线程的实现模式有些差异。Redis的多线程部分只是用来处理网络数据的读写和协议解析,执行命令仍然是单线程。之所以这么设计是不想因为多线程而变得复杂,需要去控制key、lua、事务,LPUSH/LPOP等等的并发问题。整体的设计大体如下:

另外,多线程IO默认也是不开启的,需要再配置文件中配置
io-threads-do-reads yes
io-threads 4

15.3 工具支持Cluster

之前老版Redis想要搭集群需要单独安装ruby环境,Redis 5将redis-trib.rb的功能集成到redis-cli。另外官方redis-benchmark工具开始支持cluster模式了,通过多线程的方式对多个分片进行压测。

15.4 Redis新功能持续关注

Redis6新功能还有∶

  1. RESP3新的 Redis通信协议∶优化服务端与客户端之间通信
  2. Client side caching客户端缓存∶基于RESP3协议实现的客户端缓存功能。为了进一步提升缓存的性能,将客户端经常访问的数据cache到客户端。减少TCP网络交互。
  3. Proxy集群代理模式:Proxy 功能,让Cluster拥有像单实例一样的接入方式,降低大家使用cluster的门槛。不过需要注意的是代理不改变Cluster的功能限制,不支持的命令还是不会支持,比如跨slot的多Key操作。
  4. Modules API:
    Redis 6中模块API开发进展非常大,因为Redis Labs为了开发复杂的功能,从一开始就用上Redis模块。Redis 可以变成一个框架,利用Modules 来构建不同系统,而不需要从头开始写然后还要BSD许可。Redis一开始就是一个向编写各种系统开放的平台。

Redis 6 入门到入坟 详细教程 @学习笔记相关推荐

  1. Redis详细教程-学习笔记

    Redis 概述 Redis是什么? Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.K ...

  2. 【教程汇总+持续更新】Unity游戏开发从入门到入坟

    新的一年,本该在年前整理的年终总结被拖到了年后开工.去年大量时间投入在Catlike教程的翻译上,截止目前位置,教程的进度已经完全追平原作者. 去年还有一部分是断断续续的更新SLG实战教程,但遗憾的是 ...

  3. 从入门到入坟搭建FreeNAS服务器并配置NextCloud_NAS存储

    从入门到入坟搭建FreeNAS服务器并配置NextCloud 2021-01-06 23:15:38 173点赞 963收藏 100评论 创作立场声明:本文所有商品皆自费购入,第一次发表文章,若有不当 ...

  4. canvas 从入门到入坟

    文章目录 canvas 从入门到入坟 1. 概述 2. 使用场景 2.1 图表的绘制 2.2 canvas游戏 2.3 说明与作用 3. Canvas 入门 3.1 完成目标 3.2 初始化 3.3 ...

  5. 深聊MySQL,从入门到入坟之:MySQL竟然也有后悔药!!!

    MySQL后悔药,防止看铁窗 1.引言 2.5种后悔药 2.1 limit 2.2 先测试后生产 2.3 删除前,先备份 2.4 删除前,先查询 2.5 修改时, begin+commit 3. 总结 ...

  6. 初阶指针---从入门到入坟

    今天我们来见识一下c语言里让万千少年少女从入门到入坟的一道大门槛--指针 目录 1.指针是什么? 2.指针和指针类型 3.野指针 4. 指针运算 5. 指针和数组 6. 二级指针 7. 指针数组 1. ...

  7. Flink 教程 gitbook 从入门到入土(详细教程)

    Flink从入门到入土(详细教程) 和其他所有的计算框架一样,flink也有一些基础的开发步骤以及基础,核心的API,从开发步骤的角度来讲,主要分为四大部分 1.Environment Flink J ...

  8. FHQ-Treap(非旋treap/平衡树)——从入门到入坟

    作者:hsez_yyh 链接: FHQ-Treap--从入门到入坟_hsez_yyh的博客-CSDN博客 来源:湖北省黄石二中信息竞赛组        著作权归作者所有.商业转载请联系作者获得授权,非 ...

  9. 2022最新版Redis入门到精通(云课堂视频学习笔记)

    学习视频: 2022最新版Redis入门到精通 - 网易云课堂 学习笔记: 目录 一.初识Redis-认识NoSQL 1.认识NoSQL 2.认识Redis 3.安装Redis 二.Redis常见命令 ...

  10. 黑马程序员Java教程学习笔记(五)

    学习视频:https://www.bilibili.com/video/BV1Cv411372m 如侵权,请私信联系本人删除 文章目录 黑马程序员Java教程学习笔记(五) 日期时间:Date.Sim ...

最新文章

  1. JSP Cookie处理
  2. gvim配置及相关插件安装(过程详细,附图)
  3. 使用高防后,服务器还是会受到***这是为什么?
  4. yarn vite vue3.x
  5. excel文件导入hive乱码_将excel中的数据导入hive
  6. pthon-递归实战-操作文件
  7. zabbix mysql pgsql_Zabbix 5.0 监控 PostgreSQL 数据库
  8. 使用NSURLProtocol实现UIWebView的离线缓存
  9. 【数据结构和算法笔记】用c语言实现栈
  10. javaWEB总结(29):理解多个Filter代码的执行顺序
  11. 网站域名安全:泛解析处理及防护
  12. 海康威视SDK使用总结
  13. 炫龙dcpro黑苹果_毁灭者DC W650DC装黑苹果心得
  14. kms激活win10
  15. popperjs V2 之应用库 tippy.js 源码阅读
  16. 甲骨文提供非常良心的永久免费云主机空间
  17. 缓存加速-----Squid-访问控制列表日志分析反向代理
  18. 在阿里云OSS,如何更好搭建自己的云储存
  19. python round_Python round() 函数
  20. plotly绘制3D图技巧

热门文章

  1. DevExpress ASP.NET and Blazor图表编制
  2. 加来道雄 基因编辑 纳米机器人_潜力巨大:纳米材料结合CRISPR基因编辑,成功改善阿尔茨海默病症状...
  3. 来自一个IT民工的转型
  4. 郑明秋什么版本的MySQL_mysql数据库实用教程教学课件作者郑明秋代码数据库脚本代码9787568250825.docx...
  5. 安装win7时,显示缺少所需的CD/DVD驱动器设备驱动程序
  6. MFC技术内幕系列之(二)---MFC文档视图结构内幕
  7. FMI飞马网【线下】FMI2017人工智能前沿应用与人才发展论坛!
  8. 笔记:《高效能人士的七个习惯》第五章 习惯三 要事第一——自我管理的原则
  9. 测试基础-测试用例编写
  10. SQL语句中对时间字段进行区间查询