SpringBoot的RabbitMQ消息队列: 二、第一模式"Hello World!"

用消息中心的目的是解耦,消息(数据)在多个系统中传递,各个系统自主处理各自的事物。为此,根据RabbitMQ官方教程,我们逐渐开展研究。

首先,研究第一模式"Hello World!",我们将会设计两个程序,一个发送Hello world,另一个接收这个数据并且打印到屏幕。

整体的设计如下图:

一、建立工程

1、通过http://start.spring.io/分别建立HelloSending、HelloReceiving

2、下载、解压,依次导入eclipse

3、修改pom.xml,以便于热部署

4、增加日志文件logback.xml

5、修改application.properties,其中HelloSending为

[html]  view plain  copy
  1. #服务器配置  
  2. spring.application.name=rabbitmq-hello-sending  
  3. server.port=9080   
  4. #rabbitmq连接参数  
  5. spring.rabbitmq.host=localhost  
  6. spring.rabbitmq.port=5672  
  7. spring.rabbitmq.username=test  
  8. spring.rabbitmq.password=123456  
HelloReceiving为

[html]  view plain  copy
  1. #服务器配置  
  2. spring.application.name=rabbitmq-hello-receiving  
  3. server.port=9090   
  4. #rabbitmq连接参数  
  5. spring.rabbitmq.host=localhost  
  6. spring.rabbitmq.port=5672  
  7. spring.rabbitmq.username=test  
  8. spring.rabbitmq.password=123456  
二、编制sending

sending,实现消息发送到队列

1、配置文件。增加类HelloRabbitConfig,编制代码为

[html]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.springframework.amqp.core.Queue;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6.   
  7. @Configuration  
  8. public class HelloRabbitConfig {  
  9.   
  10.     @Bean  
  11.     public Queue helloQueue() {  
  12.         return new Queue("hello");  
  13.     }  
  14. }  
2、消息发送。增加类HelloSender,编制代码为
[html]  view plain  copy
  1. package com.example;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7. import org.springframework.amqp.core.AmqpTemplate;  
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.stereotype.Component;  
  10.   
  11. @Component  
  12. public class HelloSender {  
  13.   
  14.     protected static Logger logger=LoggerFactory.getLogger(HelloSender.class);   
  15.       
  16.     @Autowired  
  17.     private AmqpTemplate rabbitTemplate;  
  18.   
  19.     public String send(String name) {  
  20.         String context = "hello "+name+" --" + new Date();  
  21.         logger.debug("HelloSender: " + context);  
  22.         this.rabbitTemplate.convertAndSend("hello", context);  
  23.         return context;  
  24.     }  
  25. }  
3、RestController。增加类HelloController,编制代码为

[html]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. @RestController   
  11. public class HelloController {  
  12.     protected static Logger logger=LoggerFactory.getLogger(HelloController.class);   
  13.       
  14.     @Autowired  
  15.     private HelloSender helloSender;  
  16.       
  17.     @RequestMapping("/send/{name}")  
  18.     public String helloworld(@PathVariable String name) {  
  19.         return helloSender.send(name);  
  20.     }  
  21. }  
三、编制Receiving

1、配置文件。增加类HelloRabbitConfig,编制代码为

[html]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.springframework.amqp.core.Queue;  
  4. import org.springframework.context.annotation.Bean;  
  5. import org.springframework.context.annotation.Configuration;  
  6.   
  7. @Configuration  
  8. public class HelloRabbitConfig {  
  9.   
  10.     @Bean  
  11.     public Queue helloQueue() {  
  12.         return new Queue("hello");  
  13.     }  
  14. }  
2、接收消息,增加类HelloReceiver,编制代码为

[html]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.amqp.rabbit.annotation.RabbitHandler;  
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. @RabbitListener(queues = "hello")  
  11. public class HelloReceiver {  
  12.     protected static Logger logger = LoggerFactory.getLogger(HelloReceiver.class);  
  13.   
  14.     @RabbitHandler  
  15.     public void process(String hello) {  
  16.         logger.debug("HelloReceiver : " + hello);  
  17.     }  
  18. }  
四、运行工程

1、在工程HelloSending所在文件夹打开cmd,运行mvn spring-boot:run

2、在工程HelloReceiving所在文件夹打开cmd,运行mvn spring-boot:run

3、在浏览器中输入http://localhost:9080/send/上帝

     在两个工程的控制台,查看日志

五、小节

1、这两个工程,其实就是上节工程的重构。这两个工程唯一的区别就是一个包含发送类,一个包含接收类而已。

2、通过这两个工程,我们实现了基本的系统间通讯。

3、生产者、消费者,彼此之间不需要认识的,一个往垃圾堆中扔垃圾,一个从垃圾中检出垃圾。

下一节,我们研究一个人扔垃圾多个人检垃圾的问题。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值