Redis集羣搭建

以前的Redis集羣搭建(在虛擬機上)文檔,放博客上以避免丟失,僅供參考java

1. Redis簡介

Redis是基於內存,也能夠基於磁盤持久化nosql數據庫,使用c語言開發。node

數據存儲結構key-valueredis

2. 安裝環境準備

Redis使用c語言開發,須要使用gcc編譯程序進行編譯。spring

1) 安裝gccsql

安裝命令:yum -y install gcc數據庫

2) 安裝上傳文件插件spring-mvc

  1. 安裝命令:yum –y install lrzsz
  2. 上傳命令:rz

3. 安裝redis

3.1 上傳安裝

使用rz命令上傳redis安裝包/usr/local/soft目錄ruby

[root@localhost soft]# ll服務器

-rw-r--r--. 1 root root   1358081 May 14  2015 redis-3.0.0.tar.gzmvc

3.2 解壓

解壓命令:tar -zxvf redis-3.0.0.tar.gz

3.3 安裝redis

1) 編譯

a) 進入redis解壓目錄:執行編譯命令: make

b) 執行編譯程序:生成編譯文src目錄下

2) 安裝

a) 命令make install PREFIX=/usr/local/soft/redis

b) 進入redis解壓目錄:執行安裝命令

[root@localhost redis-3.0.0]# make install PREFIX=/usr/local/soft/redis

cd src && make install

make[1]: Entering directory `/usr/local/soft/redis-3.0.0/src'

 

Hint: It's a good idea to run 'make test' ;)

 

    INSTALL install

    INSTALL install

    INSTALL install

    INSTALL install

    INSTALL install

make[1]: Leaving directory `/usr/local/soft/redis-3.0.0/src'

3.4 啓動redis服務

3.4.1 前臺啓動

啓動命令:./redis-server

特色:默認啓動服務,進程一種阻塞,不能直接退出,使用客戶端進行登陸

 

3.4.2 後臺啓動

修改redis配置文件,redis.conf配置文件,此時bin安裝目錄沒有配置文件,須要解壓目錄中拷貝一配置文件便可。

1) 拷貝redis.conf配置文件

a) redis.conf在redis解壓目錄中

b) 拷貝cp redis.conf ../redis/bin/

[root@localhost bin]# ll

total 15520

-rw-r--r--. 1 root root      18 Jul 26 17:14 dump.rdb

-rwxr-xr-x. 1 root root 4587078 Jul 26 17:09 redis-benchmark

-rwxr-xr-x. 1 root root   22185 Jul 26 17:09 redis-check-aof

-rwxr-xr-x. 1 root root   45403 Jul 26 17:09 redis-check-dump

-rwxr-xr-x. 1 root root 4689993 Jul 26 17:09 redis-cli

-rw-r--r--. 1 root root   41403 Jul 26 17:16 redis.conf

lrwxrwxrwx. 1 root root      12 Jul 26 17:09 redis-sentinel -> redis-server

-rwxr-xr-x. 1 root root 6448257 Jul 26 17:09 redis-server

 

2) 修改redis配置文件

a) daemonize no==daemonize yes

 

3) 啓動redis,加載配置文件

a) 命令./redis-server redis.conf

4) 登陸redis

a) 登陸命令:./redis-cli –h ip –p port

b) 登陸./redis-cli (默認登陸6379端口redis服務)

 

 

4. Redis集羣

4.1 Redis集羣搭建環境準備

redis集羣管理工具redis-trib.rb依賴ruby環境,首先須要安裝ruby環境:

安裝ruby

執行:

yum install ruby

再執行:

yum install rubygems

安裝ruby和redis的接口程序

拷貝redis-3.0.0.gem(ruby和redis接口)至/usr/local下

執行:

gem install /usr/local/redis-3.0.0.gem

 

4.2 集羣分析

搭建redis集羣,必須的有多臺redis服務器:保證一主一備4主機,4從機),搭建6redis服務器集羣。

保證8redis服務模擬8redis服務(端口不衝突,開啓集羣配置)

搭建集羣步驟:

1) 建立6目錄:700170027003。。。。。。7006

目錄名稱和redis服務端口一致

2) 開啓redis集羣配置

3) 修改redis服務器端口

4) 刪除持久化配置文件 (初始化集羣必須保持純潔性)

5) 啓動6服務器

6) 建立集羣

4.3 建立集羣目錄

建立目錄redis-cluster

安裝集羣安裝到redis-cluster目錄下面。

1) 建立8個目錄

[root@localhost redis-cluster]# mkdir 700{1..6}

[root@localhost redis-cluster]# ll

total 32

drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7001

drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7002

drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7003

drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7004

drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7005

drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7006

2) 拷貝redis安裝目錄bin7001

[root@localhost redis]# cp -r bin/ ../redis-cluster/7001/

3) 修改配置文件,開啓集羣

cluster-enabled yes   把註釋#去掉便可

 

4) 修改端口(redis配置文件端口和目錄名稱相同)

port 7001

注意:

 

5) 拷貝7001目錄下修改後的bin目錄7002…..70067目錄

drwxr-xr-x. 2 root root 4096 Jul 27 00:15 bin

[root@localhost 7001]# cp -r bin/ ../7002/

[root@localhost 7001]# cp -r bin/ ../7003/

[root@localhost 7001]# cp -r bin/ ../7004/

[root@localhost 7001]# cp -r bin/ ../7005/

[root@localhost 7001]# cp -r bin/ ../7006

6) 修改7002,7003………..7006redis服務端口

a) 要求redis服務端口和目錄文件一致。

4.4 拷貝集羣插件

使用redis-trib.rb插件建立集羣:

redis-trib.rb插件位置在redis解壓目錄下的編譯目錄src

去src拷貝插件到redis-Cluster集羣目錄中

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

 

4.5 建立集羣

1) 啓動redis服務(啓動6redis服務)

[root@localhost redis-cluster]# cd 7001/bin/

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

[root@localhost bin]# cd ..

[root@localhost 7001]# cd ..

[root@localhost redis-cluster]# cd 7002/bin/

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

[root@localhost bin]# cd ../../7003/bin/

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

[root@localhost bin]# cd ../../7004/bin/

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

[root@localhost bin]# cd ../../7005/bin/

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

[root@localhost bin]# cd ../../7006/bin/

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

[root@localhost bin]# ps -ef | grep redis

root      6926     1  0 Jul26 ?        00:00:21 ./redis-server *:6379    

root      7637     1  0 00:25 ?        00:00:00 ./redis-server *:7001 [cluster]

root      7642     1  0 00:25 ?        00:00:00 ./redis-server *:7002 [cluster]

root      7646     1  0 00:25 ?        00:00:00 ./redis-server *:7003 [cluster]

root      7650     1  0 00:25 ?        00:00:00 ./redis-server *:7004 [cluster]

root      7655     1  0 00:26 ?        00:00:00 ./redis-server *:7005 [cluster]

root      7659     1  0 00:26 ?        00:00:00 ./redis-server *:7006 [cluster]

root      7663  6287  0 00:26 pts/0    00:00:00 grep redis

2) 建立集羣

使用插件redis-trib.rb建立集羣:

建立命令

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

 

Create:建立集羣

--replicas 1:每一臺主機至少有一從機。

 

集羣成功建立信息:

>>> Creating cluster

Connecting to node 192.168.253.131:7001: OK

Connecting to node 192.168.253.131:7002: OK

Connecting to node 192.168.253.131:7003: OK

Connecting to node 192.168.253.131:7004: OK

Connecting to node 192.168.253.131:7005: OK

Connecting to node 192.168.253.131:7006: OK

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

Using 3 masters:

192.168.253.131:7001

192.168.253.131:7002

192.168.253.131:7003

Adding replica 192.168.253.131:7004 to 192.168.253.131:7001

Adding replica 192.168.253.131:7005 to 192.168.253.131:7002

Adding replica 192.168.253.131:7006 to 192.168.253.131:7003

M: 1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001

   slots:0-5460 (5461 slots) master

M: bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002

   slots:5461-10922 (5462 slots) master

M: 2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003

   slots:10923-16383 (5461 slots) master

S: 532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004

   replicates 1b807d0b0bb849632e93853728f8ed3e51d0dd48

S: 7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005

   replicates bd79a369ef0af24aa5e54455cae1f93e0cca636d

S: bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006

   replicates 2dc7730fab2a36ad00937bde4ec70421e7656f40

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.253.131:7001)

M: 1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001

   slots:0-5460 (5461 slots) master

M: bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002

   slots:5461-10922 (5462 slots) master

M: 2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003

   slots:10923-16383 (5461 slots) master

M: 532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004

   slots: (0 slots) master

   replicates 1b807d0b0bb849632e93853728f8ed3e51d0dd48

M: 7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005

   slots: (0 slots) master

   replicates bd79a369ef0af24aa5e54455cae1f93e0cca636d

M: bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006

   slots: (0 slots) master

   replicates 2dc7730fab2a36ad00937bde4ec70421e7656f40

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

4.6 啓動集羣(批處理文件)

在redis-Cluster目錄下建立啓動集羣批處理文件:start-all.sh

文件內容以下

cd 7001/bin

./redis-server redis.conf

cd ../../7002/bin

./redis-server redis.conf

cd ../../7003/bin

./redis-server redis.conf

cd ../../7004/bin

./redis-server redis.conf

cd ../../7005/bin

./redis-server redis.conf

cd ../../7006/bin

./redis-server redis.conf

1) 運行啓動sh文件

./start-all.sh

4.7 登陸集羣

登陸命令:./redis-cli –c –h ip –p port

cd 7001/bin

查詢集羣狀態

[root@localhost bin]# ./redis-cli -c -h 192.168.253.131 -p 7001

192.168.253.131: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_sent:433

cluster_stats_messages_received:433

查詢集羣節點信息

192.168.253.131:7001> cluster nodes

bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002 master - 0 1469550879424 2 connected 5461-10922

2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003 master - 0 1469550877393 3 connected 10923-16383

7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005 slave bd79a369ef0af24aa5e54455cae1f93e0cca636d 0 1469550880434 5 connected

bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006 slave 2dc7730fab2a36ad00937bde4ec70421e7656f40 0 1469550881443 6 connected

532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004 slave 1b807d0b0bb849632e93853728f8ed3e51d0dd48 0 1469550878404 4 connected

1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001 myself,master - 0 0 1 connected 0-5460

5. Jedis鏈接集羣

 

5.1 整合spring

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    <!-- Jedis配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="20"></property>
        <property name="maxTotal" value="1000"></property>
    </bean>
    <!-- 集羣版Jedis -->
    <bean class="redis.clients.jedis.JedisCluster">
        <constructor-arg name="nodes">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.253.131"></constructor-arg>
                    <constructor-arg name="port" value="7001"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.253.131"></constructor-arg>
                    <constructor-arg name="port" value="7002"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.253.131"></constructor-arg>
                    <constructor-arg name="port" value="7003"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.253.131"></constructor-arg>
                    <constructor-arg name="port" value="7004"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.253.131"></constructor-arg>
                    <constructor-arg name="port" value="7005"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.253.131"></constructor-arg>
                    <constructor-arg name="port" value="7006"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
    </bean>
</beans>

5.2 java測試代碼

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

public class SpringClusterJedis {
    /**
     * jedis鏈接集羣版redis
     */
    @Test
    public void jedisSpringRedis() {

        // 加載spring配置文件
        ApplicationContext app = new ClassPathXmlApplicationContext(
                "applicationContext-jedisCluster.xml");
        //獲取jedisCluster對象
        JedisCluster JedisCluster = app.getBean(JedisCluster.class);
        JedisCluster.set("username", "張三");
        String username = JedisCluster.get("username");
        System.out.println(username);
    }
}
相關文章
相關標籤/搜索