springboot使用RabbitMQ教程

1、安装rabbitmq
docker安装,拉取镜像
docker pull rabbitmq:management
创建容器并启动
docker run -d --name rabbitmq --publish 5671:5671 --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672 rabbitmq:management
管理地址:
http://localhost:15672/ 用户名:guest 密码:guest

2、配置springboot
(1)pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

(2)添加rabbit配置文件,配置server信息
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**

(3)创建生产者,循环下发四个任务,用sleep模拟任务处理时间,一个.代码任务处理1s的时长,分别有四个不同时长的任务下发
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**

@Author 冯战魁 @Date 2018/1/12 上午11:21br/>*/
@RestController
public class RabbitSenderController {
br/>@Autowired
private AmqpTemplate rabbitTemplate;
br/>@RequestMapping("/sender")
public void sender(){
String[] tasks = {"1.","2..","3...","4...."};
for (int i=0;i<tasks.length;i++){
String content = tasks[i];
System.out.println("Sender : " + content);
this.rabbitTemplate.convertAndSend("hello",content);
}
}
}

(4)创建两个消费者Receiver1,Receiver2处理队列的任务

Receiver1:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**

@Author 冯战魁 @Date 2018/1/12 下午3:06br/>*/
@Component
@RabbitListener(queues = "hello")
public class RabbitReceiver1 {
br/>@RabbitHandler
public void process(String content){
System.out.println("Receiver1 : " + content);
try {
dowork(content);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void dowork(String content) throws InterruptedException{
for (char ch: content.toCharArray()) {
if (ch == '.'){
Thread.sleep(1000);
}
}
}
}

Receiver2:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**

@Author 冯战魁 @Date 2018/1/12 下午3:06br/>*/
@Component
@RabbitListener(queues = "hello")
public class RabbitReceiver2 {
br/>@RabbitHandler
public void process(String content){
System.out.println("Receiver2 : " + content);
try {
dowork(content);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void dowork(String content) throws InterruptedException{
for (char ch: content.toCharArray()) {
if (ch == '.'){
Thread.sleep(1000);
}
}
}
}
(5)验证代码
启动springboot,执行生产者的接口
http://localhost:8080/sender
然后查看终端输出的信息,如图所示

springboot使用RabbitMQ教程

可以看到最先完成的消费者去执行下一个任务,是按任务时长执行,并不是按任务个数平分,防止消费者无意义的等待,提高效率。

查看rabbit的管理端http://localhost:15672/
可以看到已经生成队列
springboot使用RabbitMQ教程

至此整个任务完成!










本文转自 无心低语 51CTO博客,原文链接:http://blog.51cto.com/fengzhankui/2060275,如需转载请自行联系原作者
上一篇:Linux中CA的创建,公钥私钥以及文件加密


下一篇:java 占位符 拼接字符串