RabbitMQ学习笔记

RabbitMQ学习笔记

1、什么是消息中间件

消息中间件

是利用高效可靠的消息传递机制进行异步的数据传输,并基于数据通信进行分布式系统的集成。通过提供消息队列模型和消息传递机制,可以在分布式环境下扩展进程间的通信。

点对点模式。

点对点模式:消息生产者将消息发送到队列中,消息消费者从队列中接收消息。消息可以在队列中进行异步传输。

发布/订阅模式。

发布/订阅模式:发布订阅模式是通过一个内容节点来发布和订阅消息,这个内容节点称为主题(topic),消息发布者将消息发布到某个主题,消息订阅者订阅这个主题的消息,主题相当于一个中介。主题是的消息的发布与订阅相互独立,不需要进行基础即可保证消息的传递,发布/订阅模式在消息的一对多广播是采用

可以做什么?

​ 应用程序之间不采取直接通信,而是使用消息中间作为中介,做到数据的异步通信。开发人员不需要考虑网络协议和远程调用的问题,只需要通过各消息中间件所提供的api,就可以简单的完成消息推送,和消息接收的业务功能。

​ 消息的生产者将消息存储到队列中,消息的消费者不一定马上消费消息,可以等到自己想要用到这个消息的时候,再从相应的队列中去获取消息。这样的设计可以很好的解决,大数据量数据传递所占用的资源,使数据传递和平台分开,不再需要分资源用于数据传输,可以将这些资源用去其他想要做的事情上。

2、什么是RabbitMQ

RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台开放电信平台)框架上的。所有主要[编程语言均有与代理接口通讯的客户端库。

RabbitMQ工作原理:
在这里插入图片描述

AMQP

AMQP是一套公开的消息队列协议,最早在2003年被提出,它旨在从协议层定义消息通信数据的标准格式,为的就是解决MQ市场上协议不统一的问题。RabbitMQ就是遵循AMQP标准协议开发的MQ服务。

3、SpringBoot集成RabbitMQ

导入依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<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>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

配置基本属性

server:
  port: 8082
Spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: /


创建两个springboot modules

在这里插入图片描述

分别为生产者消费者

我们直接来集成rabbitMQ的direct模式

首先我们知道rabbitmq里面有队列和交换机的概念

通过把队列和交换机绑定,然后实现消息的传递

package com.heng.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhanggh
 * @date 2021/6/7 11:18
 **/
@Configuration
public class DirectConfiguration {

    /**
     * 声明一个交换机,并且取名为 direct_order_exchange
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("direct_order_exchange",true,false);
    }

    /**
     * 声明一个sms队列,并且quming为direct_sms_queue
     * @return
     */
    @Bean
    public Queue smsDirectQueue(){
        return new Queue("direct_sms_queue",true);
    }
    @Bean
    public Queue duanxinDirectQueue(){
        return new Queue("direct_duanxin_queue",true);
    }
    @Bean
    public Queue emailDirectQueue(){
        return new Queue("direct_email_queue",true);
    }

    /**
     * 将交换机和队列进行绑定
     * @return
     */
    @Bean
    public Binding smsDirectBinding(){
        return BindingBuilder.bind(smsDirectQueue()).to(directExchange()).with("sms");
    }
    @Bean
    public Binding emailDirectBinding(){
        return BindingBuilder.bind(emailDirectQueue()).to(directExchange()).with("email");
    }
    @Bean
    public Binding duanxinDirectBinding(){
        return BindingBuilder.bind(duanxinDirectQueue()).to(directExchange()).with("duanxin");
    }

}

然后我们模拟一下订单的生成,并把订单号放到队列中

@Autowired(required = false)
private RabbitTemplate rabbitTemplate;

public void makeOrder(String userId,String productId,int num){

    String orderId = UUID.randomUUID().toString();
    System.out.println("订单生产:" + orderId);
    String exchangeName = "fanout_order_exchange";
    String routeKey = "";
    rabbitTemplate.convertAndSend(exchangeName,routeKey,orderId);

}

接着我们模拟用户加入到购物车,这时把生成的订单号放到rabbitmq中

@Autowired
	private OrderService service;

	@Test
	void contextLoads() {
		service.makeOrder1("1","1",10);
	}

在这里插入图片描述

我们去网页端查看

交换机

在这里插入图片描述

队列

在这里插入图片描述

这时候可以发现声明的三个队列中有两个已经产生消息了,那为什么不是三个呢?

这是因为direct模式和fanout模式的区别就是有一个路由key,通过这个key将对应的消息传到对应的队列中。

生产者我们弄好了,接下来看消费者

消费者相对来言就是消费队列中的消息

package com.heng.direct;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author zhanggh
 * @date 2021/6/7 11:06
 **/
@Component
@RabbitListener(queues = {"direct_duanxin_queue"})
public class DirectDuanListen {
    @RabbitHandler
    public void Message(String message){
        System.out.println("接受消息:" + message);
    }
}

通过rabbitmq的注解@RabbitListener来进行监听,就可以消费到对应的消息了。

在这里插入图片描述
那么一个简单的direct模式就完成了。topic模式和fanout模式和direct集成方式差不多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值