交换机是由双方同时声明,队列是由接收方声明并配置绑定关系
1.先使用rabbitmq包进行开发【偏底层】,适合了解原理
目前是每两个微服务之间的通信使用一个单独的交换机:
1.订单微服务和 餐厅微服务之间的通信
,是在订单微服务中声明,监听餐厅发送的消息
1.首先创建ConnectionFactory
2.通过ConnectionFactory创建Connection
3.通过Connection创建Channel
4.声明交换机
5.声明消息队列 【如果还需要监听其他微服务发送的消息,还是用同一个队列】
6.将交换机和队列绑定,确定绑定key
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setHost("localhost");
try (Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel()) {
/*---------------------restaurant---------------------*/
channel.exchangeDeclare(
"exchange.order.restaurant",//交换机名称
BuiltinExchangeType.DIRECT,//交换机类型
true, //是否持久化
false,//如果没有队列需要该交换机,mq是否将其删除
null);//是否需要特殊参数
channel.queueDeclare(
"queue.order",//队列名称
true,//是否持久化
false,//该队列是不是被这个connection独占
false,
null);//是否需要特殊参数
//将队列和交换机绑定,确定绑定key
channel.queueBind(
"queue.order",
"exchange.order.restaurant",
"key.order");
2.订单微服务中监听 餐厅,骑手微服务发送的消息
public void handleMessage() throws IOException, TimeoutException, InterruptedException {
log.info("start linstening message");
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setHost("localhost");
try (Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel()) {
/*---------------------restaurant---------------------*/
channel.exchangeDeclare(
"exchange.order.restaurant",//交换机名称
BuiltinExchangeType.DIRECT,//交换机类型
true, //是否持久化
false,//如果没有队列需要该交换机,mq是否将其删除
null);//是否需要特殊参数
channel.queueDeclare(
"queue.order",//队列名称
true,//是否持久化
false,//该队列是不是被这个connection独占
false,
null);//是否需要特殊参数
//将队列和交换机绑定,确定绑定key
channel.queueBind(
"queue.order",
"exchange.order.restaurant",
"key.order");
/*---------------------deliveryman---------------------*/
channel.exchangeDeclare(
"exchange.order.deliveryman",
BuiltinExchangeType.DIRECT,
true,
false,
null);
channel.queueBind(
"queue.order",
"exchange.order.deliveryman",
"key.order");
}
}
声明了一个队列,俩个交换机, 将这个队列用相同的绑定key,将其绑定在两个交换机上
3.测试:
使用main方法进行测试
public static void handleMessage() throws IOException, TimeoutException, InterruptedException {
log.info("start linstening message");
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setHost("localhost");
try (Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel()) {
/*---------------------restaurant---------------------*/
channel.exchangeDeclare(
"exchange.order.111",//交换机名称
BuiltinExchangeType.DIRECT,//交换机类型
true, //是否持久化
false,//如果没有队列需要该交换机,mq是否将其删除
null);//是否需要特殊参数
channel.queueDeclare(
"queue.order",//队列名称
true,//是否持久化
false,//该队列是不是被这个connection独占
false,
null);//是否需要特殊参数
//将队列和交换机绑定,确定绑定key
channel.queueBind(
"queue.order",
"exchange.order.restaurant",
"key.order");
/*---------------------deliveryman---------------------*/
channel.exchangeDeclare(
"exchange.order.222",
BuiltinExchangeType.DIRECT,
true,
false,
null);
channel.queueBind(
"queue.order",
"exchange.order.deliveryman",
"key.order");
}
}
public static void main(String[] args) throws InterruptedException, TimeoutException, IOException {
handleMessage();
}
1.启动rabbitmq网页端:
2.找到exchange
运行后,能够找到,即绑定成功。