tnblog
首页
视频
资源
登录

springboot整合rabbitmq

7848人阅读 2019/9/5 11:34 总访问:18211 评论:3 收藏:0 手机
分类: 消息队列

Windows下安装RabbitMQ:http://www.tnblog.net/aojiancc2/article/details/232

消息队列rabbitmq介绍:http://www.tnblog.net/aojiancc2/article/details/2332

(以上引用自大佬文章)


打开idea,创建springboot项目


依赖如下:

  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-amqp</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.springframework.boot</groupId>
  7.     <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10.     <groupId>org.springframework.boot</groupId>
  11.     <artifactId>spring-boot-starter-tomcat</artifactId>
  12.     <scope>provided</scope>
  13. </dependency>
  14. <dependency>
  15.     <groupId>org.springframework.boot</groupId>
  16.     <artifactId>spring-boot-starter-test</artifactId>
  17.     <scope>test</scope>
  18. </dependency>

yml文件


创建conf目录,创建交换机的配置类ExchangeConfig

  1. package com.cy.demo.rabbitmq.conf;
  2. import org.springframework.amqp.core.DirectExchange;
  3. import org.springframework.amqp.core.FanoutExchange;
  4. import org.springframework.amqp.core.TopicExchange;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class ExchangeConfig {
  9.    /**
  10.      *   1.定义direct exchange,绑定queueTest
  11.      *   2.durable="true" rabbitmq重启的时候不需要创建新的交换机
  12.      *   3.direct交换器相对来说比较简单,匹配规则为:如果路由键匹配,消息就被投送到相关的队列
  13.      *     fanout交换器中没有路由键的概念,他会把消息发送到所有绑定在此交换器上面的队列中。
  14.      *     topic交换器你采用模糊匹配路由键的原则进行转发消息到队列中
  15.      *   key: queue在该direct-exchange中的key值,当消息发送给direct-exchange中指定key为设置值时,
  16.      *   消息将会转发给queue参数指定的消息队列
  17.      */
  18.    //direct交换器
  19.     @Bean
  20.     public DirectExchange directExchange(){
  21.         DirectExchange directExchange = new DirectExchange(RabbitMqConfig.EXCHANGE,true,false);
  22.         return directExchange;
  23.     }
  24.     /*//fanout交换器
  25.     @Bean
  26.     public FanoutExchange fanoutExchange(){
  27.         FanoutExchange fanoutExchange = new FanoutExchange(RabbitMqConfig.EXCHANGE,true,false);
  28.         return fanoutExchnge;
  29.     }*/
  30.    /* //topic交换器
  31.     @Bean
  32.     public TopicExchange topicExchange(){
  33.         TopicExchange topicExchange = new TopicExchange(RabbitMqConfig.EXCHANGE,true,false);
  34.         return topicExchange;
  35.     }*/
  36. }



创建QueueConfig配置类,配置队列

  1. package com.cy.demo.rabbitmq.conf;
  2. import org.springframework.amqp.core.Queue;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class QueueConfig {
  7.    
  8.    @Bean
  9.     public Queue firstQueue() {
  10.         /**
  11.          durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
  12.          auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
  13.          exclusive  表示该消息队列是否只在当前connection生效,默认是false
  14.          */
  15.         return new Queue("first-queue",true,false,false);
  16.     }
  17.  
  18.     @Bean
  19.     public Queue secondQueue() {
  20.         return new Queue("second-queue",true,false,false);
  21.     }
  22.     
  23.     
  24.     @Bean
  25.     public Queue thridQueue() {
  26.         return new Queue("thrid-queue",true,false,false);
  27.     }
  28. }




再创建RabbitMqConfig将指定队列和所要使用的交换机通过路由key来绑定

  1. package com.cy.demo.rabbitmq.conf;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class RabbitMqConfig {
  9.    
  10.     /** 消息交换机的名字*/
  11.     public static final String EXCHANGE = "exchange01";
  12.     /** 队列key1*/
  13.     public static final String ROUTINGKEY1 = "queue_one_key1";
  14.     /** 队列key2*/
  15.     public static final String ROUTINGKEY2 = "queue_one_key2";
  16.     /** 队列key3*/
  17.     public static final String ROUTINGKEY3 = "queue_one_key3";
  18.  
  19.     @Autowired
  20.     private QueueConfig queueConfig;
  21.     @Autowired
  22.     private ExchangeConfig exchangeConfig;
  23.  
  24.     /**
  25.      将消息队列1和交换机进行绑定
  26.      */
  27.     @Bean
  28.     public Binding binding_one() {
  29.         return BindingBuilder.bind(queueConfig.firstQueue()).to(exchangeConfig.directExchange()).with(RabbitMqConfig.ROUTINGKEY1);
  30.     }
  31.  
  32.     /**
  33.      * 将消息队列2和交换机进行绑定
  34.      */
  35.     @Bean
  36.     public Binding binding_two() {
  37.         return BindingBuilder.bind(queueConfig.secondQueue()).to(exchangeConfig.directExchange()).with(RabbitMqConfig.ROUTINGKEY2);
  38.     }
  39.  
  40.     /**
  41.      * 将消息队列3和交换机进行绑定
  42.      */
  43.     @Bean
  44.     public Binding binding_three() {
  45.         return BindingBuilder.bind(queueConfig.thridQueue()).to(exchangeConfig.directExchange()).with(RabbitMqConfig.ROUTINGKEY3);
  46.     }
  47.     
  48. }


完成配置类的创建之后,就可以通过api对队列进行操作了



创建TestController简单的对队列进行操作

  1. package com.cy.demo.rabbitmq.controller;
  2. import org.springframework.amqp.core.AmqpTemplate;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class TestController {
  9.     //注入AmqpTemplate
  10.     @Autowired
  11.     private AmqpTemplate amqpTemplate;
  12.     @RequestMapping(value = "/q1send",method = RequestMethod.GET)
  13.     public void q1send(String str){
  14.         //将消息送入之前设置的first-queue,第二个参数为送入队列的内容,
  15.         // 可以是对象也可以是其他类型
  16.         amqpTemplate.convertAndSend("first-queue",str);
  17.     }
  18.     //取出对应队列里的消息
  19.     @RequestMapping(value = "/receive",method = RequestMethod.GET)
  20.     public void receive(String queueName){
  21.         Object object =amqpTemplate.receiveAndConvert(queueName);
  22.         System.out.println(object.toString());
  23.     }
  24. }


通过启动类启动

访问localhost:8080/q1send?str=向征你好,

就可以送入队列first-queue里面了

访问http://localhost:15672,可以看到队列里的消息已经有一条了



再访问localhost:8080/receive?queueName=first-queue

把队列的名字传进去

就可以取出之前送入到队列里的消息了

这里只使用到一个队列,只做一个简单的例子



评价

5201314

2019/9/5 12:49:04

写的好,写得好

饰心

2019/9/5 12:54:11

写的好,写得好

剑轩

2019/9/5 16:06:34

向征你好[嘻嘻][嘻嘻]

Mybatis的SQL构建7 小小扩展+springboot

pom.xml的依赖,我是直接在spring.io官方下的,这样避免依赖冲突&lt;!--jdbc--&gt; &lt;dependency&gt; &lt;groupId&gt;o...

读取springboot的配置类容

读取有三种方式案例@Value案例 Environment案例@ConfigurationPropertiesa:b:默认对象时(容易提取错误信息,容易与你的...

.net core Ocelot 简单网关集群熔断架构整合目录

目录( 一 ) .netCore3.0 Ocelot 制作简单负载均衡 ( 二 ) .netCore3.1 consul服务集群 ( 三 ) .netCore3.1 Ocelot 与 Con...

Linux 性能调优(平衡负载整合

Linux 性能调优(平衡负载整合)[TOC] uptime命令的意义 通常我们通过 uptime 来了解系统负载。 名称 含义 ...

IDEA与tomcat整合

配置方式11、2、3、一直点ok就可以了创建项目配置方式22.12.22.32.42.52.6

把多个api swagger整合到一个swagger里面。api 整合

原理就是使用api网关就行了,比如ocelot。 把api都统一到一个项目里边就好办了

整合第三方技术-注解整合mybatis分析 20

一、将综合案例 12修改成注解的形式1、配置文件的修改分析注解整合MyBatis分析业务类使用注解形式声明bean,属性采用注解注...

整合Junit(测试工具) 21

一、1、介绍2、坐标案例、1、2、创建测试类3、测试结果

SSM整合-SSM整合流程简介项目结构搭建 01

一、二、创建项目如果想创建单独的项目:搜索使用Idea和Maven搭建Spring MVC1、2、3、4、5、点击finish后,点击Apply再点击...

SSM整合-Spring整合MyBatis 02

续写于SSM整合-SSM整合流程简介&amp;项目结构搭建 01一、需求目录二、1、pom.xml1.1、先删除某些自动生成的东西,直到变成...

SSM整合-配置分页插件与事务 03

续写于SSM整合-Spring整合MyBatis 02一、1、配分页插件2、配事务jdbc类型的事务2.1、开启事务驱动2.2、注入jdbc类型的事务...

SSM整合-Rest风格开发SpringMVC 05

一、二、环境1、配置1.1、新建spring-mvc.xml&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;UTF-8&quot;?&gt; &lt;bea...

SSM整合-Spring整合SpringMVC 06

1、在web.xml配置spring&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;UTF-8&quot;?&gt; &lt;web-appxmlns=&quot;http...

SSM整合-表现层数据封装 08

一、案例、1、创建封装对象2、创建编码规则对象3、表现出的用法
血腥运动,百发百中
排名
71
文章
2
粉丝
1
评论
4
springboot整合rabbitmq
剑轩 : 向征你好
springboot整合rabbitmq
饰心 : 写的好,写得好
springboot整合rabbitmq
5201314 : 写的好,写得好
flowable流程设计器
剑轩 : 工作流?
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术