企业架构队列缓存中间件分布式redis:
一直想学习下这块的。今天总算学到了,好好把redis的这块内容理解下。
1)能够描述Redis作用及其业务适用场景 ;
2)能够安装配置启动Redis;
3)能够使用命令行客户端简单操作Redis;
4)能够实现操作基本数据类型;
5)能够理解描述Redis数据持久化机制;数据可以保存到磁盘中了。
6)能够操作安装php的redis扩展;
7)能够操作和实现Redis的主从模式;
facebook用memcached,其他公司用Redis比较多。这两款软件都是使用的比较多的缓存中间件。
业务背景:
模拟运维设计方案:
根据以上业务需求,准备加入Redis缓存中间件服务器,可以使用到Redis更加丰富的功能。
在商城业务中实现:
1)session共享存储到Redis;
2)Openresty(Nginx+lua)动态限制IP访问。做一个动态防火墙,动态的机制。
Redis介绍:
介绍:
NoSQL 非关系数据库key->value键值对。
(memcache、Redis、mongoDB)
这两天把这个学完,然后再看下mysql数据库。
Redis是Remote Dictionary Server(远程数据服务器)的缩写。
由意大利人antire开发的一款内存高速缓存数据库。
该软件使用C语言编写,它的数据模型为Key-Value。
它支持丰富的数据结构,比如String(字符串)、 list(双向列表)、Hash(哈希)、Set(集合)、Sorted set(zset 有序集合)。
可持久化(保存数据到磁盘中),保证数据安全性。
Redis业务使用场景:
①[Sort Set]排行榜应用,取top n操作,例如sina微博热门话题(点击量特别高,存到Redis合适。)
②[List]获得最新N个数据 或 某个分类的最新数据
③[String]计数器应用(memcache)
④[Set]sns(social network site)获得共同好友
⑤[Set]防攻击系统(ip判断)黑白名单等等
对比memcached:
1)Redis不仅仅支持简单的K/V类型的数据,同时还提供list、set、zset、hash等数据结构的存储;
2)Redis支持master-slave(主-从模式)的应用;
3)Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
4)Redis单个value存储String的最大限制是512MB,memcached只能保存1MB的数据。
5)Redis是单核的,memcached是多核的。
由于redis只能使用单核,而memcached可以使用多核,所以在比较上,平均每一个核上redis在储存小数据时比memcached性能更高。而却100K以上数据中,memcached性能要高于redis,虽然redis最近也在储存大数据的性能上进行优化,但是比起memcached还是有点逊色。结论是无论你使用那个,每秒处理请求的次数都不会成为瓶颈。
你需要关注内存使用率。对于key-vlaue这样简单的数据储存,memcached的内存使用率更高,如果采用hash结构,redis的内存使用率会更高,当然这都依赖于具体的应用场景。
公司使用Redis或memcached,我们运维的工作就达到了。
大概了解。
安装与配置:
官方网址: https://redis.io
github: https://github.com/redis/redis
上传软件到目录中:
redis-4.3.0.tgz php扩展 server01和server03安装
redis-5.0.5.tar.gz redis软件 server08安装
解压编译安装:
- shell > tar xvf redis-5.0.5.tar.gz
- shell > cd redis-5.0.5
查找github上安装方法:

make PREFIX=/usr/locl/redis install
使用文件:/usr/local/redis/bin
redis-cli 命令行客户端
redis-server 服务器端
测试启动:这个只是测试启动,不是后台启动。
./redis-server
- [root@server08 bin]# ll
- total 32736
- -rwxr-xr-x 1 root root 4366568 Sep 15 17:25 redis-benchmark
- -rwxr-xr-x 1 root root 8111808 Sep 15 17:25 redis-check-aof
- -rwxr-xr-x 1 root root 8111808 Sep 15 17:25 redis-check-rdb
- -rwxr-xr-x 1 root root 4806792 Sep 15 17:25 redis-cli
- lrwxrwxrwx 1 root root 12 Sep 15 17:25 redis-sentinel -> redis-server
- -rwxr-xr-x 1 root root 8111808 Sep 15 17:25 redis-server
- [root@server08 bin]# ./redis-server
- 7606:C 15 Sep 2023 17:26:11.195 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
- 7606:C 15 Sep 2023 17:26:11.195 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7606, just started
- 7606:C 15 Sep 2023 17:26:11.195 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
- 7606:M 15 Sep 2023 17:26:11.195 * Increased maximum number of open files to 10032 (it was originally set to 1024).
- _._
- _.-``__ ''-._
- _.-`` `. `_. ''-._ Redis 5.0.5 (00000000/0) 64 bit
- .-`` .-```. ```\/ _.,_ ''-._
- ( ' , .-` | `, ) Running in standalone mode
- |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
- | `-._ `._ / _.-' | PID: 7606
- `-._ `-._ `-./ _.-' _.-'
- |`-._`-._ `-.__.-' _.-'_.-'|
- | `-._`-._ _.-'_.-' | http://redis.io
- `-._ `-._`-.__.-'_.-' _.-'
- |`-._`-._ `-.__.-' _.-'_.-'|
- | `-._`-._ _.-'_.-' |
- `-._ `-._`-.__.-'_.-' _.-'
- `-._ `-.__.-' _.-'
- `-._ _.-'
- `-.__.-'
-
- 7606:M 15 Sep 2023 17:26:11.196 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
- 7606:M 15 Sep 2023 17:26:11.196 # Server initialized
- 7606:M 15 Sep 2023 17:26:11.196 # 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.
- 7606:M 15 Sep 2023 17:26:11.196 # 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.
- 7606:M 15 Sep 2023 17:26:11.196 * Ready to accept connections
检查redis的进程:
- [root@server08 ~]# ps aux |grep redis
- root 7614 0.0 0.4 153996 8120 pts/1 Sl+ 17:38 0:00 ./redis-server *:6379
- root 7619 0.0 0.0 112812 972 pts/0 S+ 17:39 0:00 grep --color=auto redis
可以看到端口是6379。
Redis后台启动服务:
复制配置文件到软件目录下:
将Redis.conf从/root/soft/ redis-5.0.5复制到/usr/local/redis/bin目录下面:


然后再启动:
- [root@server08 bin]# ./redis-server ./redis.conf
- 7642:C 15 Sep 2023 18:23:50.484 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
- 7642:C 15 Sep 2023 18:23:50.484 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7642, just started
- 7642:C 15 Sep 2023 18:23:50.484 # Configuration loaded
再检查下进程:
- [root@server08 bin]# ps aux |grep redis
- root 7614 0.1 0.4 153996 8120 pts/1 Sl+ 17:38 0:02 ./redis-server *:6379
- root 7645 0.0 0.0 112812 972 pts/0 R+ 18:24 0:00 grep --color=auto redis
命令行客户端简单使用:
telnet可以连接redis,没有本身redis-cli更加好用。
简单数据操作和查看操作语法帮助:
- [root@server08 bin]# ./redis-cli
- 127.0.0.1:6379> get name
- "devops"
- 127.0.0.1:6379> help get
-
- GET key
- summary: Get the value of a key
- since: 1.0.0
- group: string
-
- 127.0.0.1:6379>
系统状态信息:
- 127.0.0.1:6379> info
- # Server
- redis_version:5.0.5
- redis_git_sha1:00000000
- redis_git_dirty:0
- redis_build_id:64d379e5a99fe9ba
- redis_mode:standalone
- os:Linux 3.10.0-1160.95.1.el7.x86_64 x86_64
- arch_bits:64
- multiplexing_api:epoll
- atomicvar_api:atomic-builtin
- gcc_version:4.8.5
- process_id:7614
- run_id:f2488e23b691df175c11bc55299b8153fe9642e3
- tcp_port:6379
- uptime_in_seconds:3043
- uptime_in_days:0
- hz:10
- configured_hz:10
- lru_clock:274960
- executable:/usr/local/redis/bin/./redis-server
- config_file:
-
- # Clients
- connected_clients:1
- client_recent_max_input_buffer:2
- client_recent_max_output_buffer:0
- blocked_clients:0
-
- # Memory
- used_memory:854240
- used_memory_human:834.22K
- used_memory_rss:13312000
- used_memory_rss_human:12.70M
- used_memory_peak:854240
- used_memory_peak_human:834.22K
- used_memory_peak_perc:100.00%
- used_memory_overhead:841014
- used_memory_startup:791248
- used_memory_dataset:13226
- used_memory_dataset_perc:21.00%
- allocator_allocated:1419664
- allocator_active:1708032
- allocator_resident:11173888
- total_system_memory:2076344320
- total_system_memory_human:1.93G
- used_memory_lua:37888
- used_memory_lua_human:37.00K
- used_memory_scripts:0
- used_memory_scripts_human:0B
- number_of_cached_scripts:0
- maxmemory:0
- maxmemory_human:0B
- maxmemory_policy:noeviction
- allocator_frag_ratio:1.20
- allocator_frag_bytes:288368
- allocator_rss_ratio:6.54
- allocator_rss_bytes:9465856
- rss_overhead_ratio:1.19
- rss_overhead_bytes:2138112
- mem_fragmentation_ratio:16.39
- mem_fragmentation_bytes:12499744
- mem_not_counted_for_evict:0
- mem_replication_backlog:0
- mem_clients_slaves:0
- mem_clients_normal:49694
- mem_aof_buffer:0
- mem_allocator:jemalloc-5.1.0
- active_defrag_running:0
- lazyfree_pending_objects:0
-
- # Persistence
- loading:0
- rdb_changes_since_last_save:1
- rdb_bgsave_in_progress:0
- rdb_last_save_time:1694770733
- rdb_last_bgsave_status:ok
- rdb_last_bgsave_time_sec:-1
- rdb_current_bgsave_time_sec:-1
- rdb_last_cow_size:0
- aof_enabled:0
- aof_rewrite_in_progress:0
- aof_rewrite_scheduled:0
- aof_last_rewrite_time_sec:-1
- aof_current_rewrite_time_sec:-1
- aof_last_bgrewrite_status:ok
- aof_last_write_status:ok
- aof_last_cow_size:0
-
- # Stats
- total_connections_received:3
- total_commands_processed:7
- instantaneous_ops_per_sec:0
- total_net_input_bytes:175
- total_net_output_bytes:29617
- instantaneous_input_kbps:0.00
- instantaneous_output_kbps:0.00
- rejected_connections:0
- sync_full:0
- sync_partial_ok:0
- sync_partial_err:0
- expired_keys:0
- expired_stale_perc:0.00
- expired_time_cap_reached_count:0
- evicted_keys:0
- keyspace_hits:2
- keyspace_misses:0
- pubsub_channels:0
- pubsub_patterns:0
- latest_fork_usec:0
- migrate_cached_sockets:0
- slave_expires_tracked_keys:0
- active_defrag_hits:0
- active_defrag_misses:0
- active_defrag_key_hits:0
- active_defrag_key_misses:0
-
- # Replication
- role:master
- connected_slaves:0
- master_replid:2eef306e4bb9be70779e4e52a1b66dc0d303d75f
- master_replid2:0000000000000000000000000000000000000000
- master_repl_offset:0
- second_repl_offset:-1
- repl_backlog_active:0
- repl_backlog_size:1048576
- repl_backlog_first_byte_offset:0
- repl_backlog_histlen:0
-
- # CPU
- used_cpu_sys:1.742750
- used_cpu_user:1.361523
- used_cpu_sys_children:0.000000
- used_cpu_user_children:0.000000
-
- # Cluster
- cluster_enabled:0
-
- # Keyspace
- db0:keys=1,expires=0,avg_ttl=0
配置环境变量:
- [root@server08 bin]# tail -5 /etc/profile
- unset i
- unset -f pathmunge
- PATH=/usr/local/java/bin:$PATH
- PATH=/usr/local/mysql/bin:$PATH
- PATH=/usr/local/redis/bin:$PATH
source /etc/profile