[Switch] Simple implementation of Redis data master-slave replication on multiple servers

[ Website Construction ] Simple implementation of Redis data master-slave replication on multiple servers



The master-slave replication function of Redis is very powerful. A master can have multiple slaves, and a slave can have multiple slaves. In this way, a powerful multi-level server cluster architecture is formed. Below I will demonstrate how to perform master-slave replication of Redis data on multiple servers. Here I assume there are two servers, one is Windows operating system (LAN  IP : 192.168.3.82), the other is Linux operating system (LAN IP: 192.168.3.90), and redis is installed on both operating systems, Windows operating system Use the cygwin tool to install, the command is:

 

 
1 tar xzf redis-2.2.2.tar.gz
2 cd redis-2.2.2
3 make

You can use the "make test" command to determine whether the installation is successful.

 

Here I use 1 master and 2 slaves (master is under Windows, one slave is under Windows, and one slave is under Linux ). The basic process is:

image

 

1. Create two directories on the Windows server, Demo1 and Demo2, where Demo1 is used to store the Master service, and Demo2 is used to store the Slave service.

Configuration file modification in the Master service:

 
1 bind 192.168.3.82

 

Configuration file modification in the Slave service:

 
1 port 6381(服务端口号要分开)
2 bind 192.168.3.82
3 slaveof 192.168.3.82 6379 (设置master的Host以及Port)

 

2. Create a directory on the Linux server, Demo, Demo stores the Slave service, and modify the configuration file in the service:

 
1 bind 192.168.3.90
2 slaveof 192.168.3.82 6379(设置master的Host以及Port)

 

This completes all configuration.

 

3. Now run these 3 services, by command:

 
1 ./redis-server redis.conf

to start the redis service.

 

Note that when I start the master and then start a slave, I can find the slave:

image

A SYNC request will be sent to respond from the Master, and it supports automatic reconnection, that is, when the master is offline, it will be in a state of waiting for a request.

And on the Master:

image

It can accept the response of the slave and start the persistence operation, indicating that every time the slave connects to the master, it will persist the disk.

 

4. Now start writing a client program that uses the .NET components of ServiceStack.Redis.dll:

 

 
01 using ServiceStack.Redis;
02  
03 static void Main(string[] args)
04 {
05     IRedisClientFactory factory = new RedisCacheClientFactory();
06     IRedisClient client = factory.CreateRedisClient("192.168.3.82", 6379);
07  
08     client.Set<string>("username""leepy");
09  
10     string username = client.Get<string>("username");
11  
12     client.Save();
13  
14     Console.WriteLine("username: {0}", username);
15  
16     Console.ReadLine();
17 }</string></string>

operation result:

image

When the data is set, the data is saved in the memory, and when the Save method is called, the data is saved in the disk.

Among them, you will find that dump.rdb appears in the three service directories, indicating that the Master's files are synchronized to the Slave.

image

image

Open the file with the UE editor to view:

image 
From the Redis source code, it can be found that the rdb file is implemented using the lzf compression algorithm, and the default lzf compression algorithm is enabled.

 

In this way, you can read the data of the Slave disk database through other client programs or Web platforms , which truly achieves the purpose of separation of read and write.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326856888&siteId=291194637