Java 异步处理 RabbitMQ

attachments-2020-03-VvHYQs7u5e7afa36afaea.jpg

不少开发人员说,将应用程序切换到异步处理很复杂。由于他们有一个自然须要同步通讯的Web应用程序。在这篇文章中,我想介绍一种方法来达到异步通讯的目的:使用一些众所周知的库和工具来设计他们的系统。 下面的例子是用Java编写的,但我相信它更多的是基本原理,同一个应用程序能够用任何语言来从新写。web

所需的工具和库:spring

  1. Spring Boot
  2. RabbitMQ

1.Web应用程序

一个用Spring MVC编写的Web应用程序并运行在Tomcat上。 它所作的只是将一个字符串发送到一个队列中 (异步通讯的开始) 并等待另外一个队列中的消息做为HTTP响应发送回来。服务器

首先,咱们须要定义几个依赖项,而后等待Spring Boot执行全部必要的自动配置。app

  1. <dependencies>dom

  2. <dependency>异步

  3. <groupId>org.springframework.boot</groupId>ide

  4. <artifactId>spring-boot-starter-web</artifactId>工具

  5. </dependency>this

  6. <dependency>spa

  7. <groupId>org.springframework.boot</groupId>

  8. <artifactId>spring-boot-starter-amqp</artifactId>

  9. </dependency>

  10. <dependency>

  11. <groupId>com.thedeanda</groupId>

  12. <artifactId>lorem</artifactId>

  13. </dependency>

  14. </dependencies>

  1.  

  2. @SpringBootApplication

  3. publicclassBlockingApplication{

  4. publicstaticvoid main(String[] args){

  5. SpringApplication.run(BlockingApplication.class, args);

  6. }

  7. @RestController

  8. publicstaticclassMessageController{

  9. privatefinalRabbitTemplate rabbitTemplate;

  10. publicMessageController(CachingConnectionFactory connectionFactory){

  11. this.rabbitTemplate =newRabbitTemplate(connectionFactory);

  12. }

  13. @GetMapping("invoke")

  14. publicString sendMessage(){

  15. Message response = rabbitTemplate.sendAndReceive("uppercase",null, request());

  16. returnnewString(response.getBody());

  17. }

  18. privatestaticMessage request(){

  19. Lorem LOREM =LoremIpsum.getInstance();

  20. String name = LOREM.getFirstName()+" "+ LOREM.getLastName();

  21. returnnewMessage(name.getBytes(),newMessageProperties());

  22. }

  23. }

  24. @Bean

  25. publicCachingConnectionFactory connectionFactory(){

  26. CachingConnectionFactory factory =newCachingConnectionFactory();

  27. factory.setAddresses("localhost:5672");

  28. factory.setUsername("admin");

  29. factory.setPassword("admin");

  30. return factory;

  31. }

  32. }

2.消费端应用程序

第二个应用程序仅仅是一个等待消息的RabbitMQ的消费端,将拿到的字符串转换为大写,而后将此结果发送到输出队列中。

  1. <dependencies>

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-amqp</artifactId>

  5. </dependency>

  6. </dependencies>

 

  1. @SpringBootApplication

  2. publicclassServiceApplication{

  3. publicstaticvoid main(String[] args){

  4. SpringApplication.run(ServiceApplication.class, args);

  5. }

  6. publicstaticclassMessageListener{

  7. publicString handleMessage(byte[] message){

  8. Random rand =newRandom();

  9. // Obtain a number between [0 - 49] + 50 = [50 - 99]

  10. int n = rand.nextInt(50)+50;

  11. String content =newString(message);

  12. try{

  13. Thread.sleep(n);

  14. }catch(InterruptedException e){

  15. e.printStackTrace();

  16. }

  17. return content.toUpperCase();

  18. }

  19. }

  20. @Bean

  21. publicCachingConnectionFactory connectionFactory(){

  22. CachingConnectionFactory factory =newCachingConnectionFactory();

  23. factory.setAddresses("localhost:5672");

  24. factory.setUsername("admin");

  25. factory.setPassword("admin");

  26. return factory;

  27. }

  28. @Bean

  29. publicSimpleMessageListenerContainer serviceListenerContainer(){

  30. SimpleMessageListenerContainer container =newSimpleMessageListenerContainer();

  31. container.setConnectionFactory(connectionFactory());

  32. container.setConcurrentConsumers(20);

  33. container.setMaxConcurrentConsumers(40);

  34. container.setQueueNames("uppercase_messages");

  35. container.setMessageListener(newMessageListenerAdapter(newMessageListener()));

  36. return container;

  37. }

  38. }

3.底层如何执行的?

程序启动并首次调用sendMessage()方法后,咱们能够看到Spring AMQP支持自动建立了一个新的回复队列并等待来自咱们的服务应用程序的响应。

  1. 2019-05-1217:23:21.451 INFO 4574---[nio-8080-exec-1].l.DirectReplyToMessageListenerContainer:Container initialized for queues:[amq.rabbitmq.reply-to]

  2. 2019-05-1217:23:21.457 INFO 4574---[nio-8080-exec-1].l.DirectReplyToMessageListenerContainer:SimpleConsumer[queue=amq.rabbitmq.reply-to, consumerTag=amq.ctag-VF-iqD9rLEuljIBstbCI1A identity=10e58093] started

若是咱们在消费端应用程序中查看消息,咱们能够看到Spring自动传播有关回复队列的信息以及相关ID,用于将其传递回Web应用程序以便可以将请求和响应配对在一块儿。

这就是发生魔术的地方。 固然,若是您想使其更复杂,您能够在协做中包含更多服务,而后将Web应用程序的最终响应放入与自动生成的队列不一样的队列中, 该队列只具备正确的关联ID。 另外,不要忘记设置合理的超时。

这个解决方案还有一个很大的缺点 - 应用程序吞吐量。 我故意这样作,以便我能够跟进这篇文章,进一步深刻调查 AsyncProfiler! 可是目前,咱们使用Tomcat做为主HTTP服务器,默认为200个线程,这意味着咱们的应用程序没法同时处理200多条消息,由于咱们的服务器线程正在等待RabbitMQ 回复队列的响应,直到有消息进入或发生超时。

 

attachments-2020-03-618oUL7h5e7afa4f5f672.jpg

相关文章
相关标签/搜索