RabbitMQ的使用与分析

来源:互联网 发布:log4j2 java代码加载 编辑:程序博客网 时间:2024/05/16 06:15

1、简介

对于系统间的解耦合,无非是采用MQ(消息系统)的方式,而在众多的MQ方案中,使用的比较广泛且性能较高的开源项目则是RabbitMQ了。

RabbitMQ是采用Erlang语言编写,天生的支持高并发,而且内部有不同其它MQ的独特设计。


2、安装

为了学习与使用rabbitMQ,我们首先需要在我们的机器上安装它的服务。这里我们以windows32系统为例,需要otp_win32_R16B02.exe和rabbitmq-server-3.2.1.exe两个安装文件,很容易就能下载到,官网上也有。这两个的安装是按常规下一步直到结束。安装完成后到rabbitMQ的根目录下的sbin目录下,执行

rabbitmq-plugins enable rabbitmq_management安装web插件,然后到计算机管理里找到rabbitmq的service,选择并启动,再执行下安装的sbin目录里的rabbitmqctl.bat文件,等服务都启动好后就可以进入web控制台了,在浏览器中输入http://127.0.0.1/55672,用户名和密码默认都是guest。如果进不去,那只

有两种可能,一是服务没启起来,二是插件没安装生效。多试上面几步就可以了。

                                               图1--控制台安装web插件

                                                 图2-web插件控制台


3、RabbitMQ的独特设计

RabbitMQ里不仅仅有Queue,还有其他一些概念,像Vhost,exchange。MQ的生产者直接连的是exchange,exchange就像一个路由器,通过一定的规则和Queue进行绑定,消费者直接连接的是Queue。ecchange与Queue有多种绑定规则,具体如下:

Fanout Exchange:

                                                   图3--扇出型路由

Topic Exchange:

                                                图4--主题式路由

DirectExchange:

                                           图5--直连型路由

HeadersExchange。

如果要像其他MQ一样用,只要选择directExchange就可以了,由于复杂的场景,我在项目中运用的是topicExchange,可以根据不同的routekey路由到不同的队列里,从而

能达到负载均衡的作用。

RabbitMQ对权限也做了很好的设计,具体的权限粒度如图所示:

                                                         图6--权限控制粒度

4、RabbitMQ的使用

为了使用的简单,先说明结合spring的使用方式,利用spring的IOC自动管理Bean。

需要引入以下的配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="            http://www.springframework.org/schema/beans                http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context.xsd            http://www.springframework.org/schema/rabbit                http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">                 <bean id="mqPropertyConfig"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="2" />        <property name="ignoreUnresolvablePlaceholders" value="true" /><property name="locations"><list><value>classpath:conf/mq/mq_$[envName].properties</value></list></property><property name="placeholderPrefix" value="${" /><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /></bean><!-- 连接服务配置 --> <rabbit:connection-factory id="connectionFactory" host="${apns.mq.Host}" username="${apns.mq.username}" password="${apns.mq.password}" virtual-host="${apns.mq.vhost}" port="${apns.mq.Port}" channel-cache-size="${apns.mq.cachesize}"/> <rabbit:admin id="amqpadmin" connection-factory="connectionFactory"/><rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/></beans>

在java代码中的引用示例如下:

<pre name="code" class="java">private AmqpTemplate amqpTemplate = context.getBean("amqpTemplate", AmqpTemplate.class);

amqpTemplate.convertAndSend(exchange, routeKey, message);
当然,exchange和Queue以及他们的绑定也可以直接用代码实现,具体例子如下:

        AmqpAdmin amqpAdmin = context.getBean(AmqpAdmin.class);        amqpAdmin.declareExchange(new TopicExchange(ENT_EXCHANGE, true, false));        amqpAdmin.declareExchange(new TopicExchange(DEV_EXCHANGE, true, false));        Map<Integer, String> map = RouteKeyUtil.getAllQueues();        for(String name : map.values()){            /**申明企业证书的队列并绑定至对应的exchange*/            amqpAdmin.declareQueue(new Queue(ENTERPRISE + name, true, false, false));            amqpAdmin.declareBinding(new Binding(ENTERPRISE + name, DestinationType.QUEUE, ENT_EXCHANGE, name, null));            /**申明开发者证书的队列并绑定至对应的exchange*/            amqpAdmin.declareQueue(new Queue(DEVELOPER + name, true, false, false));            amqpAdmin.declareBinding(new Binding(DEVELOPER + name, DestinationType.QUEUE, DEV_EXCHANGE, name, null));        }
我在项目中只引入了两个JAR文件:Maven的中央仓库里就能下载到(http://search.maven.org)

也可以直接用API而不需要spring。感兴趣的可以到官网上查看相关文档。

最后推荐一本RabbitMQ的相关书《RabbitMQ in Action》,不过这本书是英文的,网上也有电子版的。




0 0
原创粉丝点击