Login
网站首页 > 文章中心 > 其它

Using Redis Pub/Sub with Spring Boot

作者:小编 更新时间:2023-10-12 16:01:26 浏览量:12人看过

This?application consist of multiple micro services which interact with each other. We would create event driven construct, where services would connect to messaging layer and would publish and subscribe events to messaging layer.

https://www.metakoder.com/blog/using-redis-with-spring-boot

here?.We would set up messaging between these two services. Whenever a new payment would be created using Payment Service, An event would be published to the messaging layer. Our Account service would receive this event and update the balance in accounts.

release of the repository.

Step 1: Setting up Redis server

Messaging Layer.

embedded-redis?as dependency in our maven project.

dependency>

stop?redis functions with the messaging-layer service's life-cycle.

Below is the sample code.

redisServer.stop();

}

}

Step 2: Publish payment event on Redis Server

payment-service?to publish event on messaging layer.

Add Redis client dependency

We need to add below dependency to out maven project. This is required to connect Redis Server.

dependency>

Add Messaging configuration

ChannelTopic?.

ChannelTopic is the topic/queue where our Payment Service would publish the events. We are configuring topic named "payments" here. This is the same topic on which Account Service would subscribe to.

return template;

}

That's all needed to setup in our application to connect and publish event to our Redis server.

Payment?object to string correctly on the topic. Here I am publishing whole payment object as an event but we could create a custom event as well to publish.

Now we can autowire our Redis Template to any service layer to send a event to our payment topic.

RedisMessagePublisher?and used that in service layer PaymentEventHandler.

RedisMessagePublisher. But Redis Template can be used directly from any class. Below is how I am using the Redis Template.

publisher.publish(payment);

}

}

Step 3: Subscribe event from Redis Server

Account Service?to subscribe events from our "payments" topic at Redis Server.

Add Redis client dependency

We need to add below dependency to out maven project.

dependency>

Add Message Listener

Message Listener?and from there we can use the event as we need. We need to implement onMessage method of MessageListener interface. In our case I am using this event to adjust the balance in Accounts.

Message Listener.

new String(message.getBody()));

}

}

Add Messaging configuration

RedisMessageListenerContainer?. Below is how I have have configured the relevant components to subscribe the event.

return container;

}

}

We need ChannelTopic, JedisConnectionFactory and we need to configure these in RedisMessageListenerContainer along with the Message Listener.

That's it!!! This is all you need to use Redis Pub/Sub with Spring boot application.

here. And run above mentioned Accounts and Payments services with Messaging layer service.

}'

Lets check the created accounts:

                    }

}

}

]

}

}
}'

This will create new payment in payment service and an event is published at our Redis payments topic. As Account Service subscribe to this topic , It will receive the event and process it. You will notice below log in Account Service



And now if you fetch the accounts again. You will notice that balance in accounts have been updated

                    }

}

}

]

}

}

The benefit of using the Messaging Layer is that there is no direct communication between services and this could result in better decoupling and scalability of the application.

以上就是土嘎嘎小编为大家整理的Using Redis Pub/Sub with Spring Boot相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章