|
Redis简介
Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型如
下:
字符串类型(String)
哈希类型(hash)
列表类型(list)
集合类型(set)
有序集合类型(zset)(sorted set)。
作用:
1、可以减轻数据库服务器压力。
2、提高性能
1、应用场景:
缓存(数据查询、短连接、新闻内容、商品内容等等)。(最多使用)
分布式集群架构中的session分离。
聊天室的在线好友列表。
任务队列。(秒杀、抢购、12306等等)
应用排行榜。
网站访问统计。
数据过期处理(可以精确到毫秒)
Redis持久化方案
rdb:快照形式,将当前的状态保存起来,隔断时间修改
aof:命令行形式,将命令存入到aof文件里面,一秒保存数据。对数据安全性比较高,西欧马性能比较差。
默认开启rdb模式。
2、Redis数据库
数量:16
默认使用数据库:db0
设置Redis链接密码
允许本地局域网连接
Redis搭建
1、安装c语言编译器gcc
[iyunv@localhost Desktop]# yum install gcc-c++
2、下载(上传)redis的源代码
略
2.1、解压
[iyunv@localhost Desktop]# tar -zxvf redis-3.0.0.tar.gz
3、编译
[iyunv@localhost Desktop]# cd redis-3.0.0
[iyunv@localhost redis-3.0.0]# make
4、安装
进入编译目录
[iyunv@localhost Desktop]# cd src
编译安装
[iyunv@localhost src]# make install PREFIX=/usr/local/redis
PREFIX指定安装路径
3.4.3、Redis启动
1、前段启动模式
[iyunv@localhost src]# cd /usr/local/redis/bin
[iyunv@localhost bin]# .
/redis-server
默认启动端口号为6379端口
后端启动模式
A、从redis的源码目录中复制redis.conf到redis的安装目录。
[iyunv@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
B、修改redis.conf配置文件
[iyunv@localhost redis-3.0.0]# cd /usr/local/redis/bin/
[iyunv@localhost redis
-3.0.0]# vim redis.conf
C、启动
[iyunv@localhost bin]# ./redis-server redis.conf
3.4.4、查询Redis进程
[iyunv@localhost bin]# ps aux|grep redis
3.4.5、关闭Redis进程
[iyunv@localhost bin]# ./redis-cli -p 6379 shutdown
3.4.6、Redis基本命令
[iyunv@localhost bin]# ./redis-cli
127.0.0.1:6379> set a 10
OK
127.0.0.1:6379> get a
"10"
127.0.0.1:6379> incr a
(integer)11
127.0.0.1:6379> decr a
(integer)10
127.0.0.1:6379> del a
(integer)1
127.0.0.1:6379> keys
(empty list or set)
Redis缓存集群
集群原理
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 会根据节点数量大致均等的将哈希槽映射到不同的节点
redis-cluster投票:容错
(1)领着投票过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.
(2):什么时候整个集群不可用(cluster_state:fail)?
a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态. ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.
b:如果集群超过半数以上master挂掉,无论是否有slave集群进入fail状态.
ps:当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误
集群环境搭建
Ruby是一种面向对象的编程语言,20世纪90年代日本人开发。
搭建集群需要使用到官方提供的ruby脚本。
需要安装ruby的环境。小编使用的是6台虚拟机搭建的Redis集群
1、安装ruby
[iyunv@localhost Desktop] yum install ruby
[iyunv@localhost Desktop] yum install rubygems
2、redis集群管理工具
[iyunv@localhost Desktop]# cd redis-3.0.0/src/
[iyunv@localhost src]# ll
*.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb
3、安装ruby和redis的接口程序
将redis-3.0.0.gem上传的到linux服务器
安装ruby
[iyunv@localhost Desktop]# gem install redis-3.0.0.gem
集群搭建
1.、在/usr/local下创建redis-cluster文件夹
[iyunv@localhost ]# cd /usr/local
[iyunv@localhost local]# mkdir redis
-cluster
[iyunv@localhost redis]# cd redis
/
2、将redis目录下的bin目录拷贝到/redis-cluster/redis文件下
[iyunv@localhost redis]# cp -r bin ../redis-cluster/redis
[iyunv@localhost redis]# cd ..
/redis-cluster/
[iyunv@localhost redis
-cluster]# ll
total
4
drwxr
-xr-x. 2 root root 4096 Dec 21 20:04 redis
[iyunv@localhost redis
-cluster]# cd redis/
[iyunv@localhost redis
-cluster]# rm -f dump.rdb
3、修改redis-conf文件
cluster-enabled yes这行取消注释,开启redis_cluster
进入redis源代码拷贝redis-trib.rb到/usr/local/redis-cluster/目录下
[iyunv@localhost src]cp redis-trib.rb /usr/local/redis-cluster/
配置其他redis服务器略
[iyunv@localhost src] ./redis-server redis.conf
4、创建集群
[iyunv@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.0.184:6379 192.168.0.109:6379 192.168.0.110:6379 192.168.0.114:6379 192.168.0.113:6379 192.168.0.102:6379
>>> Creating cluster
Connecting to node 192.168.0.184:6379: OK
Connecting to node 192.168.0.109:6379: OK
Connecting to node 192.168.0.110:6379: OK
Connecting to node 192.168.0.114:6379: OK
Connecting to node 192.168.0.113:6379: OK
Connecting to node 192.168.0.102:6379: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.102:6379
192.168.0.109:6379
192.168.0.184:6379
Adding replica 192.168.0.113:6379 to 192.168.0.102:6379
Adding replica 192.168.0.114:6379 to 192.168.0.109:6379
Adding replica 192.168.0.110:6379 to 192.168.0.184:6379
M: 347129817247251ab9b89b6b985ce4193f68269f 192.168.0.184:6379
slots:10923-16383 (5461 slots) master
M: 202807bf910b0fe350f7a990d84671120e4e7b9f 192.168.0.109:6379
slots:5461-10922 (5462 slots) master
S: fd0bc6dca63e917298e697a1e0ccd90be3bedf5a 192.168.0.110:6379
replicates 347129817247251ab9b89b6b985ce4193f68269f
S: 9b9affc04e7052c3be2c75fafb4561fea5abb3bc 192.168.0.114:6379
replicates 202807bf910b0fe350f7a990d84671120e4e7b9f
S: 4beac51798f3790d0f59d967df713459a4bfc8bf 192.168.0.113:6379
replicates 27b8a3175ce59f4d62e4894b5abe57c64ef3e860
M: 27b8a3175ce59f4d62e4894b5abe57c64ef3e860 192.168.0.102:6379
slots:0-5460 (5461 slots) master
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.0.184:6379)
M: 347129817247251ab9b89b6b985ce4193f68269f 192.168.0.184:6379
slots:10923-16383 (5461 slots) master
M: 202807bf910b0fe350f7a990d84671120e4e7b9f 192.168.0.109:6379
slots:5461-10922 (5462 slots) master
M: fd0bc6dca63e917298e697a1e0ccd90be3bedf5a 192.168.0.110:6379
slots: (0 slots) master
replicates 347129817247251ab9b89b6b985ce4193f68269f
M: 9b9affc04e7052c3be2c75fafb4561fea5abb3bc 192.168.0.114:6379
slots: (0 slots) master
replicates 202807bf910b0fe350f7a990d84671120e4e7b9f
M: 4beac51798f3790d0f59d967df713459a4bfc8bf 192.168.0.113:6379
slots: (0 slots) master
replicates 27b8a3175ce59f4d62e4894b5abe57c64ef3e860
M: 27b8a3175ce59f4d62e4894b5abe57c64ef3e860 192.168.0.102:6379
slots:0-5460 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4、测试Redis缓存集群
./redis-cli -h 集群节点ip地址 -p 端口号 -c(代表连接集群)
[iyunv@localhost redis]# ./redis-cli -h 192.168.0.109 -p 6379 -c
192.168.0.109:6379> ping
PONG
192.168.0.109:6379>
Redis图形化操作
1、Redis Desktop Manager
只能连接单机版,不能连接集群。
2、Jedis客户端
2.1、单机版
1、需要将jedis的jar包添加到工程中,如果是maven需要添加jar包的坐标
<!-- Redis客户端 --><dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2、单机版测试
public static void main(String[] args) {
//创建jedis对象
Jedis jedis = new Jedis("192.168.1.101",6379);
//设置redis连接密码
jedis.auth("woo0nise");
//调用jedis对象的方法,方法域名和redis的命令一致
jedis.set("b", "Redis Test");
String b = jedis.get("b");
System.out.println(b);
//关闭jedis
jedis.close();
}
3、Jedis连接池
@Test
public void testJedisPool(){
//设置连接池的配置信息
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲连接数, 应用自己评估,不要超过AliCloudDB for Redis每个实例最大的连接数
config.setMaxIdle(200);
//最大连接数, 应用自己评估,不要超过AliCloudDB for Redis每个实例最大的连接数
config.setMaxTotal(300);
config.setTestOnBorrow(false);
config.setTestOnReturn(false);
String host = "192.168.1.101";
String password = "woo0nise";
//创建连接池
JedisPool pool = new JedisPool(config, host, 6379, 3000, password);
//从连接池获取连接对象
Jedis jedis = pool.getResource();
System.out.println("a:"+jedis.get("a"));
System.out.println("b:"+jedis.get("b"));
jedis.close();
pool.close();
}
2.2、集群
1、需要设置权限(我设置的为本地局域网全部可以访问该Redis集群)
@Test
public void jedisCluster(){
//集群节点集合
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
nodes.add(new HostAndPort("192.168.1.101",6379));
nodes.add(new HostAndPort("192.168.1.105",6379));
nodes.add(new HostAndPort("192.168.1.102",6379));
nodes.add(new HostAndPort("192.168.1.106",6379));
nodes.add(new HostAndPort("192.168.1.107",6379));
nodes.add(new HostAndPort("192.168.1.104",6379));
//创建集群
JedisCluster cluster = new JedisCluster(nodes);
cluster.set("test", "ok");
String data = cluster.get("test");
System.out.println(data);
cluster.close();
}
集群自带连接池
3.7、Spring整合Jedis
3.7.1、Spring整合Jedis(单机版)
applicationContext-jedis.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7 <!-- 连接池配置,可有可无 -->
8 <bean>
9 <!-- 最大连接数 -->
10 <property name="maxTotal" value="30" />
11 <!-- 最大空闲连接数 -->
12 <property name="maxIdle" value="10" />
13 <!-- 每次释放连接的最大数目 -->
14 <property name="numTestsPerEvictionRun" value="1024" />
15 <!-- 释放连接的扫描间隔(毫秒) -->
16 <property name="timeBetweenEvictionRunsMillis" value="30000" />
17 <!-- 连接最小空闲时间 -->
18 <property name="minEvictableIdleTimeMillis" value="1800000" />
19 <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
20 <property name="softMinEvictableIdleTimeMillis" value="10000" />
21 <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
22 <property name="maxWaitMillis" value="1500" />
23 <!-- 在获取连接的时候检查有效性, 默认false -->
24 <property name="testOnBorrow" value="true" />
25 <!-- 在空闲时检查有效性, 默认false -->
26 <property name="testWhileIdle" value="true" />
27 <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
28 <property name="blockWhenExhausted" value="false" />
29 </bean>
30 <!-- 配置单机版jedis(连接池) -->
31 <bean >
32 <constructor-arg name="host" value="192.168.1.101" ></constructor-arg>
33 <constructor-arg name="port" value="6379" ></constructor-arg>
34 <constructor-arg name="poolConfig" ref="jedisPoolConfig" ></constructor-arg>
35 </bean>
36
37 </beans>
applicationContext-jedis.xml 测试连接
@Test
public void JedisTest(){
ApplicationContext applicationContext
= new>
JedisPool jedisPool = (JedisPool)applicationContext.getBean("jedisPool");
Jedis jedis
= jedisPool.getResource();
jedis.set(
"a", "100");
String a
= jedis.get("a");
System.out.println(a);
jedis.close();
jedisPool.close();
}
3.7.2、Spring整合Jedis(集群版本)
1 <!-- 配置集群版jedis -->
2 <bean >
3 <constructor-arg name="nodes" >
4 <set>
5 <bean >
6 <constructor-arg name="host" value="192.168.1.101" ></constructor-arg>
7 <constructor-arg name="port" value="6379" ></constructor-arg>
8 </bean>
9 <bean >
10 <constructor-arg name="host" value="192.168.1.111" ></constructor-arg>
11 <constructor-arg name="port" value="6379" ></constructor-arg>
12 </bean>
13 <bean >
14 <constructor-arg name="host" value="192.168.1.112" ></constructor-arg>
15 <constructor-arg name="port" value="6379" ></constructor-arg>
16 </bean>
17 <bean >
18 <constructor-arg name="host" value="192.168.1.113" ></constructor-arg>
19 <constructor-arg name="port" value="6379" ></constructor-arg>
20 </bean>
21 <bean >
22 <constructor-arg name="host" value="192.168.1.114" ></constructor-arg>
23 <constructor-arg name="port" value="6379" ></constructor-arg>
24 </bean>
25 <bean >
26 <constructor-arg name="host" value="192.168.1.115" ></constructor-arg>
27 <constructor-arg name="port" value="6379" ></constructor-arg>
28 </bean>
29 </set>
30 </constructor-arg>
31 </bean>
applicationContext-jedis.xml 测试连接
@Test
public void jedisCluster(){
ApplicationContext applicationContext
= new>
JedisCluster jedisCluster = (JedisCluster)applicationContext.getBean("jedisCluster");
jedisCluster.set(
"a", "100");
String a
= jedisCluster.get("a");
System.out.println(a);
jedisCluster.close();
}
原创作品,侵权必究! Email:woo0nise@gamail.com
交流QQ群:193306983 |
|