Springboot Configuring Redis Master-Slave Services

Source: Internet
Author: User
Tags redis server port redis server

I learned a bit today. Springboot Configure the master and slave servers for Redis. Build a distributed cache service based on the principle of a master two from three Sentinels. It is primarily a configuration for Redis. Let me share with you.
Attach an official introduction to Redis
Http://redis.majunwei.com/topics/sentinel.html
To build a Redis master-slave service through Springboot, first use Redis to configure a master-slave service. Test the master- slave configuration of Redis for data read and write, and the master server automatically synchronizes the data to the slave server when a write operation occurs. The slave server is only responsible for reading and cannot write. If the primary server goes down, Sentinel (Sentinel) will elect a primary server from the server. At this point the server has read and write permissions.
Below we step to configure the master-slave server, according to a master two from the three sentinel principle, we need three Redis instances, respectively, the following information is configured Redis instance

Folder names are as follows

redis_master_s
redis_slaver1_s
redis_slaver2_s
redis.conf File

Master's redis.conf file (the rest is the default setting)

Port 6379
daemonize Yes
# This folder should be changed to its own directory
dir "/users/vobile_lzl/redis_master_s"

Slaver1 's redis.conf file

Port 6378
# master server port is 6379
slaveof 127.0.0.1 6379
dir "/users/vobile_lzl/redis_slaver1_s"

Slaver2 's redis.conf file

Port 6377
# master server port is 6379
slaveof 127.0.0.1 6379
dir "/users/vobile_lzl/redis_slaver2_s"

The master-slave server is configured.
Start a service (the command is roughly so)

./redis-server redis.conf

Test the function of master-slave replication
1. Primary server write, from server can read to
2. Cannot write from server

Note that the port, 6379, indicates the primary server, 6377, 6378 is from the server

127.0.0.1:6379> set name Lzl
OK
127.0.0.1:6377> get name
"Lzl"
127.0.0.1:6378> get name
"Lzl"
# from the server cannot write
127.0.0.1:6378> set name Lzl
(Error) READONLY you can ' t write against a read only slave .
127.0.0.1:6377> set Nam fdk
(Error) READONLY you can ' t write against a read only slave.
sentinel.conf File

Sentinel is a sentinel that monitors the health of the master and slave servers and, if the primary server is hung up, elects one as the primary server from the server.
The configuration file is as follows
Master's sentinel.conf

Port 26379
# The state of the initial configuration, this sentinel will automatically update
Sentinel monitor MyMaster 127.0.0.1 6379 2
daemonize Yes
logfile "./sentinel_log.log"

Slaver1 's sentinel.conf

Port 26378
# The state of the initial configuration, this sentinel will automatically update
Sentinel monitor MyMaster 127.0.0.1 6379 2
daemonize Yes
logfile "./sentinel_log.log"

Slaver2 's sentinel.conf

Port 26377
# The state of the initial configuration, this sentinel will automatically update
Sentinel monitor MyMaster 127.0.0.1 6379 2
daemonize Yes
LogFile "./sentinel_log.log"

Start Redis all the service side again

./redis-server redis.conf
./redis-server sentinel.conf--sentinel

Turn on Redis client separately

./redis-cli
./redis-cli-h 127.0.0.1-p 6378
./redis-cli-h 127.0.0.1-p 6377

Use the command to view the status of three Redis services

Info replication

Master
Role:master

127.0.0.1:6379> info Replication
# replication
role:master
connected_slaves:2
slave0:ip= 127.0.0.1,port=6378,state=online,offset=4102,lag=1
slave1:ip=127.0.0.1,port=6377,state=online,offset=4102, Lag=1
master_repl_offset:4102
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_ First_byte_offset:2
repl_backlog_histlen:4101

Slaver1

127.0.0.1:6378> info Replication
# replication
role:slave
master_host:127.0.0.1
master_ port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:15931
slave_priority:100
slave_read_only:1
connected_slaves:0
master_ repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

Slaver2

127.0.0.1:6377> info Replication
# replication
role:slave
master_host:127.0.0.1
master_port : 6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_ offset:21629
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_ backlog_histlen:0

Kill the Master Service

vobile-lzldemacbook-pro:~ vobile_lzl$ Ps-ef | grep redis
  501 13258     1   0  9:52 pm??         0:00.37./redis-server *:6379 

  kill-9 13258

Check the status of master again
Indicates master has been down

127.0.0.1:6379> info Replication
Could not connect to Redis at 127.0.0.1:6379:connection refused

Observe for a while, 6377 of the slave server becomes the primary server

127.0.0.1:6377> info Replication
# replication
role:master
connected_slaves:0
master_repl_ offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl _backlog_histlen:0

Restore the master server and set it to master

Todo
Configuring Master-slave redis in Springboot

The contents of the previous project are still set.
Not sure what the content can be viewed
http://blog.csdn.net/u011521890/article/details/78070773
Or what's on GitHub.
Https://github.com/hpulzl/book_recommend application.properties File

New node Configuration

Master/Slave configuration
//  name of Redis Server Sentinel listener for Redis server names
spring.redis.sentinel.master=mymaster
// comma-separated List of host:port pairs  Sentinel configuration lists
spring.redis.sentinel.nodes= 127.0.0.1:26379,127.0.0.1:26378,127.0.0.1:26377
rediscacheconfig Configuration

New Redissentinelconfiguration Code

 /** * Redis Sentinel Configuration * @return */@Bean public redissentinelconfiguration redissentinelconfiguration () {
        Redissentinelconfiguration configuration = new Redissentinelconfiguration ();
        string[] host = Redisnodes.split (",");
            for (String redishost:host) {string[] item = Redishost.split (":");
            String IP = item[0];
            String port = item[1];
        Configuration.addsentinel (New Redisnode (IP, Integer.parseint (port)));
        } configuration.setmaster (master);
    return configuration; /** * The factory class that connects Redis * * @return */@Bean public jedisconnectionfactory jedisconnectionfactory () {//The Redissentinelconfiguration object is injected into the constructor method Jedisconnectionfactory factory = new Jedisconnectionfactory (Redisse
        Ntinelconfiguration ());
        Factory.sethostname (host);
        Factory.setport (port);
        Factory.settimeout (timeout);
        Factory.setpassword (password); FactOry.setdatabase (database);
    return factory; }

The above is the master-slave configuration of Redis. Very simple.
The next step is to verify that the configuration is successful in Springboot.
Verification Ideas
* Write a controller method that uses new data from Redis
* Turn off the Redis master server and call the Contoller method again,
* If the interface can still write information, the configuration is successful. When you look at other redis slave servers, you will find one from the server into the primary server.
Test code:

@RestController
@RequestMapping ("sample") public
class Samplecontroller {

    @Autowired
    private Useredisdao Useredisdao;

    @RequestMapping ("HI") public
    String SayHello (string key,string value) {
        useredisdao.setvalue (key,value);
        return Useredisdao.getvalue (key);
    }
}

Review the Redis master server discovery, 6377 is the master server

127.0.0.1:6377> info Replication
# replication
role:master
connected_slaves:2
slave0:ip= 127.0.0.1,port=6378,state=online,offset=378547,lag=1
slave1:ip=127.0.0.1,port=6379,state=online,offset= 378547,lag=1
master_repl_offset:378682
repl_backlog_active:1
repl_backlog_size:1048576
repl_ Backlog_first_byte_offset:2
repl_backlog_histlen:378681

Invoke Local Service
Http://localhost:8080/sample/hi?key=code&value=good
Querying write information from Redis

127.0.0.1:6379> keys *
1) "Code"
2) "name"
127.0.0.1:6379> get code 
"\" good\ ""

We see three Redis services that have this information

Then kill 6377 of the process

vobile-lzldemacbook-pro:redis_slaver2_s vobile_lzl$./redis-cli-p 6377 shutdown
Vobile-lzldemacbook-pro:redis _slaver2_s vobile_lzl$./redis-cli-p 6377
Could not connect to Redis at 127.0.0.1:6377:connection refused

Requesting the server again
Http://localhost:8080/sample/hi?key=today&value=iscoding
We found that the data was still written to Redis.

127.0.0.1:6378> keys *
1) "Code"
2) "name"
3) "Today"

127.0.0.1:6379> keys *
1) "Code"
2 ) "Name"
3) "Today"

At this point, I see 6379 become master.

127.0.0.1:6379> info Replication
# replication
role:master
connected_slaves:1
slave0:ip= 127.0.0.1,port=6378,state=online,offset=32419,lag=1
master_repl_offset:32419
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:32418

The above is the configuration process, if you have any questions, see GitHub
Https://github.com/hpulzl/book_recommend

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.