RabbitMQ应用2

RabbitMQ应用2

一.实际业务逻辑

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

1.项目的创建和准备

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

2.代码实现

Controller

java 复制代码
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

java 复制代码
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

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

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

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

2.代码实现

properties

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

Listener

java 复制代码
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

java 复制代码
@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

java 复制代码
@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

java 复制代码
@Data
public class OrderInfo implements Serializable {
    private String OrderId;
    private String name;
}

消费者(物流)代码

Listener

java 复制代码
@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

java 复制代码
@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;
    }
}
相关推荐
RainbowSea4 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea4 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
ChinaRainbowSea10 小时前
1. 初始 RabbitMQ 消息队列
java·中间件·rabbitmq·java-rabbitmq
千层冷面11 小时前
RabbitMQ 发送者确认机制详解
分布式·rabbitmq·ruby
ChinaRainbowSea11 小时前
3. RabbitMQ 的(Hello World) 和 RabbitMQ 的(Work Queues)工作队列
java·分布式·后端·rabbitmq·ruby·java-rabbitmq
hycccccch21 小时前
Canal+RabbitMQ实现MySQL数据增量同步
java·数据库·后端·rabbitmq
陈平安Java and C1 天前
RabbitMQ简单介绍和安装
rabbitmq
RainbowSea1 天前
4. RabbitMQ 发布确认的配置详细说明
java·消息队列·rabbitmq
雨会停rain1 天前
如何提高rabbitmq消费效率
分布式·rabbitmq