Redis master-slave replication and master-slave switching

Source: Internet
Author: User
Tags failover


  1. Configuring Master-slave replication

      1. Establish 6380 63,812 folders (two slave servers) from a folder, such as/usr/local/slaves/

      2. Copy redis.conf to the two folders you just created

      3. Modify the redis.conf in the

        1. Port 6380

        2. slaveof 127.0.0.1 6379------IP indicates the primary server's IP port represents the primary server port

        3. Save exit, another from the server to make the same changes, if the port is not 6380, modify the break can be changed here to 6381

      4. Start the primary server Redis-server redis.conf

      5. Go to from server folder, start from server

      6. View Primary server information: Redis-cli-p 6379 info Replication, you can see that there are two slave servers

      7. Redis-cli-p 6379 go into the master server, store data set key Val

      8. Enter from server, redis-cli-p 6380 view data get key to see if data can be fetched

  2. Configuring Master-Slave Switching

    1. New three files 6379-sentinel.conf 6380-sentinel.conf 6381-sentinel.conf

    2. Edit File

    3. # # # #master sentinel.conf# #sentinel实例之间的通讯端口port 26379### #sentinel需要监控的master信息:<mastername> <masterip > <masterPort> <quorum>.####<quorum> should be less than the number of slave in the cluster, only if at least <quorum> one Sentinel instance commits " Master Failure "will assume that Master is Odwon (" objective "failure). Sentinel Monitor MyMaster 127.0.0.1 6381 2### #授权密码, can not be set in a secure environment # #sentinel Auth-pass mymaster luyx30### #master被当前sentinel实例认定为 "fail" (Sdown) time interval Sentinel failover-timeout MyMaster 900000#### The number of slave that "slaveof" to the new master and replicate synchronously when the new master is generated. # #在salve执行salveof与同步时, the client request will be terminated. # #此值较大, which means that the "cluster" terminates the sum and large amount of time the client requests. # #此值较小 means that the "cluster" still uses the old data during failover, when multiple salve serve clients. Sentinel Config-epoch MyMaster 4### #failover过期时间, when the failover has started, no failover operations have been triggered during this time, and the current Sentinel will consider this failoer to be unsuccessful. Sentinel Leader-epoch MyMaster 4
    4. Two other files only need to modify port

    5. Start file separately: Redis-sentinel 6379-sentinel

  3. Read and write master and slave servers through Java

    1. Need to add a spring configuration and two additional jar packages

    2. <properties>    <project.build.sourceencoding>utf-8</ Project.build.sourceencoding>    <springversion>3.2.9.release</springversion ></properties><dependencies>    <dependency>         <groupId>org.apache.commons</groupId>         <artifactId>commons-pool2</artifactId>         <version>2.4.2</version>    </dependency>    < dependency>        <groupid>org.springframework.data</ Groupid>        <artifactid>spring-data-redis</artifactid >        <version>1.6.0.RELEASE</version>     </dependency>  &nbSp; <dependency>        <groupid>org.springframework </groupid>        <artifactid>spring-context</ Artifactid>        <version>${springversion}</version>     </dependency>    <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-tx</artifactId>        < version>${springversion}</version>    </dependency>     <dependency>        <groupid>org.springframework</ groupid>        <artifactid>spring-context-support</ artifactid>        <version>${springversion}</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>junit </groupId>        <artifactId>junit</artifactId>         <version>4.8</version>         <scope>test</scope>    </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.4.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>
    3. Spring configuration

      1. <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= "127.0.0.1" ></ Constructor-arg>                < Constructor-arg name= "Port"  value= "26479" ></constructor-arg>             </bean>             <bean class= "Org.springframework.data.redis.connection.RedisNode" >                 <constructor-arg  Name= "Host"  value= "127.0.0.1" ></constructor-arg>                 <constructor-arg name= "Port"  value= "26579" > </constructor-arg>            </bean>         </set>    </property></bean ><bean id= "JeidsconneCtionfactory "      class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory ">    < constructor-arg ref= "Redissentinelconfiguration"/></bean><bean id= "RedisTemplate"   class= "Org.springframework.data.redis.core.RedisTemplate" >    <property name= " ConnectionFactory " ref=" Jeidsconnectionfactory "/></bean>
    4. Java test

      1. import org.junit.before;import org.junit.test;import  org.springframework.context.applicationcontext;import  org.springframework.context.support.classpathxmlapplicationcontext;import  org.springframework.dao.dataaccessexception;import  org.springframework.data.redis.connection.redisconnection;import  org.springframework.data.redis.core.rediscallback;import  org.springframework.data.redis.core.redistemplate;/** * created by vincent on  15-10-13. */public class commontest {    private  applicationcontext context ;    private redistemplate redistemplate;     final string key = "Key7";     @Before      public void init () {        context =  New classpathxmlapplicationcontext ("Applicationcontext.xml");   &nBsp;     redistemplate= context.getbean ("Redistemplate", RedisTemplate.class);     }     @Test     public void test1 () {         redistemplate.execute (New rediscallback ()  {              @Override              public long doinredis (redisconnection redisconnection )  throws DataAccessException {                  redisconnection.set (Key.getbytes (), (System.currenttimemillis () + ""). GetBytes ());                 return 1l;            }         });     }     @Test     public void  test2 ( ) {        object execute = redistemplate.execute (new  rediscallback ()  {             @Override             public object doinredis ( Redisconnection redisconnection)  throws DataAccessException {                 return redisconnection.get ( Key.getbytes ());            }         });         system.out.println (new  String ((byte[]) (execute));     }}



Redis master-slave replication and master-slave switching

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.