The practice and thinking of Java little Kee-RABBITMQ

Source: Internet
Author: User
Tags rabbitmq

Objective

This essay will summarize some of my knowledge of Message Queuing RabbitMQ, and discuss its application in high concurrency and second-kill systems.

1. Sample Preparation

Think about it, or throw a simple example, and then expand it according to its specific scenario, and I think it's clearer.

Rabbitconfig:

@Configurationpublicclass RabbitConfig {    @Bean    publiccallQueue() {        returnnew Queue(MQConstant.CALL);    }}

Client:

@Componentpublicclass Client {    @Autowired    private RabbitTemplate rabbitTemplate;    publicvoidsendCall(String content) {        for (int010000; i++) {            "-" + content;            System.out.println(String.format("Sender: %s", message));            rabbitTemplate.convertAndSend(MQConstant.CALL, message);        }    }}

Server:

@Componentpublicclass Server {    @RabbitHandler    @RabbitListener(queues = MQConstant.CALL)    publicvoidcallProcessthrows InterruptedException {        Thread.sleep(100);        System.out.println(String.format("Receiver: reply(\"%s\") Yes, I just saw your message!", message));    }}

Result:

Sender: Hello, are you there!Receiver: reply("Hello, are you there!") Yes, I just saw your message!

The example above creates a queue call in RABBITMQ in which messages wait for consumption:

The simple extension on this basis I will no longer write cases, such as the domain module after the completion of its core business rules may need to update the cache, write a message, remember a complex log, make a statistical report, and so on, these do not need timely feedback or time-consuming ancillary services can be distributed through an asynchronous queue, As a way to improve the responsiveness of the core business, such processing can make the boundaries of the domain clearer, the maintainability of code and the ability to continue to expand.

2. Peak Shaving

The scenario I mentioned in the last example is decoupling and notification, then extending, because it has good buffering properties, so there is a very suitable application scenario that is cutting the peak. For the sudden high concurrent requests, we can quickly join the queue and reply to the user A friendly prompt, and then the server can withstand the scope of processing slowly, in order to prevent the sudden CPU and memory "burst table."

After the transformation for the sender of course is relatively cool, he just put the request to join the message queue, the processing pressure to the consumer side. Then think, is there any side effect of this treatment? If the request is just a thread blocking, then join the queue slowly queued processing, it is not finished, the user to se years to get feedback? So for that, I think it's important to change the consumer's approach to asynchronous calls (that is, multithreading) to improve throughput, and the notation in Spring Boot is simple:

@Componentpublicclass Server {    @Async    @RabbitHandler    @RabbitListener(queues = MQConstant.CALL)    publicvoidcallProcessthrows InterruptedException {        Thread.sleep(100);        System.out.println(String.format("Receiver: reply(\"%s\") Yes, I just saw your message!", message));    }}

Referring to the method of example one, I have released 10,000 messages to join the queue, and the consumption end of the call each time blocking one second, it is interesting, when can finish? But if hundreds of threads are processed at the same time, those 10 seconds are enough, of course, the exact number of appropriate should also be based on the specific business scenarios and server configuration as appropriate considerations. Also, don't forget the thread pool:

@Configuration Public classAsyncconfig {@Bean     PublicExecutorAsyncexecutor() {Threadpooltaskexecutor executor =New Threadpooltaskexecutor(); Executor.setcorepoolsize(Ten); Executor.setmaxpoolsize( -); Executor.setqueuecapacity(Ten); Executor.Setthreadnameprefix("myexecutor-"); Executor.Setrejectedexecutionhandler(NewThreadpoolexecutor.Callerrunspolicy()); Executor.Initialize();returnExecutor }}
3. Exchange

RabbitMQ may provide services for N applications at the same time, if you and your Blue Yan bosom friend suddenly heart, in different business use the same routingkey, think on the stimulus. Therefore, the queue is more natural to group management, to define the rules of Exchange, then you can play alone.

Mqconstant:

publicclass MQConstant {    publicstaticfinal"YOUCLK-MESSAGE-EXCHANGE";    publicstaticfinal String CALL = MQConstant.EXCHANGE".CALL";    publicstaticfinal String ALL = MQConstant.EXCHANGE".#";}

Rabbitconfig:

 @Configuration  public  class  rabbitconfig { @Bean  public  Queue callqueue  () {return  new  Queue (mqconstant.); }  @Bean  topicexchange exchange  () {return new  topicexchange  (Mqconstant.); }  @Bean  Binding bindingexchangemessage  (Queue queuemessage, Topicexc Hange Exchange) {return  bindingbuilder. (queuemessage). to  (Exchange). with  (Mqconstant. all ); }}

At this point we'll check the queue call and see that Exchange is already bound:

Of course, the role of Exchange is much more than that, the above example is Topic mode, in addition to the Direct, Headers and fanout mode, the same way, the interested children's shoes can go to see "official documents" for more in-depth understanding.

4. Delay queue

The time-lapse scenario believes that the small partners have been exposed, especially when snapping, the unpaid orders are recycled within the specified timeframe. The payment API also has a payment after the completion of the delay to confirm the message push, the implementation principle should be similar.

Using RabbitMQ to realize this function first to understand his two features, respectively, are time-to-live Extensions and Dead letter exchanges, literally can understand a general, one is the time to live, one is Badmail. The whole process is also easy to understand, the TTL is equivalent to a buffer queue, waiting for it to expire after the message is forwarded by DLX to the actual consumption queue, so that his delay process.

Mqconstant:

publicclass MQConstant {    publicstaticfinal"PER_DELAY_EXCHANGE";    publicstaticfinal"DELAY_EXCHANGE";    publicstaticfinal"DELAY_CALL_TTL";    publicstaticfinal"CALL";}

Expirationmessagepostprocessor:

publicclassimplements MessagePostProcessor {    privatefinal Long ttl;    publicExpirationMessagePostProcessor(Long ttl) {        this.ttl = ttl;    }    @Override    publicpostProcessMessagethrows AmqpException {        message.getMessageProperties()                .setExpiration(ttl.toString());        return message;    }}

Client:

@Component Public classClient {@Autowired    PrivateRabbittemplate rabbittemplate; Public void SendCall(String content) { for(inti =1; I <=3; i++) {LongExpiration = i * the; String message = i +"-"+ content; System. out.println(String. Format("Sender:%s", message)); Rabbittemplate.Convertandsend(Mqconstant.Delay_call_ttl, (Object) message,New Expirationmessagepostprocessor(expiration)); }    }}

Server:

@Componentpublic class Server { @Async @RabbitHandler @RabbitListener(queues = MQConstant.CALL) public void callProcess(String message) throws InterruptedException { String date = (new SimpleDateFormat("HH:mm:ss")).format(new Date()); System.out.println(String.format("Receiver: reply(\"%s\") Yes, I just saw your message!- %s", message, date)); }}

Result:

Sender: 1-Hello, are you there!Sender: 2-Hello, are you there!Sender: 3-Hello, are you there!Receiver: reply("1-Hello, are you there!") Yes, I just saw your message!- 23:04:12Receiver: reply("2-Hello, are you there!") Yes, I just saw your message!- 23:04:17Receiver: reply("3-Hello, are you there!") Yes, I just saw your message!- 23:04:22

Results at a glance, respectively, in the queue delayed 5 seconds, 10 seconds, 15 seconds, of course, these are just my simple example, children's shoes can read the official documents ("TTL" && "DLX") further in-depth study.

Conclusion

This essay should not be so end, but the mood is not good at night, mixed feelings, can not continue to write, helpless. Recently is looking for new job opportunities, my: YOUCLK, whether there is no recommendation, give me a little encouragement, thank you!

The practice and thinking of Java little Kee-RABBITMQ

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.