Home > Back-end >  How to deal with Java server cluster redis overdue news listening?
How to deal with Java server cluster redis overdue news listening?

Time:01-28

# structure
Master a two (2) a Java server, redis server, Nginx load balancing for Java background,

# scenario
Double machine hot standby: when the primary server update or outage, nginx will be forwarded to the standby server, at ordinary times is the main server,

Problem # point
Lord for server code is the same, is to monitor the redis expired events, both deal with expired, again repeated, this how to solve?

# try to solve the
Modify the standby server code let its not listening, the main service outage for server listens again, but the implementation is difficult, but don't feel elegant,

CodePudding user response:

Redis Key expired to monitor events in the distributed system to prevent repeated processing

In the distributed system of Java, I use the Redis Key expiration time

Redis set as follows:

Notify keyspace - events Ex

In Java code USES JedisPubSub subscribe:

/*
(* add Redisd key expired event listeners will block the thread. As a result, in the final execution)
*/
Try {
Logger. The info (" being added Redisd key expired event listeners... ");

Jedis Jedis=Redis. Use (" data "). GetJedis ();
JedisPubSub JedisPubSub=new RedisKeyExpiredListener ();
String channel="__keyevent @ 0 __ : expired";//key expired events
If (jedis!=null) {
New Thread (() - & gt; {
Jedis. Subscribe (jedisPubSub, channel);//here started the subscription to monitor, the thread will be blocked here
}). The start ();
Logger. The info (" add Redisd key expired event listeners to complete!" );
} else {
Logger. Warn (" add Redisd failure key expired event listeners, did not get to jedis object!" );
}
{} the catch (Exception ex)
Logger. The error (" add Redisd key expired abnormal event listeners!" The ex);
}

Listen for an event is as follows:

/* *
* listen to subscribe to the channel to receive the message of the callback
*/
@ Override
Public void onMessage (String channel, String message) {
Logger. The info (" [onMessage] ");
Logger. The info (" channel="+ channel);
Logger. The info (" message="+ message);

}

Now the problem is there are Key in the Redis expires, the onMessage event will be called each program, and it is called at the same time, if with preventing repeat, please? Processing after the completion of the memory cache or library to ensure synchronization?

The above answers from IPAA tutorials network

CodePudding user response:

Single local locks, cluster distributed lock, to don't want to repeat, lock

CodePudding user response:

That does not repeat,

But one thing is not perfect enough, the backup host can also get a lock for processing,

Target is the primary server is no problem, has been provided by the master server service, not out of the problem is not the default request the standby server,

CodePudding user response:

Share the benefits a Java good underlying learning station www.java52.com
, TCC distributed scheduler, source of the underlying source code implementation of distributed scheduling engine, making the well-known the underlying implementation of a distributed framework, and the source code parsing, JDK multi-threading, container, NIO the underlying implementation, giant interview necessary knowledge
  • Related