Redis缓存以及Redis集群的简单使用

Redis简介

Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。它经过提供多种键值数据类型来适应不一样场景下的存储需求,目前为止Redis支持的键值数据类型如node

下:linux

字符串类型(String)c++

哈希类型(hash)redis

列表类型(list)算法

集合类型(set)spring

有序集合类型(zset)(sorted set)数据库

做用:编程

一、能够减轻数据库服务器压力。vim

二、提升性能后端

 

1、应用场景:

缓存(数据查询、短链接、新闻内容、商品内容等等)。(最多使用)

分布式集群架构中的session分离。

聊天室的在线好友列表。

任务队列。(秒杀、抢购、12306等等)

应用排行榜。

网站访问统计。

数据过时处理(能够精确到毫秒)

Redis持久化方案

rdb:快照形式,将当前的状态保存起来,隔断时间修改

aof:命令行形式,将命令存入到aof文件里面,一秒保存数据。对数据安全性比较高,西欧马性能比较差。

默认开启rdb模式。

2Redis数据库

 

 

数量:16

默认使用数据库:db0

设置Redis连接密码

 

容许本地局域网链接

 

Redis搭建

一、安装c语言编译器gcc

 

[root@localhost Desktop]# yum install gcc-c++

 

二、下载(上传)redis的源代码

2.1、解压

 

[root@localhost Desktop]# tar -zxvf redis-3.0.0.tar.gz

 

三、编译

 

[root@localhost Desktop]# cd redis-3.0.0

 

[root@localhost redis-3.0.0]# make

四、安装

进入编译目录

 

[root@localhost Desktop]# cd src

 

编译安装

 

[root@localhost src]# make install PREFIX=/usr/local/redis

 

PREFIX指定安装路径

3.4.3Redis启动

1、前段启动模式

 

[root@localhost src]# cd /usr/local/redis/bin
[root@localhost bin]# ./redis-server

默认启动端口号为6379端口

后端启动模式

A、从redis的源码目录中复制redis.confredis的安装目录。

 

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

 

B、修改redis.conf配置文件

 

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

 

 

C、启动

 

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

 

3.4.4、查询Redis进程

 

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

 

3.4.5、关闭Redis进程

 

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

3.4.6Redis基本命令

 

[root@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

 

[root@localhost Desktop] yum install ruby
[root@localhost Desktop] yum install rubygems

二、redis集群管理工具

 

[root@localhost Desktop]# cd redis-3.0.0/src/
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr  1  2015 redis-trib.rb

 

三、安装rubyredis的接口程序

将redis-3.0.0.gem上传的到linux服务器

安装ruby

 

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

 

集群搭建

1.、在/usr/local下建立redis-cluster文件夹

 

[root@localhost ]# cd /usr/local
[root@localhost local]# mkdir redis-cluster
[root@localhost redis]# cd redis/

 

2、将redis目录下的bin目录拷贝到/redis-cluster/redis文件下

 

[root@localhost redis]# cp -r bin ../redis-cluster/redis
[root@localhost redis]# cd ../redis-cluster/
[root@localhost redis-cluster]# ll
total 4
drwxr-xr-x. 2 root root 4096 Dec 21 20:04 redis
[root@localhost redis-cluster]# cd redis/
[root@localhost redis-cluster]# rm -f dump.rdb

 

3、修改redis-conf文件

 cluster-enabled yes这行取消注释,开启redis_cluster

 

进入redis源代码拷贝redis-trib.rb/usr/local/redis-cluster/目录下

 

[root@localhost src]cp redis-trib.rb /usr/local/redis-cluster/

 

配置其余redis服务器略

 

[root@localhost src] ./redis-server redis.conf

 

四、建立集群

 

[root@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(表明链接集群)

 

[root@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图形化操做

一、Redis Desktop Manager

 

只能链接单机版,不能链接集群。

二、Jedis客户端

2.1、单机版

一、须要将jedisjar包添加到工程中,若是是maven须要添加jar包的坐标

 

<!-- Redis客户端 --><dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

 

二、单机版测试

 

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();
}

 

 

三、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.7Spring整合Jedis

3.7.1Spring整合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 id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
 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 id="jedisPool" class="redis.clients.jedis.JedisPool" >
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 ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
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.2Spring整合Jedis(集群版本)

 1 <!-- 配置集群版jedis -->
 2 <bean id="jedisCluster"  class="redis.clients.jedis.JedisCluster" >
 3 <constructor-arg name="nodes" >
 4 <set>
 5  <bean class="redis.clients.jedis.HostAndPort" >
 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 class="redis.clients.jedis.HostAndPort" >
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 class="redis.clients.jedis.HostAndPort" >
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 class="redis.clients.jedis.HostAndPort" >
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 class="redis.clients.jedis.HostAndPort" >
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 class="redis.clients.jedis.HostAndPort" >
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 ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
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
相关文章
相关标签/搜索