温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot使用RabbitMQ教程

发布时间:2020-06-24 18:57:36 来源:网络 阅读:3349 作者:无心低语 栏目:开发技术

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;
/**

  • @Author 冯战魁
  • @Date 2018/1/12 下午2:50*/
    @Configuration
    br/>*/
    @Configuration
    br/>@Bean
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setAddresses("127.0.0.1:5672");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    connectionFactory.setVirtualHost("/");
    connectionFactory.setPublisherConfirms(true); //必须要设置
    return connectionFactory;}
    @Bean
    br/>}
    @Bean
    return new Queue("hello");
    }
    }

(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:21*/
    @RestController
    br/>*/
    @RestController
    br/>@Autowired
    br/>@RequestMapping("/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:06*/
    @Component
    br/>*/
    @Component
    public class RabbitReceiver1 {@RabbitHandler
    br/>@RabbitHandler
    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:06*/
    @Component
    br/>*/
    @Component
    public class RabbitReceiver2 {@RabbitHandler
    br/>@RabbitHandler
    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教程

至此整个任务完成!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI