Redis Grade Improvement (master-Slave, Sentinel) combined with spring

Source: Internet
Author: User
Tags constructor redis sleep port number

Redis master-Slave

1: Create from Redis directory, copy the following files in the original Redis service:
  a:redis-benchmark
  b:redis-cli
  c:redis.conf
  d:redis-server

2: Modify Redis.conf profile
    A: Modify the service port number to ensure that the port number is unique
        port 6380
    B: Add from configuration
        slaveof 127.0.0.1 6379

3: Start the service, To see if there is a master Redis data (./redis-server redis.conf) from Redis
4: Note
    A: You need to create your own from Redis related directory
    B: Only Read permissions from Redis

Redis Sentinel

1: Create from Sentinel directory, copy the following files in the Redis installation directory: A:redis-sentinel 2: Add Sentinel Profile-sentinel.conf Port 16379 #** Ports Unique * * dir "/usr/redis -sen/6379/temp "#** Temp directory * * * daemonize Yes #protected-mode no logfile"/usr/redis-sen/6379/sentinel.log "#* * Log File * * Sentinel monitor Redismaster 192.168.203.142 6379 1 #** Master Redis and aliases * * Sentinel Down-after-milliseconds Redis                 Master 5000 #**5 sec Detection * * Sentinel failover-timeout redismaster 15000 #sentinel auth-pass redismaster r123  #** Authentication Password * * # Generated by CONFIG REWRITE Sentinel Config-epoch redismaster 144 Sentinel Leader-epoch Redismaster 144 #sentinel known-slave redismaster 192.168.203.141 6380 Sentinel Known-slave redismaster 192.168.2 03.142 6380 #** from redis** Sentinel Current-epoch 144 3: Start the service to see if the Sentinel process exists (./redis-server redis.conf) A: Start the service./redis-s Entinel sentinel.conf B: View service PS Aux|grep Redis-sentinel 4: Note A: If the master Redis hangs, the Sentinel goes back to modify the profile from Redis, and the Sentinel itself config file will be changed from Redis to primary Redis B: It's best to test yourself manually.

Redis and Spring integration

1:spring+maven, adding the relevant jar package: A:spring-data-redis B:jedis 2: Add Spring-jedis.xml profile <bean id= "Redissentinelconfigur ation "class=" org.springframework.data.redis.connection.RedisSentinelConfiguration "> <property name= "Master" > <bean class= "Org.springframework.data.redis.connection.RedisNode" > <prop Erty name= "name" value= "Redismaster" ></property> </bean> </property> < Property name= "Sentinels" > <set> <bean class= "Org.springframework.data.redis.conne Ction.
                    Redisnode "> <constructor-arg name=" host "value=" 192.168.203.142 "></constructor-arg>
        <constructor-arg name= "Port" value= "16379" ></constructor-arg> </set> </property> </bean> <bean id= "jeidsconnectionfactory" class= "Org.springframework.data.redis.conn Ection.jedis.JedisConnectionFactory "> <constructor-arg ref=" redissentinelconfiguration "></constructor-arg> </ bean> <bean id= "redistemplate" class= "Org.springframework.data.redis.core.RedisTemplate" P: connection-factory-ref= "Jeidsconnectionfactory"/>

3: Writing test classes

Package Com.raiyi.redis.spring.sen;

Import Javax.annotation.Resource;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
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;
Import org.springframework.data.redis.core.ValueOperations;
Import org.springframework.test.context.ContextConfiguration;

Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = {"Classpath*:spring-mvc.xml", "classpath *:spring-jedis.xml ",}) public class Redismstest {@Resource (name =" Redistemplate ") redistemplate<string, STR
    Ing> API;

    @Resource (name = "Redistemplate") valueoperations<string, object> Vooper;
               @Test public void Ms () {new Thread () {public void run () { for (int i = 0; i < 111111111; i++) {Api.execute (new rediscallback<boolean> () { @Override public Boolean Doinredis (redisconnection connection) throws Dataaccessexce
                            ption {byte[] key = ("Tmpkey"). GetBytes ();
                            Byte[] Value = ("Tmpvalue" + system.currenttimemillis () + "..." + math.random ()). GetBytes ();
                            Connection.set (key, value);
                        return true;
                    }
                    });
                    try {thread.sleep (2000L); } catch (Interruptedexception e) {//TODO auto-generated catch block e.pr
                    Intstacktrace ();

        }}}}.start (); New Thread () {public void run () {while (true) {Api.exEcute (New rediscallback<boolean> () {@Override public Boolean doinre Dis (redisconnection connection) throws DataAccessException {byte[] key = ("Tmpkey"). GetBytes (

                            );
                            string result = new String (Connection.get (key));
                            SYSTEM.OUT.PRINTLN (result);
                        return true;
                    }
                    });
                    try {thread.sleep (2000L); } catch (Interruptedexception e) {//TODO auto-generated catch block e.pr
                    Intstacktrace ();

        }}}}.start ();
 while (true) {}}}

4: Notes
A: No Redis password is configured in this Sentinel
B: Use a Redis short connection, the operation will be disconnected once, otherwise if the master Redis hangs the master Redis after the Sentinel configuration modification
C: There are other ways to configure, please add

"'

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.