Spring boot microservices + SAGA + RabbitMQ (2024)

Couple of years ago, i have developed and explained a microservice application with spring cloud in this blog. You can check out that series of posts to learn about spring cloud eureka, gateway, resilience, jwt and mysql and access those services with an MVC application with SAGA architecture. I have also added RabbitMQ integration to simulate a mail service to send mails in the background. That was another series of posts starting here.

In this post, i have updated the SAGA microservice with RabbitMQ integration with the latest 2024 spring framework specs and explained what is changed. In order to understand this post, you need to check out the microservice system in the older posts first. The infrastructure there is still the same. But if you have confidence, you can get the latest codes from github and try to run them yourself by referencing this post. You can find all the updated code here. The choice is yours :)

TL,DR microservice infrastructure

As you can see in the picture, an MVC app is using the microservice architecture. The purpose of the system was to divide a tickets app into small services. Therefore i have developed an imaginary "aldimbilet.com" website. It means "i bought ticket". In this system, the MVC app is directly and only talking to gateway. And the gateway is redirecting the requests to Eureka which is responsible for loadbalancing and service registry. Eureka is redirecting the requests to 3 different services and their failovers. These are user services, event services and payment services. You can run these services multiple times for load balancing among them. Also each service can connect to their own database individually using different technologies. If these services are unavailable, gateway tells eureka to redirect the requests to failover services. resilience4j is used for this. So you can show "service unavailable" on the screen instead of getting 404 or connection refused errors. Config server is holding some of the properties of each service inside it, so that it can be centralized. It was not mandatory though.

RabbitMQ in the system can be used for sending mails. When a user buys a ticket or an event is cancelled, users must be notified with an email. These operations can be asynchronous background operations. Therefore rabbitmq queues are ideal for this. The email addresses are put into queues and mailservice listens to them. If any error happens during the mail sending operation, the messages can go to deadletter queue in rabbitmq. This wasn't mandatory but in order to keep track of errors, this would be a nice solution. The sequence to run these apps is also explained in the old posts. The running sequence should be Eureka -> Config server -> Gateway -> Other services.

What changed

In the Eureka app, java and spring boot versions are updated. The rest of the app worked just fine. There are some changes in the properties. I have changed the port to default 8761. Because i had troubles if i change it to another port. When Eureka is up, you can access it via localhost:8761.

In the Config server app, java and spring boot versions are updated. In the old application, i have developed it to get properties from github. Now the properties are directly inside the config server application. Connecting to git everytime is hard. Here in this properties, spring.profiles.active=native is important. Otherwise config server looks for a git connection. Also the application-native properties file is also necessary.

In the Gateway app, java and spring boot versions are updated. There is almost no changes in this project. I used to start the apps with the profile called "local" but realized it is not necessary and turned it into default in all projects.

Userservice, activityservice and paymentservice all have the same changes. All projects have java and spring boot versions updated. I have removed lombok. I have put the properties in config server, rather than github. I have changed the database properties to postgresql. The repositories was using Criteria api of hibernate, now they are JpaRepository interfaces without a single line of code. Don't forget to change the port information for Eureka. Also these services are now using the new spring security classes in the config classes. Payment service is sending messages to rabbitmq but there are no changes there.

Userservice, activityservice and paymentservice failover almost have no changes. They have java and spring boot versions updated. They have new eureka configs. Property files were carried into config server.

Lastly, there are small changes in the Thymeleaf pages of the MVC app. Of course java and spring boot versions are updated. In this app, if you get an answer from failover services, you would get a FeignException and catch it, because failover services return 503 code. Therefore you can easily show "service unavailable, please wait" message on the screen. Catching the FeignException is important because this app is using OpenFeign clients. You need to discover how to use this MVC app, it is a mess :D But it works. Just login first and go to the activities screen.

Mailservice is the same, it works the same since it is just a simulation. It receives messages from rabbitmq. I have accidentally created a private bean there :D Therefore you should directly get the latest version of this service. Of course, java version and spring boot version is updated here..

Up-to-date microservice is ready

This way, i have necromenced another old project. It made me happy to see most of the code i have written back then is still working in the year 2024. Even though some codes are obsolete, i recommend you to check out the old series of posts in the blog. Then you can add the changes in this blog. Or you can directly get the updated code and try to understand it by referencing the old posts. I don't have to explain the system from scratch here, since the main logic is all the same, just updated the codes. I wish you happy coding. See you at the next post :)


Leave a comment