Redis3.2 learning record

nosql

Features: access to large, high concurrency, high availability, massive data

redis3.2
role: to reduce the pressure relational database query
Installation:
unzip the windows can be used to start the service such as: redis-server redis-config file
under linux:
software compression package under / Home / Soft /
tar -zxvf source - C / usr / local / decompression to / usr / local / at
entering the extracted directory, gcc compiler use (yum -y install gcc installation). Generate an executable program.
Compile make, make install (you can not, the main role is to add to redis environment variable)
into the src, start the service: ./ redis-server, this is the way to start the front desk, ctrl + c to exit
backstage start ./redis- server & (can also be set in redis.conf in daemonize is yes)
using ps see the process: ps -ef | grep Redis

Start the client: ./ redis-cli

Close redis:

1. Switch to the redis / src / directory, perform ./redis-cli the shutdown (safer, recommended in this way, primarily to
accomplish the data, performing closed)
2.kill PID or kill -9 pid ( not recommended)
3. exit redis-cli you can enter exit

Client:
1. Client command line, directly connected to the machine: ./ redis-cli
specified IP and port: ./ redis-cli -H 127.0.0.1 -p 6379
2. graphical client. redis desktop manager (this tool requires the machine have c compiler library), remote access,
pay attention to the network access security policy, first turn off the linux firewall
centos6.5 off the firewall command
1. permanently take effect, reboot recovery will not
open: chkconfig iptables on
close: chkconfig iptables off

2. immediate effect, after the restart recovery
open: service iptables start
shut down: service iptables stop


Second, modify redis.conf profile :( mainly redis there are security measures, only the default native access)
the bind comment out this line ip ip binding
protected-mode yes no mode to protect the

Modify the configuration file do first backup: cp redis.conf bak_redis.conf
use vim to modify,
vim redis.conf

Use the command mode / bind; bind keyword search.

Note: After modifying, when we start to specify that we modify the configuration file ;
./redis-server ../redis.conf &

View firewall: systemctl status firewalld (Active inactive closed)

有一个java写的客户端工具:redisclient-win32x86_64.2.0.jar(直接使用,不用依赖啥C++类库的
)

编程客户端:Jedis

redis的数据持久化,内部执行


操作基本命令
1.ping 返回PONG,表示当前服务可用
2.dbsize 查看当前数据库中key的数目(默认访问第0个库)
3.select db 切换数据库,如:select 5 表示使用第5 个库
4.flushdb 删除当前库的所有数据
5.exit或quit 退出客户端
操作key命令:
1.keys pattern 查找所有符合模式pattern的key,pattern使用通配符(在生产环境中,
少用*,采用 通配符查找,单线程查找,通配符(*所有、?单个字
符、))
2.exists key [key...] 判断key是否存在 返回整数数量。
3.expire key seconds 设置key的超时时间,如果key不存在,则设置无效,返回值0
4.ttl key 查看key剩余的生存时间(秒为单位),返回值-1:永久存在;-2不存在
;>0的值, 代表剩余生存时间
5.type key 查看key所存储值得数据类型
6.del key [key...] 删除指定的key,不存在的key忽略
操作value命令:

五种数据类型
a.string 包括二进制数据,序列化后的数据,最大512M
b.hash 特别适合用于存储对象(分布式环境中解决session会话不一致,采用redis服务器
进行共享,使用hash存储当前登录的用户信息)
c.list 简单的字符串列表
d.set 无序集合,唯一,不能重复
e.zset 有序集合,经过排序的集合类型

数据类型操作命令
A.string基本命令:
set,get,
incr(理解成i++,incr key[如果key不存在,则先创建+1] 值加1并返回,原子操作,可用
于全局的计数器)
decr 减一 (在线用户数,登陆+1,退出-1)
append key value 追加value,返回追加后的总字符数,如果追加的key不存在,则相对
set
B.string常用命令:
  strlen key 长度
  getrange key start end (相当substring(),)
  setrange key offset value 用value覆盖(相等replace()。)
  mset 创建多个key,value
  mget 获取多个key值
A.hash基本命令:
  hset key field value
  hget key field
  hmset key [field.. value..]
  hmget key field[field...]
  hgetall key 获取哈希表key中所有的域和值
  hdel key field[field] 删除key中的field
B.hash常用命令:
  hkeys key 获取key中的所有field
  hvals key 获取key中的所有value
  hexists key field 查看给定field是否存在
A.list基本命令:
头部操作:以l开始,尾部操作:以r开始
  lpush key value[value...] 存储顺序和操作顺序相反
  rpush key value[value...] 存储顺序和操作顺序一致
  lrange key start stop 获取列表key中指定区间内的元素,包含start stop
  lindex key index 获取指定下标index的成员
  llen key 获取列表key的长度
B.list常用命令:
  lrem key count value 根据参数count的值,删除列表中雨参数value相等的元素,count
>0,从列表的左侧向右侧删除,count <0,则从右向左,count =0,则删除与value相等的所有值,返回
删除元素的个数
  lset key index value 将列表key下标为index的元素的值设置为value
  linsert key BEFORE|ALFTER pivot value 将值value插入到列表key 当中位于值povot之
前或之后的位置
A.set基本命令:
  sadd key member[member...] 添加成员,重复的成员无法添加的
  smembers key 获取集合key中的所有成员元素,key不存在,则返回空集合
  sismember key member 判断当前成员member在当前集合key中是否存在
  scard key 获取集合里面的成员个数
  srem key member[member...] 删除集合key中的一个或多个成员

B.set常用命令:
  srandmember key [count] 随机返回集合中一(count)个元素,不删除元素,count默认为1
,count正数,返回的集合元素各不相同,count负数:则允许重复
  spop key [count] 随机从集合中删除一(count)个元素,并返回删除的元素,count默认为1

A.zset基本命令:
  zadd key score member[score member...] 将一个或多个member元素及其score值加入到
有序集合key中,如果member存在集合中,则更新值;socre可以是整数或浮点数
  zrange key start stop[WITHSCORES] 查询有序集合,指定区间的内的元素,默认不带分
数socres
  zrem key member[member...] 删除指定成员的member
  zcard key 统计成员key的个数

B.zset常用命令:
  zrangebyscore key min max [withscores] [LIMT offset count] 获取有序集key中,所
有score值介于min和max之间的成员,limit用来限制返回结果的数量和区间,withscores显示score
和value


高级部分:
A.redis事务(与写代码有关系,其他三项服务器设置)
transaction,一组命令的集合,保证至少有两条及以上命令被正常执行。
1.multi 事务的开始标志,当前还未执行命令
2.exec 执行所所有事务块内的命令
3.discard 取消事务
事务的执行过程:首先开启事务multi,其次向事务队列中加入命令,最后执行事务提交exec
redis事务是没有回滚的,只保证性能
4.watch key [key...] 监视当前key是否被其他事务改变
watch机制:主动放弃事务。这种方式避免了资源竞争,解决数据安全问题
对比sql事务中,算是乐观锁机制
5.unwatch key [key...] 取消监视
B.持久化
两种方式:
1.RDB(redis database)方式:在指定时间间隔内将内存中的数据快照写入到磁盘中(默认
是dump.rdb),保存数据是在单独进程中写文件,默认开启的。
实现:redis.conf中配置
  save: 配置执行RDB生成快照文件的时间策略
  dbfilename:设置RDB的文件名,默认文件为dump.rdb
  dir:指定RDB文件的存储位置,默认是./当前目录
优点:由于存储的是数据快照文件,恢复数据很方便,也比较快
缺点:会丢失最后一次快照以后更改的数据。由于经常操作磁盘,RDB会分出一个子进程,
如果redis数据很大,子进程占用比较多的时间。
3.AOF(Append-only File)方式:如果你怕数据丢失,可以采用AOP方式,即redi每次接
收到一条改变数据的命令时,它将把该命令写到一个AOF文件中(只记录写操作,读操作不记录),
当redis重启时,它通过执行AOF文件中所有的命令来恢复数据。
实现:redis.conf中配置
  appendonly:默认是no,改成yes即可开启aof持久化
  appendfilename:指定AOF文件名,默认文件名为appendonly.aof
  dir:指定RDB和AOF文件存放的命令,默认是./
  appendfsync:配置aof文件写命令数据的策略;
    no:不主动进行同步操作,而是完全交由操作系统来做(即每30s一次),
比较快但不是很安全
    always:每次执行写入都会执行同步,慢一些但比较安全
    everysec:每秒执行一次同步操作,比较平衡,介于速度和安全之间。默
认项
  auto-aof-rewrite-min-size:允许重写的最小AOF文件大小,默认是64M.当文件大
于64M时,开始自动整理aof文件,去掉无用的操作命令。

C.主从复制
高可用的解决方案,集群(一种服务器的网络结构)
1.主从复制--读写分离原则
两种实现方式:1.修改配置文件,启动时,指定主从服务器。(实际开发中使用)
2.命令行中指定master-slave之间的关系。
修改配置文件,分别配置master-slave的conf文件
  master:redis6380.conf
    include /usr/local/redis-3.2.10/redis.conf
    daemonize yes
    port 6380
    pidfile /var/run/redis_6380.pid
    logfile 6380.log
    dbfilename dump6380.rdb
  slave:redis6382.conf
    include /usr/local/redis-3.2.10/redis.conf
    daemonize yes
    port 6382
    pidfile /var/run/redis_6382.pid
    logfile 6382.log
    dbfilename dump6382.rdb
    slaveof 127.0.0.1 6380
  slave:redis6384.conf
    include /usr/local/redis-3.2.10/redis.conf
    daemonize yes
    port 6384
    pidfile /var/run/redis_6384.pid
    logfile 6384.log
    dbfilename dump6384.rdb
    slaveof 127.0.0.1 6380
info replication 查看启动的master-slave信息关系
问题:容灾处理
A.Master6380突然停止(模拟挂掉)
将slave 6382升级为master,salve 6384新挂载到新master上
命令:slaveof no one表示将当前从服务器升级为master;
slaveof host port改成成新的从服务器slave
B.把突然挂掉的服务修好了,重新添加到现在的主从关系中。
重新启动6380
执行salveof 127.0.0.1 6382
+++++++++++++++++++上面出了故障,需要人为参与,为使自动,产生哨兵模式++++++++++++
2.高可用Sentine哨兵
    监控--主从服务器是否正常工作
    提醒--通过sentinel会通知管理员或其他应用程序
    自动故障转移--监控的主redis不能正常工作,sentinel会开始进行故障迁移操作
,重新分配主从redis

哨兵:奇数个,至少三个,采取投票方式决定。默认端口26379
哨兵监控主redis,采用心跳机制,ping根据响应结果判断redis运行状态.
哨兵是独立的程序,之间相互独立的
配置哨兵配置文件:
主要修改两个位置:
port 26380
sentinel monitor mymaster 127.0.0.1 6380 2
格式:sentinel monitor <name> <masterIP> <masterPort> <Quorum投票数>

启动哨兵模式后,自动化处理故障的主从关系。

D.安全设置
1.设置密码

2.绑定ip

修改默认端口

E.Jedis操作Redis
redis是单线程,存在线程不安全问题,往往与common-pool一起使用,创建连接池。获取
jedis对象进行操作。


F.redis中有关技术问题:
1.缓存穿透:
在高并发条件下,会导致无法从缓存中查询到数据,直接从数据库查询,请求量
过大,这样会导致数据库系统压力很大。这种情况下,就是缓存穿透。
解决方案:
采用多线程方式解决,使用锁或关键字synchronized,双重检测锁进行。
2.缓存雪崩:
当缓存服务器重启或者大量缓存集中在某一个时间段失效,这样在失效的时候,
会给后端系统带来很大压力。导致系统崩溃。
  1:在缓存失效后,通过加锁或者队列来控制读数据库写缓存的线程数量。比如对
某个key只允许一个线程查询数据和写缓存,其他线程等待。

  2:做二级缓存,A1为原始缓存,A2为拷贝缓存,A1失效时,可以访问A2,A1缓存
失效时间设置为短期,A2设置为长期

  3:不同的key,设置不同的过期时间,让缓存失效的时间点尽量均匀。

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ysq0908/p/10993480.html