一些常用的小玩意之redis

1. 什么是Redis?

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010315日起,Redis的开发工作由VMware主持。NoSQL数据库的一种。

1.1. Redis的安装

Redisc语言开发的。

安装redis需要c语言的编译环境。如果没有gcc需要在线安装。yum install gcc-c++

安装步骤:

第一步:redis的源码包上传到linux系统。 tar -zxvf包名

第二步:解压缩redis

第三步:编译。进入redis源码目录。make

第四步:安装。make install PREFIX=/usr/local/redis

PREFIX参数指定redis的安装目录。一般软件安装到/usr目录下

1.2. 连接redis

1.2.1. redis的启动:

前端启动:在redis的安装目录下直接启动redis-server

[root@localhost bin]# ./redis-server

后台启动:

/root/redis-3.0.0/redis.conf复制到/usr/local/redis/bin目录下

[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/

修改配置文件:

 

[root@localhost bin]# ./redis-server redis.conf

查看redis进程:

[root@localhost bin]# ps aux|grep redis

root      5190  0.1  0.3  33936  1712 ?        Ssl  18:23   0:00 ./redis-server *:6379    

root      5196  0.0  0.1   4356   728 pts/0    S+   18:24   0:00 grep redis

[root@localhost bin]#

PS: ~ 当前用户的home路径   cd/ cd ~

1.2.2. Redis-cli

[root@localhost bin]# ./redis-cli

默认连接localhost运行在6379端口的redis服务。

[root@localhost bin]# ./redis-cli -h 192.168.25.153 -p 6379

-h:连接的服务器的地址

-p:服务的端口号

关闭redis[root@localhost bin]# ./redis-cli shutdown

可以测试一下:输入ping  响应pong,说明连接正常

1.3. Redis五种数据类型(string,hash,set,listSortedSet)

Stringkey-value(做缓存)

Redis中所有的数据都是字符串。命令不区分大小写,key是区分大小写的。Redis是单线程的。Redis中不适合保存内容大的数据。

练习开始:

字符串  getset

   set str1   abc

   get str1

   keys *   // 查找当前数据库中所有的key

   incr key1  // key1刚开始不存在,随后自增+1  变为1

   decr key1  // -1  可以为负数

   get  key1

   set  key2 abc

   incr key2  // 报错,因为无法+1,或者无法计算

   del key2  // 删除

getset

incr:加一(生成id

Decr:减一

Hashkey-fields-values(做缓存)

相当于一个key对于一个mapmap中还有key-value

使用hashkey进行归类。

Hset:向hash中添加内容

Hget:从hash中取内容

练习:

Hset hash1 field1 1

Hget hash1 field1

Hset hash1 field2 2

Hset hash1 field3 a

Hset hash1 field4 b

Keys *

Hkeys hash1  //查看hash1中的字段

Hvals hash1   //查看hash1中的value

Hgetall hash1  //即可查看hash1的值也可以看字段

Hdel hash1 field3 // 删除某个字段

List:有顺序可重复

可以左添加,也可以右添加

192.168.25.153:6379> lpush list1 a b c d

(integer) 4

192.168.25.153:6379> lrange list1 0 -1

1) "d"

2) "c"

3) "b"

4) "a"

192.168.25.153:6379> rpush list1 1 2 3 4

(integer) 8

192.168.25.153:6379> lrange list1 0 -1

1) "d"

2) "c"

3) "b"

4) "a"

5) "1"

6) "2"

7) "3"

8) "4"

192.168.25.153:6379>

192.168.25.153:6379> lpop list1

"d"

192.168.25.153:6379> lrange list1 0 -1

1) "c"

2) "b"

3) "a"

4) "1"

5) "2"

6) "3"

7) "4"

192.168.25.153:6379> rpop list1

"4"

192.168.25.153:6379> lrange list1 0 -1

1) "c"

2) "b"

3) "a"

4) "1"

5) "2"

6) "3"

192.168.25.153:6379>

Set:元素无顺序,不能重复

192.168.25.153:6379> sadd set1 a b c c c d  

(integer) 4

192.168.25.153:6379> smembers set1  //查看元素

1) "b"

2) "c"

3) "d"

4) "a"

192.168.25.153:6379> srem set1 a   // 删除

(integer) 1

192.168.25.153:6379> smembers set1

1) "b"

2) "c"

3) "d"

Spop set1  可以取出来一个值

192.168.25.153:6379>

还有集合运算命令,自学。

Sadd seta  a b c d e

Sadd setb  c d e f g

Sdiff seta setb    // a中独有元素

Sdiff setb seta    // b中独有元素

Sinter seta setb   //交集

Sunion seta setb  //并集

SortedSetzset):有顺序,不能重复兼备listset的优点

192.168.25.153:6379> zadd zset1 2 a 5 b 1 c 6 d

(integer) 4

192.168.25.153:6379> zrange zset1 0 -1

1) "c"

2) "a"

3) "b"

4) "d"

192.168.25.153:6379> zrem zset1 a   //删除元素

(integer) 1

192.168.25.153:6379> zrange zset1 0 -1 // 查看所有数据

1) "c"

2) "b"

3) "d"

192.168.25.153:6379> zrevrange zset1 0 -1 // 默认升序排列,该命令可以降序排列

1) "d"

2) "b"

3) "c"

192.168.25.153:6379> zrange zset1 0 -1 withscores //带分数显示出来

1) "c"

2) "1"

3) "b"

4) "5"

5) "d"

6) "6"

192.168.25.153:6379> zrevrange zset1 0 -1 withscores //带分数显示出来

1) "d"

2) "6"

3) "b"

4) "5"

5) "c"

6) "1"

192.168.25.153:6379>

1.4. Key命令

设置key的过期时间。

Expire key second:设置key的过期时间

Ttl key:查看key的有效期

Persist key:清除key的过期时间。Key持久化。

-1 代表是持久化的  -2代表不存在  是正数表示正在倒计时

192.168.25.153:6379> expire Hello 100

(integer) 1

192.168.25.153:6379> ttl Hello

(integer) 77

练习:

Expire key1 100

Ttl key1

Ttl key1

Ttl keyb  //-1 永久保存

Ttl aaaa  //-2  不存在

Expire key1 100 //时间可以重置

Persist key1 //清除过期时间

2. Redis的持久化方案

Redis的所有数据都是保存到内存中的。

rdb:快照形式,定期把内存中当前时刻的数据保存到磁盘。Redis默认支持的持久化方案。

aof形式:append only file。把所有对redis数据库操作的命令,增删改操作的命令。保存到文件中。数据库恢复时把所有的命令执行一遍即可(不包括查询)。

dump.rdb  可能会丢数据   xxxx.aof   相比较rdb而言,数据保存更加完整。

redis.conf配置文件中配置。

Rdb

 

Aof的配置:(默认不开启)

 

两种持久化方案同时开启使用aof文件来恢复数据库。

cat appendonly.aof

3. Redis集群的搭建

3.1. redis-cluster架构图



redis-cluster投票:容错


 


架构细节:

(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.

(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.

(3)客户端与redis节点直连,不需要中间proxy.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可

(4)redis-cluster把所有的物理节点映射到[0-16383]slot,cluster负责维护node<->slot<->value

Redis集群中内置了 16384 个哈希槽,当需要在 Redis集群中放置一个 key-value时,redis先对 key使用 crc16算法算出一个结果,然后把结果对 16384求余数,这样每个 key都会对应一个编号在 0-16383之间的哈希槽,redis会根据节点数量大致均等的将哈希槽映射到不同的节点

 

3.2. Redis集群的搭建

Redis集群中至少应该有三个节点。要保证集群的高可用,需要每个节点有一个备份机。

Redis集群至少需要6台服务器。

搭建伪分布式。可以使用一台虚拟机运行6redis实例。需要修改redis的端口号7001-7006

3.2.1. 集群搭建环境

1、使用ruby脚本搭建集群。需要ruby的运行环境。

安装ruby

yum install ruby

yum install rubygems

2、安装ruby脚本运行使用的包。

[root@localhost ~]# gem install redis-3.0.0.gem

Successfully installed redis-3.0.0

1 gem installed

Installing ri documentation for redis-3.0.0...

Installing RDoc documentation for redis-3.0.0...

[root@localhost ~]#

[root@localhost ~]# cd redis-3.0.0/src

[root@localhost src]# ll *.rb

-rwxrwxr-x. 1 root root 48141 Apr  1  2015 redis-trib.rb

 

3.2.2. 搭建步骤

需要6redis服务器。搭建伪分布式。

需要6redis实例。

需要运行在不同的端口7001-7006

第一步:创建6redis实例,每个实例运行在不同的端口。需要修改redis.conf配置文件。配置文件中还需要把cluster-enabled yes前的注释去掉。

 

第二步:启动每个redis实例。

第三步:使用ruby脚本搭建集群。

./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006

创建关闭集群的脚本:

[root@localhost redis-cluster]# vim shutdow-all.sh

redis01/redis-cli -p 7001 shutdown

redis01/redis-cli -p 7002 shutdown

redis01/redis-cli -p 7003 shutdown

redis01/redis-cli -p 7004 shutdown

redis01/redis-cli -p 7005 shutdown

redis01/redis-cli -p 7006 shutdown

[root@localhost redis-cluster]# chmod u+x shutdow-all.sh

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005  192.168.25.153:7006

>>> Creating cluster

Connecting to node 192.168.25.153:7001: OK

Connecting to node 192.168.25.153:7002: OK

Connecting to node 192.168.25.153:7003: OK

Connecting to node 192.168.25.153:7004: OK

Connecting to node 192.168.25.153:7005: OK

Connecting to node 192.168.25.153:7006: OK

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.25.153:7001

192.168.25.153:7002

192.168.25.153:7003

Adding replica 192.168.25.153:7004 to 192.168.25.153:7001

Adding replica 192.168.25.153:7005 to 192.168.25.153:7002

Adding replica 192.168.25.153:7006 to 192.168.25.153:7003

M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001

   slots:0-5460 (5461 slots) master

M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002

   slots:5461-10922 (5462 slots) master

M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003

   slots:10923-16383 (5461 slots) master

S: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004

   replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3

S: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005

   replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01

S: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006

   replicates 2935007902d83f20b1253d7f43dae32aab9744e6

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join.....

>>> Performing Cluster Check (using node 192.168.25.153:7001)

M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001

   slots:0-5460 (5461 slots) master

M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002

   slots:5461-10922 (5462 slots) master

M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003

   slots:10923-16383 (5461 slots) master

M: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004

   slots: (0 slots) master

   replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3

M: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005

   slots: (0 slots) master

   replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01

M: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006

   slots: (0 slots) master

   replicates 2935007902d83f20b1253d7f43dae32aab9744e6

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@localhost redis-cluster]#

3.3. 集群的使用方法

Redis-cli连接集群。

[root@localhost redis-cluster]# redis01/redis-cli -p 7002 -c

-c:代表连接的是redis集群

4. Jedis

需要把jedis依赖的jar包添加到工程中。Maven工程中需要把jedis的坐标添加到依赖。

推荐添加到服务层。E3-content-Service工程中。

4.1. 连接单机版

第一步:创建一个Jedis对象。需要指定服务端的ip及端口。

第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。

第三步:打印结果。

第四步:关闭Jedis

@Test

public void testJedis()throws Exception {

// 第一步:创建一个Jedis对象。需要指定服务端的ip及端口。

Jedis jedis = new Jedis("192.168.25.153", 6379);

// 第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。

String result = jedis.get("hello");

// 第三步:打印结果。

System.out.println(result);

// 第四步:关闭Jedis

jedis.close();

}

4.2. 连接单机版使用连接池

第一步:创建一个JedisPool对象。需要指定服务端的ip及端口。

第二步:从JedisPool中获得Jedis对象。

第三步:使用Jedis操作redis服务器。

第四步:操作完毕后关闭jedis对象,连接池回收资源。

第五步:关闭JedisPool对象。

@Test

public void testJedisPool()throws Exception {

// 第一步:创建一个JedisPool对象。需要指定服务端的ip及端口。

JedisPool jedisPool = new JedisPool("192.168.25.153", 6379);

// 第二步:从JedisPool中获得Jedis对象。

Jedis jedis = jedisPool.getResource();

// 第三步:使用Jedis操作redis服务器。

jedis.set("jedis","test");

String result = jedis.get("jedis");

System.out.println(result);

// 第四步:操作完毕后关闭jedis对象,连接池回收资源。

jedis.close();

// 第五步:关闭JedisPool对象。

jedisPool.close();

}

4.3. 连接集群版

第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。

第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。

第三步:打印结果

第四步:系统关闭前,关闭JedisCluster对象。

@Test

public void testJedisCluster()throws Exception {

// 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。

Set<HostAndPort> nodes = new HashSet<>();

nodes.add(new HostAndPort("192.168.25.153", 7001));

nodes.add(new HostAndPort("192.168.25.153", 7002));

nodes.add(new HostAndPort("192.168.25.153", 7003));

nodes.add(new HostAndPort("192.168.25.153", 7004));

nodes.add(new HostAndPort("192.168.25.153", 7005));

nodes.add(new HostAndPort("192.168.25.153", 7006));

JedisCluster jedisCluster = new JedisCluster(nodes);

// 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。

jedisCluster.set("hello","100");

String result = jedisCluster.get("hello");

// 第三步:打印结果

System.out.println(result);

// 第四步:系统关闭前,关闭JedisCluster对象。

jedisCluster.close();

}

注:以上内容整理自互联网,如有侵权,请联系删除

猜你喜欢

转载自blog.csdn.net/wang_3600/article/details/78563910
今日推荐