-
Configuring Master-slave replication
-
Establish 6380 63,812 folders (two slave servers) from a folder, such as/usr/local/slaves/
-
Copy redis.conf to the two folders you just created
-
Modify the redis.conf in the
-
Port 6380
-
slaveof 127.0.0.1 6379------IP indicates the primary server's IP port represents the primary server port
-
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
-
Start the primary server Redis-server redis.conf
-
Go to from server folder, start from server
-
View Primary server information: Redis-cli-p 6379 info Replication, you can see that there are two slave servers
-
Redis-cli-p 6379 go into the master server, store data set key Val
-
Enter from server, redis-cli-p 6380 view data get key to see if data can be fetched
-
Configuring Master-Slave Switching
-
New three files 6379-sentinel.conf 6380-sentinel.conf 6381-sentinel.conf
-
Edit File
-
# # # #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
-
Two other files only need to modify port
-
Start file separately: Redis-sentinel 6379-sentinel
-
Read and write master and slave servers through Java
-
Need to add a spring configuration and two additional jar packages
-
<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>
-
Spring configuration
-
<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>
-
Java test
-
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