生产者工程
1、RabbitTemplate配置类
无,其实并不是没有,而是将配置类的方式换成了注解的。但实际上开发建议用配置类,注解也就图一乐。
2、消息发送服务
package com.sky.service.impl;
import com.sky.service.OrderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
/**
* @author 尹稳健~
* @version 1.0
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void makeOrder(String userId, String productId, int num) {
// 1:根据商品id查询库存是否充足
//2:保存订单
String orderId = UUID.randomUUID().toString();
System.out.println("订单生成成功"+orderId);
//3:通过MQ来完成消息的分发
// 参数1 : 交换机 参数2:路由key / 队列名称 参数3 :消息内容
String exchangeName = "fanout_order_exchange";
String routeKey = "";
rabbitTemplate.convertAndSend(exchangeName,routeKey,orderId);
}
@Override
public void makeOrderDirect(String userId, String productId, int num) {
// 1:根据商品id查询库存是否充足
//2:保存订单
String orderId = UUID.randomUUID().toString();
System.out.println("订单生成成功"+orderId);
//3:通过MQ来完成消息的分发
// 参数1 : 交换机 参数2:路由key / 队列名称 参数3 :消息内容
String exchangeName = "direct_order_exchange";
String routeKey = "";
rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);
}
@Override
public void makeOrderTopic(String userId, String productId, int num) {
// 1:根据商品id查询库存是否充足
//2:保存订单
String orderId = UUID.randomUUID().toString();
System.out.println("订单生成成功"+orderId);
//3:通过MQ来完成消息的分发
// 参数1 : 交换机 参数2:路由key / 队列名称 参数3 :消息内容
String exchangeName = "topic_order_exchange";
String routeKey = "com.email.duanxin.sky";
rabbitTemplate.convertAndSend(exchangeName,routeKey,orderId);
}
}
2、消费者工程
消费者短信服务:
package com.sky.service.topic;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Service;
/**
* @author 尹稳健~
* @version 1.0
*/
@Service
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "duanxin.topic.queue",autoDelete = "false",durable = "true"),
exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
key = "#.duanxin.#"
))
public class TopicDuanxinConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("duanxin topic ---->"+message);
}
}
消费者邮件服务:
package com.sky.service.topic;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Service;
/**
* @author 尹稳健~
* @version 1.0
*/
@Service
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "email.topic.queue",autoDelete = "false",durable = "true"),
exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
key = "*.email.#"
))
public class TopicEmailConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("email topic ---->"+message);
}
}
消费者短信息服务:
package com.sky.service.topic;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Service;
/**
* @author 尹稳健~
* @version 1.0
*/
@Service
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "sms.topic.queue",autoDelete = "false",durable = "true"),
exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
key = "com.#"
))
public class TopicSMSConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("sms topic ---->"+message);
}
}
测试
生产者发送消息
消费者接收消息
接收成功!
并且将信息成功应答!
小结
相较于注解和配置类的开发
- 个人觉得配置类开发很舒服,因为逻辑清晰,方便维护。
- 当然注解也有注解的好处,简单便捷,但是有一些复杂操作时,就显得有点鸡肋了。