Redis主從複製和主從切換

 

  1. 配置主從複製html

    1. 創建從文件夾,譬如 /usr/local/slaves/下創建 6380 6381 兩個文件夾(兩個從服務器)java

    2. 複製redis.conf到剛創建的兩個文件夾中node

    3. 修改redis.conf 中的redis

      1. port 6380spring

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

      3. 保存退出,另外一個從服務器作一樣修改,若是端口不是6380,修改斷開便可,此處修改成6381json

    4. 啓動主服務器 redis-server redis.confapi

    5. 進入從服務器文件夾,啓動從服務器服務器

    6. 查看主服務器信息:redis-cli -p 6379 info Replication,能夠看到有兩個從服務器app

    7. redis-cli -p 6379 進去主服務器,存儲數據 set key val

    8. 進入從服務器,redis-cli -p 6380 查看數據 get key  查看是否能取出數據

  2. 配置主從切換

    1. 新建文件 sentinel.conf

    2. 編輯文件

    3. ####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. <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. <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. 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));
        
            }
        }

 

參考文檔:

    http://redis.readthedocs.org/en/latest/topic/sentinel.html

 

加個廣告:     各位ITer若是身邊有朋友,親人作工程的,請推薦一下咱們的這個網站 www.17heli.com 免費提供工程信息,同時使用APP能夠提供在線人工客服找項目,介紹人脈,成功率高。謝謝啦。

相關文章
相關標籤/搜索