Redis主从复制和主从切换

  1. 配置主从复制

    1. 建立从文件夹,譬如 /usr/local/slaves/下建立 6380 6381 两个文件夹(两个从服务器)

    2. 复制redis.conf到刚建立的两个文件夹中

    3. 修改redis.conf 中的

      1. port 6380

      2. slaveof 127.0.0.1 6379  ------ip表示主服务器的ip  端口表示主服务器端口

      3. 保存退出,另一个从服务器做同样修改,如果端口不是6380,修改断开即可,此处修改为6381

    4. 启动主服务器 redis-server redis.conf

    5. 进入从服务器文件夹,启动从服务器

    6. 查看主服务器信息:redis-cli -p 6379 info Replication,可以看到有两个从服务器

    7. redis-cli -p 6379 进去主服务器,存储数据 set key val

    8. 进入从服务器,redis-cli -p 6380 查看数据 get key  查看是否能取出数据

  2. 配置主从切换

    1. 新建文件 sentinel.conf

    2. 编辑文件

    3. ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      ####master  sentinel.conf
      ##sentinel实例之间的通讯端口
      port 26379
      ####sentinel需要监控的master信息:<mastername> <masterIP> <masterPort> <quorum>.
      ####<quorum>应该小于集群中slave的个数,只有当至少<quorum>个sentinel实例提交"master失效" 才会认为master为ODWON("客观"失效) .
      sentinel monitor mymaster 127.0.0.1 6381 1
      sentinel down-after-milliseconds mymaster 1000
      sentinel failover-timeout mymaster 180000
      sentinel parallel-syncs mymaster 1
    4. 其他两个文件只需要修改端口即可

    5. 启动redis-sentinel sentinel.conf &

  3. 通过java读写主从服务器

    1. 需要添加Spring配置和其他两个jar包

    2. ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      < 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 >
       
           < 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配置

      1. ?
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        < 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测试

      1. ?
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        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" );
                 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));
         
             }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis主从复制是一种基于异步的,单向的复制机制,可以将一个Redis实例的数据复制到多个其他实例上,从而实现数据的备份、负载均衡等功能。下面是Redis主从复制的深度剖析: 1. 主从复制的原理 Redis主从复制的原理是,从Redis主节点(Master)向从节点(Slave)发送RDB快照文件和增量命令流,从而实现数据的复制。主节点将自己的状态信息发送给从节点,从节点按照主节点的状态进行复制。 2. 主从复制的过程 主从复制的过程可以分为三个阶段:同步、复制和命令传播。 (1)同步阶段 主从复制的同步阶段是指从节点和主节点建立连接,进行身份验证,并将主节点的状态信息发送给从节点。在这个阶段,从节点向主节点发送SYNC命令,主节点接收到SYNC命令后,会生成RDB快照文件,并将RDB快照文件发送给从节点。当从节点接收到RDB快照文件后,会将其加载到内存中,然后向主节点发送PSYNC命令,主节点接收到PSYNC命令后,将增量命令流发送给从节点。 (2)复制阶段 主从复制的复制阶段是指从节点从主节点接收增量命令流,并将其应用到自己的数据库中。在这个阶段,从节点会将接收到的增量命令流写入自己的缓冲区中,然后按序应用到自己的数据库中。从节点记录自己已经复制的命令的偏移量和主节点的偏移量,用于在命令传播阶段进行判断。 (3)命令传播阶段 主从复制的命令传播阶段是指从节点向主节点发送命令确认信息,以及主节点向从节点发送新的增量命令流。在这个阶段,从节点会周期性地向主节点发送命令确认信息,主节点接收到命令确认信息后,会将新的增量命令流发送给从节点。从节点接收到新的增量命令流后,会将其应用到自己的数据库中。如果从节点和主节点之间的网络连接断开,从节点会向主节点重新发送SYNC命令,主节点会重新发送RDB快照文件和增量命令流。 3. 主从复制的优缺点 主从复制的优点是: (1)实现数据的备份和恢复功能,可以在主节点出现故障时,通过从节点快速恢复数据。 (2)实现负载均衡功能,可以通过将读请求分配到不同的从节点上,实现读写分离。 (3)提高服务的可用性,可以通过从节点提供服务,当主节点发生故障时,可以快速切换到从节点提供服务。 主从复制的缺点是: (1)从节点只能读取数据,不能写入数据,如果需要写入数据,必须通过主节点进行写操作。 (2)从节点的数据可能存在延迟,如果主节点的数据更新频繁,从节点的数据可能会落后于主节点。 (3)如果主节点出现故障,从节点需要重新选举出新的主节点,可能会导致服务中断。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值