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
相關文章
相關標籤/搜索