Redis单机和集群搭建(笔记)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30276961/article/details/83474057

单机部署

1.下载安装包

http://download.redis.io/releases/ 找一个版本,注意,要确保ruby里有对应版本,不能太新,否则集群无法搭建
wget http://download.redis.io/releases/redis-4.0.1.tar.gz

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可以查看集群信息

[root@slave2 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:7005@17005 slave bbf60a3ac68ee0eeac1b8a32efa6e483649bd3b0 0 1532371748000 5 connected
873a63b248ae7f1f118e1dc94fe65e1c4f837a56 172.16.30.102:7006@17006 slave 281b9a69f4e024f5653e2c0bd675cdeb7728bf82 0 1532371747289 6 connected
281b9a69f4e024f5653e2c0bd675cdeb7728bf82 172.16.30.102:7003@17003 master - 0 1532371746281 3 connected 10923-16383
5c3e753421f36eabbc1f68cc155135fd92027a9f 172.16.30.102:7001@17001 myself,master - 0 1532371747000 1 connected 0-5460
9fdf31666b7c56e995c6db7d354043d7556c3002 172.16.30.102:7004@17004 slave 5c3e753421f36eabbc1f68cc155135fd92027a9f 0 1532371749311 4 connected
bbf60a3ac68ee0eeac1b8a32efa6e483649bd3b0 172.16.30.102:7002@17002 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

猜你喜欢

转载自blog.csdn.net/sinat_30276961/article/details/83474057