引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.properties 配置
spring.rabbitmq.addresses=192.168.0.105:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=150000
server.port=8081
server.servlet.context-path=/
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.http.encoding.charset=utf-8
spring.jackson.default-property-inclusion=non_null
向rabbitmq发送消息
@Component
public class OrderSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendOrder(Order order) throws Exception{
CorrelationData correlation=new CorrelationData();
correlation.setId(order.getMessageId());
rabbitTemplate.convertAndSend("order-exchange","order.send",order, correlation);
}
}
增加ExChange(order-exchange)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200223163339960.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM0NDkwNDY=,size_16,color_FFFFFF,t_70)
增加Queue(order-queue)
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020022316342959.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM0NDkwNDY=,size_16,color_FFFFFF,t_70)
绑定order-queue到order-exchange
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200223163554300.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM0NDkwNDY=,size_16,color_FFFFFF,t_70)
单元测试
@Autowired
private OrderSender orderSender;
@Test
public void sendOrder() throws Exception {
Order order=new Order();
order.setId(1);
order.setName("测试订单1");
order.setMessageId(System.currentTimeMillis()+"$"+ UUID.randomUUID().toString());
orderSender.sendOrder(order);
}