1. 程式人生 > >Redis單機和叢集搭建(筆記)

Redis單機和叢集搭建(筆記)

單機部署

1.下載安裝包

2.解壓安裝

tar -xzvf redis-4.0.1.tar.gz
 cd redis-4.0.1/
make PREFIX=/usr/local/redis/ install  如果是一般使用者,前面加sudo

3.拷貝配置檔案

sudo cp redis.conf /usr/local/redis/bin/

4.修改後臺啟動模式

把redis.conf裡的daemonize改為yes,其他可先不改。

5.啟動redis

./redis-server redis.conf

6.檢查redis是否執行正常

ps -ef | grep redis
./redis-cli

執行ping

7.通過jedis連線redis單機

7.1依賴
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.0</version>
</dependency>
7.2單元測試
/**
     * jedis測試 單機版
     */
    @Test
    public void testJedisSingle(){
        Jedis jedis = new Jedis("192.168.198.130", 6379);
        jedis.set("test", "this i a test");
        String str  = jedis.get("test");
        System.out.println("---:"+str);
        //關閉jedis的連結
        jedis.close();
    }
    
    /**
     * 使用連線池
     */
    @Test
    public void testJedisPool(){
        JedisPool jedisPool = new JedisPool("192.168.198.130", 6379);
        Jedis jedis = jedisPool.getResource();
        String str = jedis.get("test");
        System.out.println("---:"+str);
        jedis.close();
    }
7.3 jedis和spring整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- 讀取jedis配置檔案; 這裡可以不用配置,-dao已經配置了掃描配置檔案 -->
    <!-- <context:property-placeholder location="classpath:/properties/*.properties"/> -->
    <!-- 連線池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大連線數 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空閒連線數 -->
        <property name="maxIdle" value="10" />
        <!-- 每次釋放連線的最大數目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 釋放連線的掃描間隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 連線最小空閒時間 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 連線空閒多久後釋放, 當空閒時間>該值 且 空閒連線>最大空閒連線數 時直接釋放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 獲取連線時的最大等待毫秒數,小於零:阻塞不確定的時間,預設-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在獲取連線的時候檢查有效性, 預設false -->
        <property name="testOnBorrow" value="true" />
        <!-- 在空閒時檢查有效性, 預設false -->
        <property name="testWhileIdle" value="true" />
        <!-- 連線耗盡時是否阻塞, false報異常,ture阻塞直到超時, 預設true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>    
    <!--jedis客戶端單機版 -->
     <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="${JEDIS_HOST}"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>  
    <bean id="redisClientDao" class="com.taotao.rest.dao.impl.JedisClientSingle"></bean> <!--定義自己定義的jedis工具類-->
</beans>
/**
     * 結合spring的redis單機版測試
     */
    @Test
    public void testSpringSingle(){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
        JedisPool jedisPool = (JedisPool)context.getBean("redisClient");
        Jedis jedis = jedisPool.getResource();
        jedis.set("key1", "1111");
        String str = jedis.get("key1");
        System.out.println("--:"+str);
        jedis.close();
        jedisPool.close();
    }

redis叢集搭建

1.安裝ruby

yum install ruby
yum install rubygems

2.ruby版本調整

執行gem install redis應該會出現錯誤

ERROR:  Error installing redis:
     redis requires Ruby version >= 2.2.2.

CentOS7 yum庫中ruby的版本支援到 2.0.0,可gem安裝redis需要最低是2.2.2,採用rvm來更新ruby:

2.1執行金鑰驗證
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

2.2

curl -sSL https://get.rvm.io | bash -s stable
find / -name rvm -print

結果應該如下:

        /usr/local/rvm
     /usr/local/rvm/src/rvm
     /usr/local/rvm/src/rvm/bin/rvm
     /usr/local/rvm/src/rvm/lib/rvm
     /usr/local/rvm/src/rvm/scripts/rvm
     /usr/local/rvm/bin/rvm
     /usr/local/rvm/lib/rvm
     /usr/local/rvm/scripts/rvm
source /usr/local/rvm/scripts/rvm
rvm list known
    MRI Rubies
    [ruby-]1.8.6[-p420]
    [ruby-]1.8.7[-head] # security released on head
    [ruby-]1.9.1[-p431]
    [ruby-]1.9.2[-p330]
    [ruby-]1.9.3[-p551]
    [ruby-]2.0.0[-p648]
    [ruby-]2.1[.10]
    [ruby-]2.2[.7]
    [ruby-]2.3[.4]
    [ruby-]2.4[.1]
    ruby-head

挑個新一點的

rvm install 2.5.1
rvm use 2.5.1
rvm use 2.5.1 --default
rvm remove 2.3.4

ruby --version
gem install redis

看一下版本

     Fetching: redis-4.0.1.gem (100%)
    Successfully installed redis-4.0.1
    Parsing documentation for redis-4.0.1
    Installing ri documentation for redis-4.0.1
    Done installing documentation for redis after 3 seconds
    1 gem installed

3.建立6個節點

在/user/local/下建立一個叢集目錄,取名叫redis-cluster,在裡面再建立6個資料夾 7001,7002,7003,7004,7005,7006 然後將redis安裝目錄下的bin下的檔案拷貝到這些700x資料夾裡

然後需要修改它們的配置檔案:

port 7001(每個節點的埠號)
daemonize yes
bind 192.168.119.131(綁定當前機器 IP,繫結需要繫結的機器)
dir /usr/local/redis-cluster/7001/data/(資料檔案存放位置)
pidfile /var/run/redis_7001.pid(pid 7001和port要對應)
cluster-enabled yes(啟動叢集模式)
cluster-config-file nodes7001.conf(7001和port要對應)
cluster-node-timeout 15000
appendonly yes

vim redis.conf之後通過:%s/7001/7002/g快速修改

每個節點的配置都修改好之後,啟動所有節點redis

./7001/redis-server ./7001/redis.conf 
./7002/redis-server ./7002/redis.conf 
......

啟動好之後,通過ps -ef | grep redis檢視,應該能看到6個redis程序。 此時,這幾個redis都是各自獨立的叢集,需要通過ruby指令碼建立叢集

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

–replicas 1 表示主從複製比例為1:1,所以應該是三個master節點和三個slave節點

如果一切順利,叢集應該已經搭建完成,通過如下命令連線叢集 ./7001/redis-cli -h 172.16.30.102 -p 7001 -c 進入之後,通過cluster info和cluster nodes可以檢視叢集資訊

[[email protected] redis-cluster]# ./7001/redis-cli -h 172.16.30.102 -p 7001 -c
172.16.30.102:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:10343
cluster_stats_messages_pong_sent:10256
cluster_stats_messages_sent:20599
cluster_stats_messages_ping_received:10251
cluster_stats_messages_pong_received:10343
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:20599
172.16.30.102:7001> cluster nodes
4f4bd0ad1b80139a1ad12174de7f843100f799ec 172.16.30.102:[email protected] slave bbf60a3ac68ee0eeac1b8a32efa6e483649bd3b0 0 1532371748000 5 connected
873a63b248ae7f1f118e1dc94fe65e1c4f837a56 172.16.30.102:[email protected] slave 281b9a69f4e024f5653e2c0bd675cdeb7728bf82 0 1532371747289 6 connected
281b9a69f4e024f5653e2c0bd675cdeb7728bf82 172.16.30.102:[email protected] master - 0 1532371746281 3 connected 10923-16383
5c3e753421f36eabbc1f68cc155135fd92027a9f 172.16.30.102:[email protected] myself,master - 0 1532371747000 1 connected 0-5460
9fdf31666b7c56e995c6db7d354043d7556c3002 172.16.30.102:[email protected] slave 5c3e753421f36eabbc1f68cc155135fd92027a9f 0 1532371749311 4 connected
bbf60a3ac68ee0eeac1b8a32efa6e483649bd3b0 172.16.30.102:[email protected] master - 0 1532371748300 2 connected 5461-10922
172.16.30.102:7001> 

可能遇到的問題 1、伺服器的防火牆是否允許叢集匯流排埠通過 redis叢集不僅需要開通redis客戶端連線的埠,而且需要開通叢集匯流排埠,叢集匯流排埠為redis客戶端連線的埠 + 10000  如上面用到redis埠為6379,7000,7001,7002,7003,7004,7005,則叢集匯流排埠為16379,17000,17001,17002,17003,17004,17005,故,所有伺服器的點需要開通redis的客戶端連線埠和叢集匯流排埠。如果伺服器有安全組安全組也要開通,沒有隻要開通伺服器本身的 ##centos7.3伺服器

systemctl status firewalld.service #防火牆狀態
systemctl start firewalld.service   #防火牆開啟
systemctl disable firewalld.service #防火牆不可用

firewall-cmd --zone=public --add-port=80/tcp --permanent #開啟埠

命令含義:

–zone #作用域

–add-port=80/tcp #新增埠,格式為:埠/通訊協議

–permanent #永久生效,沒有此引數重啟後失效

重啟防火牆 firewall-cmd --reload