12, Spring Technology stack-redis Sentinel implementation of high available caching cluster scheme combat __redis

Source: Internet
Author: User
Tags failover redis redis cluster install redis redis server

introduction of distributed characteristics of Redis Sentinel

Redis Sentinel is a distributed system that is designed to operate in an environment where many sentinel processes cooperate with one another. The advantages of having many sentinel processes cooperating with each other are as follows: When multiple Sentinel agree that a master is no longer available, fault detection is performed. This significantly reduces the error probability. Even if not all of the Sentinel are working, Sentinel can work properly, which makes the system very healthy.

All Sentinels,redis instances, the client connected to Sentinel and Redis, is a large distributed system of special nature. In this article, I deploy the Redis cluster in the form of an instance from the deployment of the Redis Sentinel cluster, using the Redis master slave model to describe how to use Redis Sentinel to implement the high availability of the cache system in the Spring project.

Learn about the basics of sentinel before deployment a healthy cluster deployment that requires at least three Sentinel instances three Sentinel instances should be placed on a failed stand-alone computer or virtual machine, For example, a different physical machine or a virtual machine that is executed on a different available area. Sentinel + Redis Distributed systems do not ensure that write requests are saved during a failure because Redis uses asynchronous replication. However, there are many ways to deploy Sentinel to allow Windows to limit lost writes to a specific moment, and of course there are other unsafe ways to deploy them. The client must support Sentinel. Most client libraries support Sentinel, but not all of them. No high availability settings are safe if you do not regularly test in your test environment, or even in the production environment you do not often go to test if Sentinel is working properly. But you may have a bad configuration and only appear late (3 o'clock in the morning your main node is down). Sentinel,docker, other network address translation table, port mapping should be very careful to use: Docker perform port remapping, destroy Sentinel automatically discover additional Sentinel process and a master node from the node list. See more information about Sentinel and Docker later in the article.

examples of this example are implemented by deploying 3 Redis Sentinel to implement the high availability of sentinel instances. This example implements the high availability of Redis cache by deploying 1 Redis Master Instances and 3 slave instances.

Redis Master instance and slave instance deployment

Download and install Redis, this article does not explain, concrete can refer to http://blog.csdn.net/zyhlwzy/article/details/78366265.

Refer to Redis Master (master-slave) replication (Replication) settings in the 8th section Redis deployment of the master-slave replication master-slave, but it should be noted that this article needs a master instance, 3 slave instances.

Redis instance information is as follows:

IP Port Notes
192.168.199.126 6379 Master
192.168.199.249 6382 Slave
192.168.199.249 6383 Slave
192.168.199.249 6384 Slave

Configure Master
Download and install Redis, this article does not explain, concrete can refer to http://blog.csdn.net/zyhlwzy/article/details/78366265.

Master's configuration is simple, we can open the Daemon (demo instance does not set authentication information, if necessary set itself).

Enter the Redis configuration file directory (/data/redis/redis-4.0.1/redis.conf) edit redis.conf for the following configuration.

Daemonize Yes

Configure Slave

Download and install Redis, this article does not explain, concrete can refer to http://blog.csdn.net/zyhlwzy/article/details/78366265.

Create the Slave profile directory, copy redis.conf to the corresponding directory, and configure it.

Mkdir/data/redis/cluster
cd/data/redis/cluster
mkdir-p 6382
cp/data/redis/redis-4.0.1/redis.conf/ data/redis/cluster/6382/redis-6382.conf
mkdir-p 6383
cp/data/redis/redis-4.0.1/redis.conf/data/redis/ cluster/6383/redis-6383.conf
mkdir-p 6384
cp/data/redis/redis-4.0.1/redis.conf/data/redis/cluster/6384/ Redis-6384.conf

3 slave Instances of the configuration file content, note that the following bold font to modify the contents of the section (note that the note to remove the note), the other are the same:

configuration Options option Value Description
Daemonize Yes Redis is not run as a daemon by default, and if you want it to run in the background, you can change it to Yes.
Pidfile /data/redis/cluster/6382/redis-6382. pid When Redis is running as a daemon, it will write a PID to the/var/run/redis.pid file by default
Port 6382 The listener port number defaults to 6379, and if you set it to 0, Redis will not listen for any client connections on the socket.
Database 1 Sets the number of databases.
Cluster-enabled No Enable or deactivate a cluster
Cluster-config-file /data/redis/cluster/6382/nodes-6382. conf Cluster configuration file, automatically generated at startup
Cluster-node-timeout 15000 Node interconnect timeout, per millisecond
Cluster-migration-barrier 1 This parameter represents how many good nodes a master node will have to cede from the node.
Cluster-require-full-coverage Yes If some key spaces in the cluster are not overwritten by any of the nodes in the cluster, the cluster will stop accepting writes
AppendOnly Yes Enable AOF persistence mode
Dir /data/redis/cluster/6382/ Node Data Persistence Storage Directory
Slaveof 192.168.199.126 6379 Master node Configuration

If the configuration succeeds and starts, when connecting to master through the Client tool, enter the following command

Info replication

Will get the following results

# Replication
role:master
connected_slaves:3
slave0:ip=192.168.199.249,port=6384,state=online, Offset=1127147,lag=1
slave1:ip=192.168.199.249,port=6383,state=online,offset=1127292,lag=1
slave2:ip= 192.168.199.249,port=6382,state=online,offset=1127292,lag=1
Master_replid: Beba8e13c44c5a4afcf8c82889b524b2f76faa22
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1127292
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:78717
repl_backlog_histlen:1048576

Redis Sentinel Deployment

3 Redis Sentinel information is as follows

IP Port Notes
192.168.199.126 26379 Sentinel
192.168.199.126 26479 Sentinel
192.168.199.126 26579 Sentinel

After the Redis installation is complete, a sentinel.conf file is created under the Redis directory (this instance is redis-4.0.1), creating a sentinel directory and copying the profile to rename the Custom Sentinel profile.

Mkdir/data/redis/sentinel
cp/data/redis/redis-4.0.1/sentinel.conf  /data/redis/sentinel/sentinel_26379. conf
cp/data/redis/redis-4.0.1/sentinel.conf  /data/redis/sentinel/sentinel_26479.conf
cp/data/ redis/redis-4.0.1/sentinel.conf  /data/redis/sentinel/sentinel_26579.conf

sentinel_26379.conf configuration content:

Protected-mode no
port 26379
Sentinel Monitor MyMaster 192.168.199.249 6384 2
Sentinel Failover-timeout MyMaster 60000
Sentinel Config-epoch mymaster 1
Sentinel Leader-epoch mymaster 1
Sentinel Known-slave MyMaster 192.168.199.126 6379

sentinel_26479.conf configuration content:

Protected-mode no
port 26479
Sentinel Monitor MyMaster 192.168.199.249 6384 2
Sentinel Failover-timeout MyMaster 60000
Sentinel Config-epoch mymaster 1
Sentinel Leader-epoch mymaster 1
Sentinel Known-slave MyMaster 192.168.199.126 6379

sentinel_26579.conf configuration content:

Protected-mode no
port 26579
Sentinel Monitor MyMaster 192.168.199.249 6384 2
Sentinel Failover-timeout MyMaster 60000
Sentinel Config-epoch mymaster 1
Sentinel Leader-epoch mymaster 1
Sentinel Known-slave MyMaster 192.168.199.126 6379

start Redis Master instance

Enter the Redis master server Redis Bin directory and execute the following command:

./redis-server/data/redis/redis-4.0.1/redis.conf

start Redis Slave instance

Enter the Redis slave server Redis Bin directory and execute the following command:

./redis-server/data/redis/cluster/6382/redis-6382.conf
./redis-server/data/redis/cluster/6383/ Redis-6383.conf
./redis-server/data/redis/cluster/6384/redis-6384.conf

Start Sentinel Instance

Enter the Redis slave server Redis Bin directory and execute the following command:

./redis-sentinel/data/redis/sentinel/sentinel_26379.conf 
./redis-sentinel/data/redis/sentinel/sentinel_ 26479.conf 

If the following message appears at startup, it indicates a successful start

maven Configuration

<dependency>
        <groupId>redis.clients</groupId>
        <artifactid>jedis</artifactid >
        <version>2.9.0</version>
    </dependency>       
    <dependency>
         <groupid >org.springframework.data</groupId>
         <artifactId>spring-data-redis</artifactId>
          <version>1.7.5.RELEASE</version>
</dependency>

Add the following configuration to the Config.properties

#Redis Sentinel use
redis.sentinel1.host=192.168.199.126
redis.sentinel1.port=26379
redis.sentinel2.host=192.168.199.126
redis.sentinel2.port=26479
redis.sentinel3.host=192.168.199.126
redis.sentinel3.port=26579
Redis.sentinel.mastername=mymaster

creates a new spring-redis-sentinel.xml configuration file, writes the following information

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"
    Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:jee= "Http://www.springframework.org/schema/jee"
    xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:util= "Http://www.springframework.org/schema/util" xmlns:task= "Http://www.springframework.org/schema/task" xmlns:cache= "http://www.springframework.org/schema/ Cache "Xmlns:c= ' http://www.springframework.org/schema/c ' xmlns:p=" http://www.springframework.org/schema/p "XSI:SC hemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.0.xsd Http://www.springframework.org/schema/jdbc HTTP://WWW.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd Http://www.springframework.org/schema/jee http:// Www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http:// Www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http:// Www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http:// Www.springframework.org/schema/task/spring-task-4.0.xsd Http://www.springframework.org/schema/cache http:// Www.springframework.org/schema/cache/spring-cache-3.1.xsd "default-lazy-init= true" > <!--turn on spring cache annotation Work

    Can--> <cache:annotation-driven cache-manager= "Rediscachemanager"/> <context:annotation-config

    <context:property-placeholder ignore-unresolvable= "true" location= "Classpath:config.properties"/> <!--Redis--> <bean id= "Poolconfig" class=Dis. Jedispoolconfig "> <property name=" maxtotal "value=" ${redis.maxtotal} "/> <property name=" maxId Le "value=" ${redis.maxidle} "/> <property name=" Maxwaitmillis "value=" ${redis.maxwait} "/> &LT;PR
        Operty name= "Testonborrow" value= "${redis.testonborrow}"/> </bean> <bean id= "Sentinelconfiguration" class= "Org.springframework.data.redis.connection.RedisSentinelConfiguration" > <property name= "Master "> <bean class=" org.springframework.data.redis.connection.RedisNode "> <property na
        Me= "name" value= "${redis.sentinel.mastername}" ></property> </bean> </property> <property name= "Sentinels" > <set> <bean class= "ORG.SPRINGFRAMEWORK.DATA.R" Edis.connection.RedisNode "> <constructor-arg name=" host "value=" ${redis.sentinel1.host} ">< /constructor-arg>
                    <constructor-arg name= "Port" value= "${redis.sentinel1.port}" ></constructor-arg>
                    </bean> <bean class= "Org.springframework.data.redis.connection.RedisNode" > <constructor-arg name= "host" value= "${redis.sentinel2.host}" ></constructor-arg> &L
                T;constructor-arg name= "Port" value= "${redis.sentinel2.port}" ></constructor-arg> </bean> <bean class= "Org.springframework.data.redis.connection.RedisNode" > <constructor -arg name= "host" value= "${redis.sentinel3.host}" ></constructor-arg> <constructor-arg name=
        "Port" value= "${redis.sentinel3.port}" ></constructor-arg> </bean> </set>
        </property> </bean> <!--redis Server center--> <bean id= "Jedisconnectionfactory" Class= "Org.springframeWork.data.redis.connection.jedis.JedisConnectionFactory "> <constructor-arg name=" sentinelconfig "ref=" Senti Nelconfiguration "></constructor-arg> <constructor-arg name=" Poolconfig "ref=" Poolconfig "></cons" tructor-arg> </bean> <bean id= "redistemplate" class= "Org.springframework.data.redis.core.RedisTemplat" E "> <property name=" connectionfactory "ref=" Jedisconnectionfactory "/> <property-name=" KeySeri Alizer "> <bean class=" Org.springframework.data.redis.serializer.StringRedisSerializer " > </property> <property name= "ValueSerializer" > <bean class= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean&

    Gt <!--Redis Cache manager--> <bean id= "Rediscachemanager" Org.springframework.data.redis.cache.RedisCacheManager "> &LT;constructor-arg name= "redisoperations" ref= "redistemplate"/> </bean> <bean id= "redisutils" class= " Ron.blog.blog_service.utils.RedisUtils "/> </beans>

introducing Spring-redis-sentinel.xml in Spring-context.xml

<import resource= "Classpath:spring-redis-sentinel.xml"/>

Write Redis help class Redisutils

The public class Redisutils {
    /** 
     * Redistemplate is a help class that simplifies Redis data access, 
     * This class encapsulates the Redis command in a high-level, This class allows you to invoke Valueoperations, Listoperations, and so on. 
     * *  
    @Autowired  
    private redistemplate<serializable, object> redistemplate;  

    /** 
     * Bulk Delete corresponding value 
     *  
     * @param keys * * 
       
    
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.