需要在本地安装Rabbit的服务器
安装教程
刚开始进入公司的时候,我们公司的架构还比较单一,就采用一种单体结构,而单体结构的话它是把所以的业务都堆积在一个项目里面,但是随着公司业务不断发展和推进,我们公司的负责人就开始把项目进行分裂,变成了一个分布式的架构,就把系统进行了一个拆分,在拆分的过程中我们就要考虑到一个问题,比如在拆分过程中,我们把用户模块和下单模块做了一个拆分,但是它们之间的数据要进行一个交互,因为在下单的时候需要获取到用户的一些资料,所以我们公司采用了一个消息队列,在选择的时候就一直在思考用什么消息队列,这个时候就选择了RebbitMQ,而我自己使用RebbitMQ的一个核心的感受,就是它是一个异步的多线程,是一个分发的多线程机制,然后它可以让我们网站的一个效率做成倍的提升,因为它是异步的所以它可以让我们处理数据的能力更加的高效和稳健
# 服务端口
server:
port: 8081
#配置rabbitmq服务 测试不用写,默认本机
spring:
rabbitmq:
username: guest #默认账号
password: guest #默认密码
virtual-host: /
host: localhost
port: 5672
#消息确认配置项
#确认消息已发送到交换机: Exchange
publisher-confirm-type: correlated
#确认消息已发送到队列: Queue
publisher-returns: true
package com.exam.RebbitMQ.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfig {
//1:声明注册fanout模式的交换机,参数1:对应的service的fanoutName,参数2:持久化(true,false),参数3:自动删除(false/true)
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout_order_exchang", true, false);
}
//2:声明队列 sms.fanout.queue,email.fanout.queue,duanxin.fanout.queue
//参数1:名字,参数2:持久化队列true
//短信队列
@Bean
public Queue smsQueue() {
System.err.println("执行了sms");
return new Queue("sms.fanout.queue",true);
}
@Bean
public Queue duanxinQueue() {
System.err.println("执行了duanxin");
return new Queue("duanxin.fanout.queue",true);
}
//邮箱队列
@Bean
public Queue emailQueue() {
System.err.println("执行了email");
return new Queue("email.fanout.queue",true);
}
//3:完成绑定关系(队列和交换机完成绑定关系)
@Bean
public Binding smsBinding() {
//把smsQueue放到fanoutExchange交换机上面
return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
}
@Bean
public Binding duanxinBinding() {
//把duanxinQueue放到fanoutExchange交换机上面
return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
}
@Bean
public Binding emailBinding() {
//把emailQueue放到fanoutExchange交换机上面
return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
}
}
package com.exam.RebbitMQ.service;
public interface OrderService {
void makeOrder(String userid,String productid,int num);
}
package com.exam.RebbitMQ.service.Impl;
import java.util.UUID;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.exam.RebbitMQ.service.OrderService;
@Service
public class OrderServiceImpl implements OrderService{
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 模拟用户下单
**/
public void makeOrder(String userid,String productid,int num) {
//1.根据商品ID查询商品是否充足
//2.保存订单
String orderId = UUID.randomUUID().toString();
System.err.println("订单生成成功"+orderId);
//3.通过MQ来完成消息的分发
//参数1:交换机 参数二:路由key/queue队列名称 参数三:消息内容
String exchangName ="fanout_order_exchang";
String routingKey = "";
rabbitTemplate.convertAndSend(exchangName, routingKey, orderId);
}
}
package com.huyi.rabbitmq;
import com.huyi.rabbitmq.service.OrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitMqApplicationTests {
@Autowired
private OrderService orderService;
@Test
void contextLoads() {
orderService.makeOrder("1","1", 18);
}
}
# 服务端口
server:
port: 8082
#配置rabbitmq服务 测试不用写,默认本机
spring:
rabbitmq:
username: guest #默认账号
password: guest #默认密码
virtual-host: /
host: localhost
port: 5672
#消息确认配置项
#确认消息已发送到交换机: Exchange
publisher-confirm-type: correlated
#确认消息已发送到队列: Queue
publisher-returns: true
package com.huyi.rabbitmq_consumber.service;
public interface SmsConsumerService {
void reviceMessage(String message);
}
package com.huyi.rabbitmq_consumber.service.Impl;
import com.huyi.rabbitmq_consumber.service.SmsConsumerService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
//注意:这里要和生产者RabbitMqConfig文件中的名字对应起来
@RabbitListener(queues = {"sms.fanout.queue"})
public class SmsConsumerServiceImpl implements SmsConsumerService {
@RabbitHandler
public void reviceMessage(String message) {
System.err.println("sms_fanout--接收到了订单信息");
}
}
package com.huyi.rabbitmq_consumber.service;
public interface EmailConsumerService {
void reviceMessage(String message);
}
package com.huyi.rabbitmq_consumber.service.Impl;
import com.huyi.rabbitmq_consumber.service.EmailConsumerService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
@RabbitListener(queues = {"email.fanout.queue"})
public class EmailConsumerServiceImpl implements EmailConsumerService {
@RabbitHandler
public void reviceMessage(String message) {
System.err.println("Email_fanout--接收到了订单信息"+message);
}
}
package com.huyi.rabbitmq_consumber.service;
public interface DuanxinConsumerService {
void reviceMessage(String message);
}
package com.huyi.rabbitmq_consumber.service.Impl;
import com.huyi.rabbitmq_consumber.service.DuanxinConsumerService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component
@RabbitListener(queues = {"duanxin.fanout.queue"})
public class DuanxinConsumerServiceImpl implements DuanxinConsumerService {
@RabbitHandler
public void reviceMessage(String message) {
System.err.println("Duanxin_fanout--接收到了订单信息"+message);
}
}