Event-Driven MicroServices using SpringBoot and RabbitMQ

Project Achitecture

  • RabbitMQ Producer: Order service
  • RabbitMQ Consumer:
    • Stock service:
    • Email service:

Create and Setup Microservices

access spring initializr, each microservice needs install plugins:

  • Spring for RabbitMQ
  • Spring Web
  • Lombok

configure the port:

  • Order service: 8080(default)
  • Stock service: 8081
  • Email service: 8082
// filepath: stock-service/src/main/resources/application.properties
server.port=8081


// filepath: email-service/src/main/resources/application.properties
server.port=8082

RabbitMQ Producer – Order Service

Configure RabbitMQ step by step

  • configure the basic variables for avoiding the hard coding.
// order-service/src/main/resources/application.properties
rabbitmq.queue.order.name=order
rabbitmq.exchange.name=order_exchange
rabbitmq.binding.routing.key=order_routing_key
  • create config package and RabbitMQConfig to auto create queue, exchange, routing-key binding and redefint the converter(support json).
// order-service/src/main/java/com/example/orderservice/config/RabbitMQConfig.java
package com.example.orderservice.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
   
    // @Value is a Spring Boot annotation used for injecting values into variables
    @Value("${rabbitmq.queue.order.name}")
    String queueName;
    @Value("${rabbitmq.exchange.name}")
    String exchangeName;
    @Value("${rabbitmq.binding.routing.key}")
    String routingKey;

    // spring bean for queue -- order queue
    // @Bean is an annotation in Spring Boot used to declare a method that returns an object to be managed by the Spring container as a bean.
    // The method is used to tell the container how to create the bean instance, and the returned object is registered with the container for use throughout the program.
    @Bean
    public Queue orderQueue(){
   
        return new Queue(queueName);
    }

    // spring bean for exchange
    @Bean
    public TopicExchange orderExchange(){
   
        return new TopicExchange(exchangeName);
    }

    // spring bean for binding between exchange and queue using routing key
    @Bean
    public Binding binding(){
   
        return BindingBuilder
                .bind(orderQueue())
                .to(orderExchange())
                .with(routingKey);
    }

    // message converter
    @Bean
    public MessageConverter converter(){
   
        return new Jackson2JsonMessageConverter();
    }

    // configure RabbitMQTemplate
    @Bean
    public AmqpTemplate amqpTemplate(ConnectionFactory connectionFactory){
   
        RabbitTemplate rabbitTemplate = new R
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值