1. Introduction
The solution coupling between systems is simply an MQ (message system) approach, while in many MQ scenarios, the more extensive and higher-performance open source projects are RABBITMQ.
RABBITMQ is written in Erlang language and is inherently supported for high concurrency, and has a unique design with different MQ features.
2, installation
In order to learn and use RABBITMQ, we first need to install its services on our machines. Here we take the Windows 32 system for example, the need for Otp_win32_r16b02.exe and rabbitmq-server-3.2.1.exe two installation files, it is easy to download to, the official online also. These two installations are routinely next until the end. After the installation is completed, go to the Sbin directory under the root directory of the RABBITMQ and execute
Rabbitmq-plugins Enable rabbitmq_management Install the Web plug-in, and then go to the Computer Management to find the RABBITMQ service, select and start, After the installation of the Sbin directory of Rabbitmqctl.bat files, and other services are started to enter the Web console, in the browser to enter the http://127.0.0.1/55672, username and password by default are guest. If you can't get in, that one
There are two possibilities, one is the service does not start, and the other is the plug-in does not have the installation effect. Try a few more steps on it.
Figure 1--Console Install Web plugin
Figure 2-web Plug-in Console
3, the unique design of RABBITMQ
RABBITMQ not only have queue, but also some other concepts, like Vhost,exchange. MQ producers are directly connected to exchange,exchange like a router, bound by a certain number of rules and queue, and the consumer connects directly to the queue. ECChange and queue have several binding rules, as follows:
Fanout Exchange:
Figure 3--Fan-out routing
Topic Exchange:
Figure 4--Theme-style routing
Directexchange:
Figure 5--Direct-attached routing
Headersexchange.
If you want to use the same as other MQ, as long as the choice of directexchange can be, because of complex scenes, I used in the project is Topicexchange, can be based on different Routekey routing to different queues, thus
can achieve the function of load balance.
RABBITMQ has also done a good job of the design, the specific permissions granularity as shown in the figure:
Figure 6--Permissions Control granularity
4, the use of RABBITMQ
For the sake of simplicity, let's use spring's IOC to automate the management of beans, first by describing how you can combine spring.
The following configuration needs to be introduced:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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.O Rg/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.spring
Framework.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.PropertyPla Ceholderconfigurer "> <property name=" Order "value=" 2 "/> <property name=" Ignoreunresolvableplacehold ERs "value=" true "/> <property naMe= "Locations" > <list> <value>classpath:conf/mq/mq_$[envname].properties </value> </ list> </property> <property name= "Placeholderprefix" value= "${"/> <property "name=" Smodename "value=" system_properties_mode_override/> </bean> <!--connection service configuration--> <rabbit:connection-f Actory 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>
Examples of references in Java code are as follows:
<pre name= "code" class= "java" >private amqptemplate amqptemplate = Context.getbean ("Amqptemplate", Amqptemplate.class);
Amqptemplate.convertandsend (Exchange, Routekey, message);
Of course, exchange and queue and their bindings can also be implemented directly in code, as follows:
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 ()) {
/** declares the queue of the enterprise certificate and binds to the corresponding exchange*/
amqpadmin.declarequeue (The new queue (ENTERPRISE + Name, True, False, false);
Amqpadmin.declarebinding (New Binding (ENTERPRISE + name, Destinationtype.queue, ent_exchange, name, null));
/** affirms the queue of the developer certificate and binds to the corresponding exchange*/
amqpadmin.declarequeue (new queue (DEVELOPER + name, True, False, false);
Amqpadmin.declarebinding (New Binding (DEVELOPER + name, Destinationtype.queue, dev_exchange, name, null));
I've only introduced two jar files into the project: MAVEN's central repository can be downloaded (http://search.maven.org)
You can also use the API directly without the need for spring. Interested in the official website can view the relevant documents.
Finally, I recommend a RABBITMQ related book "Rabbitmq in Action", but this book is in English, online also has electronic version.