RabbitMQ应用2
一.实际业务逻辑
订单系统中使用MQ(不写订单系统逻辑)
1.项目的创建和准备
- 先把空项目创建好
- 在空项目中创建其他的spring项目

- 创建新的spring项目在这个空项目中
- 项目创建好会出现一个文件包
- 有些情况下需要手动添加Maven
- 在里面选择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;
}
}