您好,欢迎访问一九零五行业门户网

SpringBoot如何集成RabbitMQ

一、rabbitmq介绍rabbitmq是实现amqp(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、 高可用性等方面表现不俗。rabbitmq主要是为了实现系统之间的双向解耦而实现的。当生产者大量产生数据时,消费者无法快速消费,那么需要一个中间层。保存这个数据。
amqp,即advanced message queuing protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件的主要目的在于解耦组件,使得消息发送者和接收者互不干扰且相互独立。因此,发送者无需知道使用者的存在,反之亦然。amqp的显著特征包括面向消息、队列、路由(包括点对点和发布/订阅)、可靠性和安全性。
rabbitmq是一个开源的amqp实现,服务器端用erlang语言编写,支持多种客户端,如:python、ruby、.net、java、jms、c、php、actionscript、xmpp、stomp等,支持ajax。这项技术在分布式系统中存储和转发消息方面表现出了良好的易用性、扩展性和高可用性。
二、相关概念通常我们谈到队列服务, 会有三个概念: 发消息者、队列、收消息者,rabbitmq 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (exchange). 这样发消息者和队列就没有直接联系, 转而变成发消息者把消息给交换器, 交换器根据调度策略再把消息再给队列
左侧 p 代表 生产者,也就是往 rabbitmq 发消息的程序。
中间即是 rabbitmq,其中包括了 交换机 和 队列。
右侧 c 代表 消费者,也就是往 rabbitmq 拿消息的程序。
其中比较重要概念有 4 个,分别为:虚拟主机,交换机,队列,和绑定。
虚拟主机:一个虚拟主机持有一组交换机、队列和绑定。为什么需要多个虚拟主机呢?很简单,rabbitmq当中,用户只能在虚拟主机的粒度进行权限控制。 因此,如果需要禁止a组访问b组的交换机/队列/绑定,必须为a和b分别创建一个虚拟主机。每一个rabbitmq服务器都有一个默认的虚拟主机“/”。
交换机:exchange 用于转发消息,但是它不会做存储 ,如果没有 queue bind 到 exchange 的话,它会直接丢弃掉 producer 发送过来的消息。
这里有一个比较重要的概念:路由键 。根据路由键,交换机将消息转发到对应的队列中。
绑定:也就是交换机需要和队列相绑定,这其中如上图所示,是多对多的关系。
springboot集成rabbitmq非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持。
三、简单使用1.配置pom包主要是添加spring-boot-starter-amqp的支持
<dependency><groupid>org.springframework.boot</groupid><artifactid>spring-boot-starter-amqp</artifactid></dependency>
2.配置文件配置rabbitmq的安装地址、端口以及账户信息.
spring.application.name=spirng-boot-rabbitmqspring.rabbitmq.host=192.168.0.86spring.rabbitmq.port=5672spring.rabbitmq.username=adminspring.rabbitmq.password=123456
3.队列配置@configurationpublic class rabbitconfig {@beanpublic queue queue() {return new queue("hello");}}
4.发送者rabbittemplate是springboot 提供的默认实现public class hellosender {@autowiredprivate amqptemplate rabbittemplate;public void send() {string context = "hello " + new date();system.out.println("sender : " + context);this.rabbittemplate.convertandsend("hello", context);}}
5.接收者@component@rabbitlistener(queues = "hello")public class helloreceiver {@rabbithandlerpublic void process(string hello) {system.out.println("receiver : " + hello);}}
6.测试@runwith(springrunner.class)@springboottestpublic class rabbitmqhellotest {@autowiredprivate hellosender hellosender;@testpublic void hello() throws exception {hellosender.send();}}
注意:发送者和接收者的queue name必须一致,不然不能接收
多对多使用
一个发送者,n个接收者或者n个发送者和n个接收者会出现什么情况呢?
一对多发送
对上面的代码进行了小改造,接收端注册了两个receiver,receiver1和receiver2,发送端加入参数计数,接收端打印接收到的参数,下面是测试代码,发送一百条消息,来观察两个接收端的执行效果.
@testpublic void onetomany() throws exception {for (int i=0;i<100;i++){neosender.send(i);}}
结果如下:
receiver 1: spirng boot neo queue ****** 11
receiver 2: spirng boot neo queue ****** 12
receiver 2: spirng boot neo queue ****** 14
receiver 1: spirng boot neo queue ****** 13
receiver 2: spirng boot neo queue ****** 15
receiver 1: spirng boot neo queue ****** 16
receiver 1: spirng boot neo queue ****** 18
receiver 2: spirng boot neo queue ****** 17
receiver 2: spirng boot neo queue ****** 19
receiver 1: spirng boot neo queue ****** 20
根据返回结果得到以下结论
一个发送者,n个接受者,经过测试会均匀的将消息发送到n个接收者中
多对多发送
复制了一份发送者,加入标记,在一百个循环中相互交替发送
@testpublic void manytomany() throws exception {for (int i=0;i<100;i++){neosender.send(i);neosender2.send(i);}}
结果如下:
receiver 1: spirng boot neo queue ****** 20
receiver 2: spirng boot neo queue ****** 20
receiver 1: spirng boot neo queue ****** 21
receiver 2: spirng boot neo queue ****** 21
receiver 1: spirng boot neo queue ****** 22
receiver 2: spirng boot neo queue ****** 22
receiver 1: spirng boot neo queue ****** 23
receiver 2: spirng boot neo queue ****** 23
receiver 1: spirng boot neo queue ****** 24
receiver 2: spirng boot neo queue ****** 24
receiver 1: spirng boot neo queue ****** 25
receiver 2: spirng boot neo queue ****** 25
结论:和一对多一样,接收端仍然会均匀接收到消息.
四、高级使用//对象的支持//springboot以及完美的支持对象的发送和接收,不需要格外的配置。//发送者public void send(user user) {system.out.println("sender object: " + user.tostring());this.rabbittemplate.convertandsend("object", user);}...//接受者@rabbithandlerpublic void process(user user) {system.out.println("receiver object : " + user);}
结果如下:
sender object: user{name='neo', pass='123456'}
receiver object : user{name='neo', pass='123456'}
1.topic exchange在rabbitmq中,topic是最灵活的一种方式,它允许根据routing_key随意绑定到不同的队列
首先对topic规则配置,这里使用两个队列来测试
@configurationpublic class topicrabbitconfig {final static string message = "topic.message";final static string messages = "topic.messages";@beanpublic queue queuemessage() {return new queue(topicrabbitconfig.message);}@beanpublic queue queuemessages() {return new queue(topicrabbitconfig.messages);}@beantopicexchange exchange() {return new topicexchange("exchange");}@beanbinding bindingexchangemessage(queue queuemessage, topicexchange exchange) {return bindingbuilder.bind(queuemessage).to(exchange).with("topic.message");}@beanbinding bindingexchangemessages(queue queuemessages, topicexchange exchange) {return bindingbuilder.bind(queuemessages).to(exchange).with("topic.#");}}
使用queuemessages同时匹配两个队列,queuemessage只匹配"topic.message"队列
public void send1() {string context = "hi, i am message 1";system.out.println("sender : " + context);this.rabbittemplate.convertandsend("exchange", "topic.message", context);}public void send2() {string context = "hi, i am messages 2";system.out.println("sender : " + context);this.rabbittemplate.convertandsend("exchange", "topic.messages", context);}
发送send1会匹配到topic.#和topic.message 两个receiver都可以收到消息,发送send2只有topic.#可以匹配所有只有receiver2监听到消息
2.fanout exchangefanout 就是我们熟悉的广播模式或者订阅模式,给fanout交换机发送消息,绑定了这个交换机的所有队列都收到这个消息。
fanout 相关配置:
@configurationpublic class fanoutrabbitconfig {@beanpublic queue amessage() {return new queue("fanout.a");}@beanpublic queue bmessage() {return new queue("fanout.b");}@beanpublic queue cmessage() {return new queue("fanout.c");}@beanfanoutexchange fanoutexchange() {return new fanoutexchange("fanoutexchange");}@beanbinding bindingexchangea(queue amessage,fanoutexchange fanoutexchange) {return bindingbuilder.bind(amessage).to(fanoutexchange);}@beanbinding bindingexchangeb(queue bmessage, fanoutexchange fanoutexchange) {return bindingbuilder.bind(bmessage).to(fanoutexchange);}@beanbinding bindingexchangec(queue cmessage, fanoutexchange fanoutexchange) {return bindingbuilder.bind(cmessage).to(fanoutexchange);}}
这里使用了a、b、c三个队列绑定到fanout交换机上面,发送端的routing_key写任何字符都会被忽略:
public void send() {string context = "hi, fanout msg ";system.out.println("sender : " + context);this.rabbittemplate.convertandsend("fanoutexchange","", context);}
结果如下:
sender : hi, fanout msg
...
fanout receiver b: hi, fanout msg
fanout receiver a : hi, fanout msg
fanout receiver c: hi, fanout msg
结果说明,绑定到fanout交换机上面的队列都收到了消息.
以上就是springboot如何集成rabbitmq的详细内容。
其它类似信息

推荐信息