RabbitMQ Bus implementation¶
Introduction¶
As part of the Distributed Data annex, I shall integrate RabbitMQ into my Ramses project to enable scalability, minimize coordination, redundancy & readiness for evolution. Within Ramses a few integration patterns are used(there are more, I don’t know each one as there are over 40.):
Topics and subscription
Point to point: Queues(relevant)
Micro Services
Messaging
Transformation(relevant)
Execution¶
In order to integrate rabbit mq into my back-end REST spring boot API I first created an overview of the inner communication that was required.
I then began by creating my Ramses exchange in RabbitMQ.
And then implemented the connection string into my application properties.
spring.rabbitmq.host=10.0.0.34
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
#todo add secrets to env file
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.exchange=ramses.exchange
#spring.rabbitmq.routingkey=ramses.routingkey
spring.rabbitmq.queue=ramses.queue
This is a required change in both CommentService & GameService.
I then created a CommentDTO in my GameService (Transformation) in order to be able to handle creating a list of comments based off of the messages in the queue later on.
public class Comment {
private int Id;
private String CommentMessage;
private int gameId;
These attributes correspond with the comment model in the Comment Microservice. In my non-trivial microservice page found here: https://se6.oksolution.nl/learning/scaling/nontrivial.html I elaborated further on my implementation of the rabbitmq configuration.
One thing unmentioned in this about the implementation is the adjustments made to the GameController & CommentMessageController
@GetMapping("/{id}/comment")
public List<Comment> getAllComments(@PathVariable final int id) {
//final Optional<Game> game = _GameCollection.getGame(id);
return _GameCollection.getGameComments(id);
//return _rabbitSender.getAllComments(id);
//return (List<Comment>) _GameCollection.getGame(id).map(p-> {
// _GameCollection.getGameComments(id);
// return ResponseEntity.noContent().build();
//}).orElseThrow(()-> new NoResultException());
}
I left the comments if I’d need to debug another time.
@RabbitListener(queues = "getAllComments")
public List<Comment> getComments(
String id
){
int realId = Integer.parseInt(id);
List<Comment> allCommentsFromGame = commentcollection.getAllGameComments(realId);
//List<Comment> comments = commentcollection.getAllComments();
//List<Comment> test_comments = commentcollection.getGameComments(id);
//return test_comments;
return allCommentsFromGame;
}
I don’t understood why but the id being passed from the queue was being parsed only as a string. I assume the usual use-case is to parse UUIDs into the IDs. Here too are comments stored in case I’d need to debug it again.
I initially made a mistake setting up my Comment DTO which I found out later after long periods of debugging, the getters & setters were missing.
Here is the getAllComments queue during a load test: