When writing a module recently , Cache used , Because it's communication between two services , So there will be data inconsistency , Finally, we choose to use Redis To solve the problem .

What is publish and subscribe ?

It's like our usual circle of friends , We're a publisher when we send our friends , And the person who can see this circle of friends is a subscriber , Subscribers can like the published messages according to their own preferences , Comments and other operations .

Here is a picture of the image :


There are three steps for a complete publish subscription :

*
Subscriber to publisher ( channel ) Make a subscription : For example, we want to play , Then you can subscribe to a program , Every update will send us a notification

*
Publisher sends message : The publisher is like a starting gun , Send a message to all subscribers

*
Subscribers receive messages and make actions : When a publisher publishes a message , The action that a subscriber can process or respond to a message

When to use ?

In a distributed scenario , If you need to update the status of each node, you can use publish subscribe mode .

Business scenario :
I was writing a correction service the other day , The correction service needs to go to the question bank service first and call the question to have been answered by the user , After the recalibration is completed, the corrected results need to be returned to the question bank service for storage .

Solution :
1. use kafka As a data transmitter ( courier ), Asynchronous implementation ( Solving high concurrency problems )
2. use redis And cache As cache
3. Publisher serves the question bank , Subscribers serve for correction ( Issue a message after the topic service updates the rating information )

Sample code
// subscriber : // Execute on initialization subscribeRedisChannel(CHANNEL_NAME); // Subscribed Channels public void
subscribeRedisChannel(final String channel) { log.info("add redis PUB/SUB ,
refresh Channel is " + channel); new Thread(() -> { while (true) { try {
jedis.subscribe(refreshCachePubSub, channel); }catch (Exception e) { log.warn(
"Redis Sub Exception", e); // Subscription exception , suspend 1s thread try { Thread.sleep(1000); } catch
(Exception e2) { log.warn("Sleep Exception", e2); } } } }).start(); } // Refresh cache
private JedisPubSub refreshCachePubSub = new JedisPubSub() { @Override public
void onMessage(String channel, String message) { log.info("refresh channel : "
+ channel +" || refresh key : " + message); if (CHANNEL_NAME.equals(channel)) {
// If present messge That is, only the corresponding cache is emptied , If not, clear the cache All caches for if (StringUtils.isBlank(message)) {
cacheApi.getCache().invalidateAll(); }else {
cacheApi.getCache().invalidate(Long.parseLong(message)); } } } };// Publisher : //
After the update is complete , Active call , Update correspondence key Cache for public void invalidCache(String key) {
redisService.publishRedisChannel(RedisService.CHANNEL_NAME, key); }
The blog that stopped for a long time has finally been updated .
People can't be idle ... As soon as I'm free, I'm more and more lazy
It's like losing weight ... Always say , Has been fattening up

The meaning of life lies in , Constantly enrich yourself , Constantly challenge yourself , Constantly embracing change .