In order to improve the performance of redis, you can create a redis based on the master-slave mode, which is responsible for write operations on the master server and read operations on the slave server.
Configuration of Redis master-slave mode:
1. Copy the redis.conf file to the /usr/local/bin directory, name it redis-slave.conf, modify the port number to 6479, and act as a slave server based on this file
cp redis.conf redis-slave.conf
2. Modify the redis-slave.conf configuration file
Here we use the redis server with port 6479 as the slave server with port 6379 server
Modify the redis-slave.conf configuration file
After modification, start the master-slave server:
[root@localhost bin]# redis-server redis.conf
[root@localhost bin]# redis-server redis-slave.conf
Connect to the reids master-slave server and view the master-slave information
test:
The value of a is set to 10 on the host, and a is obtained from the slave
Master-slave mode code:
package com.study.util; import redis.clients.jedis.Jedis; public class RedisMasterSlave { public static void main(String[] args) throws InterruptedException { Jedis master = new Jedis("192.168.150.129",6379);//主机 master.auth("1234"); Jedis slave = new Jedis("192.168.150.129",6579); // Slave slave.auth("1234" ); // Set the host of the 6579 server to 6379 slave.slaveof("192.168.150.129",6379 ); master.select(1); master.setex( "a",100,"100"); // The host writes // Delay for one second to prevent reading and writing in memory from being too fast, reading is completed before writing and null occurs Thread.sleep(1000 ); slave.select(1); String result = slave.get("a"); // The slave reads System.out.println(result); master.close(); slave.close(); } }
Before code execution:
After execution:
Note: The redis server with port 6579 is a newly configured server. The configuration file does not configure its main server before the code is executed. At this time, it runs as a separate server.
After executing the code slave.slaveof("192.168.150.129",6379);, it is set as the slave server of 6379. At this time, the server with port 6379 has two slave servers of 6479 and 6579.
Code git address: https://gitee.com/sjcq/redis.git