When writing a module recently, Cache used, Because it's communication between two services, So there will be data inconsistency, Finally, we choose to useRedis 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) Subscribe to: 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. Usekafka As a data transmitter( Courier), Asynchronous implementation( Solving high concurrency problems)
2. Useredis Andcache As caching
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, suspend1s 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 existmessge That is, only the corresponding cache is emptied, If not, clear thecache 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 correspondencekey Caching 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 talking about, Has been fattening up

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