RabbitMQ应用2

发布于:2025-04-03 ⋅ 阅读:(19) ⋅ 点赞:(0)

一.实际业务逻辑

订单系统中使用MQ(不写订单系统逻辑)

1.项目的创建和准备

  1. 先把空项目创建好
    在这里插入图片描述
  2. 在空项目中创建其他的spring项目

在这里插入图片描述

  1. 创建新的spring项目在这个空项目中
    在这里插入图片描述
  2. 项目创建好会出现一个文件包
    在这里插入图片描述
  3. 有些情况下需要手动添加Maven
    在这里插入图片描述
  4. 在里面选择Maven后,项目就能够使用XML配置文件了
    在这里插入图片描述

2.代码实现

Controller

package com.example.orderservice.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/create")
    public String create() {
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("","order.create","订单信息,订单ID: " + orderId);
        return "ok";
    }
}

Configuration

package com.example.orderservice.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.QueueBuilder;


@Configuration
public class RabbitMQConfig {

    @Bean("orderQueue")
    public Queue orderQueue() {
        return QueueBuilder.durable("order.create").build();
    }
}

properties

spring.application.name=orderservice
spring.rabbitmq.addresses=amqp://账号:密码@IP:端口号/虚拟机

二.物流系统使用MQ(不实现物流系统业务)

1.项目创建同订单(一样)

2.代码实现

properties

spring.application.name=logistics
server.port=9090
spring.rabbitmq.addresses=amqp://账号:密码@IP:端口号/虚拟机

Listener

package com.example.logistics.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class OrderListener {
    @RabbitListener(queues = "order.create")
    public void handMessage(String orderInfo) {
        System.out.println("接收到订单消息: " + orderInfo);
    }
}

使用对象来发送接收消息

生产者(订单)代码

Controller

@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/create")
    public String create() {
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("","order.create","订单信息,订单ID: " + orderId);
        return "ok";
    }


    @RequestMapping("/create2")
    public String create2() {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(UUID.randomUUID().toString());
        orderInfo.setName("商品 "  + new Random().nextInt(100));
        rabbitTemplate.convertAndSend("","order.create", orderInfo);
        return "ok2";
    }
}

Configuration

@Configuration
public class RabbitMQConfig {

    @Bean("orderQueue")
    public Queue orderQueue() {
        return QueueBuilder.durable("order.create").build();
    }

    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter jackson2JsonMessageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
        return rabbitTemplate;
    }
}

model

@Data
public class OrderInfo implements Serializable {
    private String OrderId;
    private String name;
}

消费者(物流)代码

Listener

@Component
@RabbitListener(queues = "order.create")
public class OrderListener {
    @RabbitHandler
    public void handMessage1(String orderInfo) {
        System.out.println("接收到订单消息String: " + orderInfo);
    }

    @RabbitHandler
    public void handMessage2(OrderInfo orderInfo) {
        System.out.println("接收到订单消息OrderInfo: " + orderInfo);
    }

}

Configuration

@Configuration
public class RabbitMQConfig {
    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter jackson2JsonMessageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter);
        return rabbitTemplate;
    }
}