Redis主从复制和主从切换

时间:2022-09-21 12:37:47

Redis主从复制和主从切换

参考:
+ Redis Sentinel机制与用法(一)
+ Redis主从复制和主从切换

摘要
Redis的主从复制功能可以实现读写分离,一个主可以挂载多个从.从服务器只能实现读不可写.当主服务器挂掉之后,无法切换到从服务器写. 通过Redis的Sentinel可以实现主从切换:当主服务器挂掉之后,自动将其中一个从服务器升级为主服务器

一:配置主从复制

  1. 当前的机器分布

    redis1:192.168.25.128
    redis2:192.168.25.129
    
  2. 期望实现结果:在redis1中set的值可以在redis2中取出

  3. 配置:

    在redis2的reids配置文件中加入
    slaveof 192.168.25.128 6379
    重启redis即可。
    
  4. 注意:redis.conf中的bind 127.0.0.1最好注释掉,否则可能在别的电脑上通过ip访问不到
  5. 测试结果:redis1中改变 redis2中也改变了

    [root@bogon etc]# redis-cli -h 192.168.25.128
    192.168.25.128:6379> get name
    "xiaoguo"
    192.168.25.128:6379> set name xiaowang
    OK
    192.168.25.128:6379> 
    
    [root@bogon etc]# redis-cli -h 192.168.25.129
    192.168.25.129:6379> get name
    "xiaowang"
    192.168.25.129:6379> 
    

二:配置自动切换

  1. 我的redis的目录结构

    redis/
            bin  dump.rdb  etc  redis.log  temp-1467161779.33597.rdb  temp-1467278541.2161.rdb
    配置文件在etc中,执行脚本在bin中
    
  2. 在etc中添加sentinel.conf文件,文件内容如下(配置详细讲解见博客顶部参考的文章)

    sentinel monitor mymaster 192.168.25.128 6379 1
    sentinel down-after-milliseconds mymaster 60000
    sentinel failover-timeout mymaster 180000
    sentinel parallel-syncs mymaster 1
    
  3. 保存后,执行redis-sentinel sentinel.conf &(如果不加&则不是后台运行)
  4. 通过以上配置就可以自动切换了
  5. 以上配置我在192.168.25.129中也配置了一份,放哪股指sentinel的单点故障

三:项目中的测试

  1. 我的项目是maven springmvc配置如下

      <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxTotal" value="30" />
            <property name="maxIdle" value="200" />
            <property name="maxWaitMillis" value="3000" />
            <property name="testOnBorrow" value="true"/>
        </bean>
        <!-- spring data redis -->
        <bean id="jedisConnectionFactory"
              class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="poolConfig" >
                <ref bean="jedisPoolConfig"/>
            </property>
            <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/>
            <property name="timeout" value="100000"/>
        </bean>
    
        <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
            <property name="master">
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <property name="name" value="mymaster"/>
                </bean>
            </property>
            <property name="sentinels">
                <set>
                    <bean class="org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name="host" value="192.168.25.128"></constructor-arg>
                        <constructor-arg name="port" value="26379"></constructor-arg>
                    </bean>
                    <bean class="org.springframework.data.redis.connection.RedisNode">
                        <constructor-arg name="host" value="192.168.25.129"></constructor-arg>
                        <constructor-arg name="port" value="26379"></constructor-arg>
                    </bean>
                </set>
            </property>
        </bean>
    
        <!-- Redis Template -->
        <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
            <property name="connectionFactory" ref="jedisConnectionFactory" />
    
        </bean>
    

    pom文件(请自行增删):

        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.favccxx.favsoft</groupId>
            <artifactId>favspringmvcrestful</artifactId>
            <packaging>war</packaging>
            <version>0.0.1-SNAPSHOT</version>
            <name>favspringmvcrestful Maven Webapp</name>
            <url>http://maven.apache.org</url>
    
            <properties>
                <spring.version>4.1.1.RELEASE</spring.version>
            </properties>
    
            <dependencies>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>3.8.1</version>
                    <scope>test</scope>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.data</groupId>
                    <artifactId>spring-data-redis</artifactId>
                    <version>1.6.0.RELEASE</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>${spring.version}</version>
                </dependency>
    
                <dependency>
                    <groupId>jstl</groupId>
                    <artifactId>jstl</artifactId>
                    <version>1.2</version>
                </dependency>
                <dependency>
                    <groupId>taglibs</groupId>
                    <artifactId>standard</artifactId>
                    <version>1.1.2</version>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                    <version>2.5</version>
                </dependency>
                <dependency>
                    <groupId>cglib</groupId>
                    <artifactId>cglib-nodep</artifactId>
                    <version>3.1</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>3.1</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>fastjson</artifactId>
                    <version>1.2.5</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>1.8.2</version>
                </dependency>
                <dependency>
                    <groupId>net.sf.ehcache</groupId>
                    <artifactId>ehcache</artifactId>
                    <version>2.7.5</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <version>1.6.6</version>
                </dependency>
                <dependency>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                    <version>2.5.2</version>
                </dependency>
                <dependency>
                    <groupId>commons-pool</groupId>
                    <artifactId>commons-pool</artifactId>
                    <version>1.6</version>
                </dependency>
                <dependency>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                    <version>1.1.1</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                    <version>1.7.10</version>
                    <scope>test</scope>
                </dependency>
    
            </dependencies>
            <build>
                <finalName>favspringmvcrestful</finalName>
            </build>
        </project>
    

    HelloWordController:

        package com.xiaoli.nginx.controller;
    
        import org.springframework.data.redis.core.RedisTemplate;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.RequestMapping;
    
        import javax.annotation.Resource;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
    
        /**
         * Created by xiaoli on 2016/6/30.
         */
        @Controller
        public class HelloWordController {
            @Resource
            private RedisTemplate<String, Object> redisTemplate;
    
            @RequestMapping("/")
            public String sayHello(HttpServletRequest request, HttpServletResponse response, Model model) {
        //        redisTemplate.opsForValue().set("name","xiaoguo");
                String value = (String) redisTemplate.opsForValue().get("name");
                model.addAttribute("name", value);
                return "hello";
            }
        }
    

    hello.jsp:

        <%--
          Created by IntelliJ IDEA.
          User: xiaoli
          Date: 2016/6/30
          Time: 16:08
          To change this template use File | Settings | File Templates.
        --%>
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
            <title>Title</title>
        </head>
        <body>
        hello,i'm ${name}
        </body>
        </html>
    

    启动项目:杀掉redis1 刷新界面 已让可以访问,redis2顶上去了,如果你去看redis2,你会发现方才你添加的 slaveof 192.168.25.128 6379不见了,没错,他变成了主了,哥们!