Redis从入门到高可用--初识

Redis从入门到高可用–初识

一、redis是什么

1、开源

2、基于键值对的存储服务系统(K-V)

3、多种数据结构(5种)

4、高性能、功能丰富

二、redis特性回顾

1、速度快;

10万OPS,其将数据存在内存(主因),使用c语言开发的,单线程模型;

2、持久化

Redis持久化分为:RDB和AOF

持久化简单说就是断电不丢数据;Redis所有的数据保存在内存,对数据的更新将异步的保存到磁盘;

3、多种数据结构

String、hash 、Set、List、Sort Set五种数据结构;

续:Bitmaps位图、HyperLogLog、GEO

4、支持多种编程语言

Java、Python、PHP、Ruby、Lua等;

5、功能丰富

发布订阅、Lua脚本、事务、Pipeline

6、“简单”

其实并不简单啊

不依赖外部库

单线程模型

7、主从复制

主从服务器

8、高可用、分布式

Redis-Sentinel支持高可用

Redis-Cluster支持分布式

三、redis单机版安装

1、Redis安装(Linux)

wget http://download.redis.io/releases/redis-3.0.3.tar.gz

tar -xzf redis-3.0.7.tar.gz

ln -s redis-3.0.7 redis

cd redis

make && make install

2、可执行文件说明

3、三种启动方法


最简单启动

执行:redis-server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@promote redis]# redis-server 
2345:C 06 Jun 08:14:25.135 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2345:M 06 Jun 08:14:25.135 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 2345
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

2345:M 06 Jun 08:14:25.148 # Server started, Redis version 3.0.3
2345:M 06 Jun 08:14:25.148 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2345:M 06 Jun 08:14:25.149 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2345:M 06 Jun 08:14:25.149 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2345:M 06 Jun 08:14:25.149 * The server is now ready to accept connections on port 6379

此时使用默认配置启动


动态参数启动

执行:redis-server –port 6380

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@promote ~]# redis-server --port 6380
2405:M 06 Jun 08:16:50.720 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6380
| `-._ `._ / _.-' | PID: 2405
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

2405:M 06 Jun 08:16:50.731 # Server started, Redis version 3.0.3
2405:M 06 Jun 08:16:50.731 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2405:M 06 Jun 08:16:50.731 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2405:M 06 Jun 08:16:50.732 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2405:M 06 Jun 08:16:50.732 * The server is now ready to accept connections on port 6380

配置文件启动

配置redis-6382.conf 文件修改后如下:其在目录
/root/redis/conf文件夹下,

1
2
3
4
5
daemonize yes
port 6382
#工作目录
dir "/root/redis/data"
logfile "6382.log"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@promote redis]# redis-server config/redis-6382.conf 
[root@promote redis]# ps -ef|grep redis
root 8636 1 0 08:53 ? 00:00:00 redis-server *:6382
root 8640 2412 0 08:53 pts/3 00:00:00 grep --color=auto redis

[root@promote redis]# cd data/
[root@promote data]# ll
total 4
-rw-r--r--. 1 root root 2252 Jun 6 08:53 6382.log
[root@promote data]# cat 6382.log
8636:M 06 Jun 08:53:12.322 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.3 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6382
| `-._ `._ / _.-' | PID: 8636
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

8636:M 06 Jun 08:53:12.325 # Server started, Redis version 3.0.3
8636:M 06 Jun 08:53:12.325 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
8636:M 06 Jun 08:53:12.325 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
8636:M 06 Jun 08:53:12.325 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8636:M 06 Jun 08:53:12.325 * The server is now ready to accept connections on port 6382
[root@promote data]# ll
total 4
-rw-r--r--. 1 root root 2252 Jun 6 08:53 6382.log
[root@promote data]#

4、简单的客户端连接

redis-cli -h 127.0.0.1 -p 6379
1
2
3
4
5
6
7
8
9
10
[root@promote ~]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
[root@promote ~]# redis-cli
127.0.0.1:6379> set hello ww
OK
127.0.0.1:6379> get hello
"ww"
127.0.0.1:6379>
1
2
[root@promote ~]# redis-cli -p 6380
127.0.0.1:6380>

四、redis典型的使用场景

1、缓存系统

2、计数器

Incr()方法实现

比如:微博的转发数评论数

3、消息队列系统

简单的消息队列;
Producer、Consumer

ActiveMQ

4、排行榜

Sort Set数据结构实现

5、社交网络

粉丝数、点赞数

6、实时系统

文章目录
  1. 1. 一、redis是什么
  2. 2. 二、redis特性回顾
    1. 2.0.1. 1、速度快;
    2. 2.0.2. 2、持久化
    3. 2.0.3. 3、多种数据结构
    4. 2.0.4. 4、支持多种编程语言
    5. 2.0.5. 5、功能丰富
    6. 2.0.6. 6、“简单”
    7. 2.0.7. 7、主从复制
    8. 2.0.8. 8、高可用、分布式
  • 3. 三、redis单机版安装
    1. 3.0.1. 1、Redis安装(Linux)
    2. 3.0.2. 2、可执行文件说明
    3. 3.0.3. 3、三种启动方法
      1. 3.0.3.1. 最简单启动
      2. 3.0.3.2. 动态参数启动
      3. 3.0.3.3. 配置文件启动
    4. 3.0.4. 4、简单的客户端连接
  • 4. 四、redis典型的使用场景
    1. 4.0.1. 1、缓存系统
    2. 4.0.2. 2、计数器
    3. 4.0.3. 3、消息队列系统
    4. 4.0.4. 4、排行榜
    5. 4.0.5. 5、社交网络
    6. 4.0.6. 6、实时系统