I. Overview of Message Queuing
Message Queue Middleware is an important component in distributed system, which mainly solves the problems of application coupling, asynchronous message and traffic cutting front. Achieve high performance, high availability, scalability and eventual consistency architecture. is an indispensable middleware for large-scale distributed systems.
At present in the production environment, using more message queue has ACTIVEMQ,RABBITMQ,ZEROMQ,KAFKA,METAMQ,ROCKETMQ and so on.
Second, Message Queuing application scenario
The following is a description of the usage scenarios that Message Queuing commonly uses in real-world applications. Asynchronous processing, application decoupling, traffic cutting front and message communication four scenarios. This article uses Activemq+springboot to simulate these four scenarios.
2.1 Asynchronous processing
Scene Description: After the car triggered the fence alarm, need to send alarm messages and alarm messages. The traditional approach has two types of 1. Serial mode; 2. Parallel mode.
(1) Serial mode: After the alarm information is written to the database, send the alarm message, and then send the alarm message. After all three tasks have been completed, the alarm information is added to the statistical list.
(2) Parallel mode: When the alarm information is written to the database, the alarm message and SMS are sent at the same time.
Assuming that three business nodes each use 50 of a second, regardless of other overhead such as the network, the serial mode time is 150 milliseconds, the parallel time may be 100 milliseconds.
Since the number of requests processed by the CPU within the unit time is certain, assume that the throughput is 100 times in CPU1 seconds. The number of requests that the CPU can process in a serial mode of 1 seconds is 7 times (1000/150). The volume of requests processed in parallel is 10 times (1000/100).
Summary: As described in the above case, the traditional way of system performance (concurrency, throughput, response time) there will be bottlenecks. How to solve this problem?
The introduction of Message Queuing will not be necessary for the business logic to be processed asynchronously. After the transformation, the following structure:
code example
① introducing ACTIVEMQ dependencies in the Pom file
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-activemq</Artifactid> <version>1.5.6.RELEASE</version> </Dependency>
② Add ACTIVEMQ configuration to the configuration file
spring.activemq.broker-url=tcp://127.0.0.1:61616# the time to wait before considering the end #spring.activemq.close-timeout=15s # Whether the default proxy URL should be in memory. If an explicit proxy is specified, this value is ignored. Spring.activemq.in-memory=true # Whether to stop message passing before rolling back a rollback message. This means that the message order is not preserved when this command is enabled. spring.activemq.non-blocking-redelivery=false# Password spring.activemq.password=123456# The time to wait for a message to send a response. Set to 0 to wait forever. spring.activemq.send-timeout=0spring.activemq.user=haha# whether to trust all packages #spring.activemq.packages.trust-all=# A comma-separated list of specific packages to be trusted (when all packages are not trusted) #spring. activemq.packages.trusted=# when connection requests and China are blocked. Setting false throws a "JMSException exception". #spring. activemq.pool.block-if-full=true# If the pool is still full, the time is blocked before the exception is thrown. #spring. activemq.pool.block-if-full-timeout=-1ms# whether to create a connection at startup. Can be used to heat the pool at startup. #spring. activemq.pool.create-connection-on-startup=true# whether to use pooledconnectionfactory instead of ordinary connectionfactory. #spring. Activemq.pool.enabled=false # Connection Expiration timeout. #spring. activemq.pool.expiry-timeout=0ms# connection Idle timeout #spring.activemq.pool.idle-timeout=30s# connection pool maximum number of connections # spring.activemq.pool.max-connections=1# the maximum number of valid sessions per connection. #spring. Activemq.pool.maximum-active-session-per-connection=500# when there is a "jmsexception" attempt to reconnect #spring.activemq.pool.reconnect-on-exception=true# the time that runs between idle connection cleanup threads. When a negative number is not available, the eviction thread runs without an idle connection. #spring. activemq.pool.time-between-expiration-check=-1ms# whether to use only one messageproducer# Spring.activemq.pool.use-anonymous-producers=true
③ message Producer
ImportJava.util.Map;Importjavax.jms.Destination;ImportJavax.jms.Queue;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.jms.core.JmsMessagingTemplate;Importorg.springframework.jms.core.JmsTemplate;ImportOrg.springframework.jms.core.MessagePostProcessor;Importorg.springframework.scheduling.annotation.EnableScheduling;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component;/*** Alarm message producer *@authorKO **/@Component//@EnableScheduling Public classAlarmproducer {//you can also inject jmstemplate,jmsmessagingtemplate to encapsulate the jmstemplate.@AutowiredPrivatejmstemplate jmstemplate;//private Jmsmessagingtemplate jmstemplate; //@Autowired//private queue queue; //@Scheduled (fixeddelay=5000)//5s executes only one method without a parameter to use this annotation Public voidsendMessage (Destination Destination, String message) {//jmstemplate.convertandsend (destinationname, payload, messagepostprocessor); This. Jmstemplate.convertandsend (destination, message); }
Bidirectional queue
@JmsListener (destination= "Out.queue")
public void Consumermessage (String text) {
System.out.println ("The reply message received from the Out.queue queue is:" +text);
// }
}
④ Message Consumer
Importorg.apache.commons.lang.StringUtils;ImportOrg.springframework.jms.annotation.JmsListener;Importorg.springframework.stereotype.Component;/*** Fence Alarm consumer *@authorKO **/@Component Public classAlarmconsumer {//use Jmslistener to configure a queue for consumer listening, where text is the received message@JmsListener (Destination = "Mytest.queue")
@SendTo ("Out.queue") in order to implement a two-way queue Public voidreceivequeue (String text) {if(Stringutils.isnotblank (text)) {System.out.println ("The message received by Alarmconsumer is:" +text); System.out.println ("Send the alarm message [" +text+ "] to XXX"); System.out.println ("Send the alarm message [" +text+ "] sms to XXX"); System.out.println (""); } } }
⑤controller, write the Test interface.
@AutowiredPrivateAlarmproducer Alarmproducer; @RequestMapping (Value= "/chufabaojing", method=requestmethod.get) @ApiOperation (value= "Trigger alarm", notes= "trigger alarm") @ApiImplicitParams ({@ApiImplicitParam (name= "DeviceName", value = "Name", example = "xxxx", required =true, DataType = "string", paramtype= "query"), }) Publicstring chufabaojing (String devicename) {List<String> alarmstrlist =NewArraylist<>(); Alarmstrlist.add (DeviceName+ "Out fence01"); Alarmstrlist.add (DeviceName+ "Out Fence02"); Alarmstrlist.add (DeviceName+ "in Fence01"); Alarmstrlist.add (DeviceName+ "in Fence02"); System.out.println ("Equipment" +devicename+ "out of the fence alarm"); //Alarm information written to the databaseSYSTEM.OUT.PRINTLN ("Alarm data written to database ... "); //Write Message QueueDestination Destination =NewActivemqqueue ("Mytest.queue"); for(String alarmstr:alarmstrlist) {alarmproducer.sendmessage (destination, ALARMSTR); } //the message is written in the message queue.//The following two steps are moved to the ACTIVEMQ consumer.//Send mail//Send SMS return"Success"; }
2.2 Application Decoupling
Scene introduction, in the spring Cloud distributed micro-service project, the work order management and equipment management are two micro services, if a work order Zhang San orders, then the ticket status to be set to dispatch, inspectors set as Zhang San, equipment status to be placed in the inspection.
The traditional approach is to first adjust the work order management of the ticket update interface, after successful recall device Management Device Update interface, after successful return to the operation prompt to the user. The disadvantage of this is the application of coupling, if the dispatch operation when the device Management micro-service is hung or blocked, then the dispatch operation will fail or to wait for a long time without feedback. In addition, if the interface of the device management is changed, the code in the work order management should also be changed.
The introduction of message middleware, dispatch, work Order management Work order update interface after the processing of information written to the message queue, and then directly back to the operation feedback to the user. Regardless of the work order Management Service is not normal, the normal from the message queue to subscribe to message processing, not normal waiting for the reply to normal after subscribing to message processing.
2.3 Peak Flow Shaving
Introduction of the scene, XX company's system was originally developed for the customer a area, now in order to seize the market, won the B and C two regions of the customer, then the new system on-line, there is how to put B and C basic data into XX company's system to the problem, In a short period of time to transform the huge old data to fit the new system and import it, it is easy to hang up the system, in addition to the daily incremental data generation.
At this point, you can introduce the message middleware, B and C customers as long as the data rules in the message queue, XX company can methodically from the message queue to subscribe to data, can effectively alleviate the short period of high flow pressure, but this also on the reliability of the message middleware requirements.
If you encounter ACTIVEMQ bottlenecks, you can look at the ACTIVEMQ cluster scenario, this article http://blog.csdn.net/shuangzh115/article/details/50989182
2.4 Point-to-point communication
Similar to the features of chat rooms.
Introduction to the usage scenarios of message middleware ACTIVEMQ (combined with springboot example)