This project utilizes RabbitMQ message queue to enable efficient and reliable communication between two Spring Boot microservices. RabbitMQ is a widely used message broker in microservices architectures, and this project showcases the integration of RabbitMQ to enable asynchronous and resilient communication between services.
👉 More information about RabbitMQ
- IDE
- Java 17+
- Gradle 7.5+
- Auth-service
- Rabbitmq Configuration
@Configuration
public class RabbitmqConfig {
private final String exchange = "auth-exchange";
private final String authQueue = "auth-to-user-register-queue";
private final String authKey = "auth-to-user-register-key";
@Bean
DirectExchange authExchange() {
return new DirectExchange(exchange);
}
@Bean
Queue registerQueue() {
return new Queue(authQueue);
}
@Bean
public Binding registerBinding(final DirectExchange authExchange, final Queue registerQueue) {
return BindingBuilder.bind(registerQueue).to(authExchange).with(authKey);
}
}
- Producer
@Service
public class RegisterProducer {
private final RabbitTemplate rabbitTemplate;
private final String exchange = "auth-exchange";
private final String authKey = "auth-to-user-register-key";
public RegisterProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendRegisterMessage(RegisterModel registerModel) {
rabbitTemplate.convertAndSend(exchange, authKey, registerModel);
}
}
- Model
public class RegisterModel implements Serializable {
private Long authId;
private String name;
private String surname;
private String email;
private String password;
//getter and setter
}
- User-service
- Consumer
@Service
public class RegisterConsumer {
private final UserService userService;
public RegisterConsumer(UserService userService) {
this.userService = userService;
}
@RabbitListener(queues = "auth-to-user-register-queue")
public void registerConsumer(RegisterModel registerModel) {
userService.save(registerModel);
}
}
- Clone the project to your local machine.
https://github.com/ayse-ozcan/rabbitmq-spring-boot.git
- If you have Docker installed locally, you can quickly start RabbitMQ, PostgreSQL, and MongoDB servers using Docker.
- RabbitMQ
docker run -d -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=secret -p 5672:5672 -p 15672:15672 rabbitmq:3-management
- PostgreSQL
docker run -d --name some-postgres -e POSTGRES_PASSWORD=secret -e PGDATA=/var/lib/postgresql/data/pgdata -v /custom/mount:/var/lib/postgresql/data -p 5432:5432 postgres
- MongoDB
docker run -d -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret -p 27017:27017 mongo
- Make sure microservices are running.
- Open Postman, a popular API testing tool.
- Create a new request in Postman.
- Choose the HTTP method (POST) for your request.
- Enter the URL "http://localhost:9090/api/v1/auth/save" as the endpoint.
- Click on the "Send" button to send the request to the specified URL.
- You will observe that the request sent from the auth-service is also recorded in the user-service.