如何整合RabbitMQ

1、添加spring-boot-starter-amqp

		<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2、添加配置

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.dynamic=true
spring.rabbitmq.cache.connection.mode=channel

3、注入队列

@Configuration
public class RabbitConfig {
@Bean
public Queue Queue() {
return new Queue("hello");
}
}

4、创建操作数据的Repository对象

interface CityRepository extends Repository<City, Long> {

	Page<City> findAll(Pageable pageable);

	Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
String country, Pageable pageable); City findByNameAndCountryAllIgnoringCase(String name, String country); }

5、创建消费者

@Component
public class RabbitConsumer {
@RabbitHandler
@RabbitListener(queues = "hello")
public void process(@Payload String foo) {
System.out.println(new Date() + ": " + foo);
}
}

6、启动主类

@SpringBootApplication
@EnableScheduling
public class AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(AmqpApplication.class, args);
}
}

控制台输出:

Sun Sep 30 16:30:35 CST 2018: hello

到此,一个简单的SpringBoot2.0集成RabbitMQ就完成了。

熟悉RabbitMQ的小伙伴们应该知道,RabbitMQ在一般的队列基础上,增加了ExChange的概念。ExChange有四种类型:Direct, Topic, Headers and Fanout。其中Headers实际很少使用,Direct较为简单。接下来将详细介绍如何使用topic和Fanout。

Topic Exchange

1、配置Topic规则

@Configuration
public class TopicRabbitConfig { @Bean
public Queue queueMessage1() {
return new Queue(MQConst.TOPIC_QUEUENAME1);
} @Bean
public Queue queueMessage2() {
return new Queue(MQConst.TOPIC_QUEUENAME2);
} @Bean
TopicExchange exchange() {
return new TopicExchange(MQConst.TOPIC_EXCHANGE);
} @Bean
Binding bindingExchangeMessage(Queue queueMessage1, TopicExchange exchange) {
// 将队列1绑定到名为topicKey.A的routingKey
return BindingBuilder.bind(queueMessage1).to(exchange).with(MQConst.TOPIC_KEY1);
} @Bean
Binding bindingExchangeMessages(Queue queueMessage2, TopicExchange exchange) {
// 将队列2绑定到所有topicKey.开头的routingKey
return BindingBuilder.bind(queueMessage2).to(exchange).with(MQConst.TOPIC_KEYS);
}
}

2、配置消费者

@Component
public class TopicConsumer { @RabbitListener(queues = MQConst.TOPIC_QUEUENAME1)
@RabbitHandler
public void process1(String message) {
System.out.println("queue:topic.message1,message:" + message);
} @RabbitListener(queues = MQConst.TOPIC_QUEUENAME2)
@RabbitHandler
public void process2(String message) {
System.out.println("queue:topic.message2,message:" + message);
}
}

3、生产消息

在Producer类中添加:

        // Topic
rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEYS, "from keys");
rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEY1, "from key1");

再次启动主类,控制台输出:

queue:topic.message2,message:from keys
queue:topic.message1,message:from key1
queue:topic.message2,message:from key1

Fanout Exchange

1、配置Fanout规则

@Configuration
public class FanoutRabbitConfig {
@Bean
public Queue MessageA() {
return new Queue(MQConst.FANOUT_QUEUENAME1);
} @Bean
public Queue MessageB() {
return new Queue(MQConst.FANOUT_QUEUENAME2);
} @Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(MQConst.FANOUT_EXCHANGE);
} @Bean
Binding bindingExchangeA(Queue MessageA, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(MessageA).to(fanoutExchange);
} @Bean
Binding bindingExchangeB(Queue MessageB, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(MessageB).to(fanoutExchange);
}
}

2.配置消费者

@Component
public class FanoutConsumer { @RabbitListener(queues = MQConst.FANOUT_QUEUENAME1)
@RabbitHandler
public void process1(String message) {
System.out.println("queue:fanout.message1,message:" + message);
} @RabbitListener(queues = MQConst.FANOUT_QUEUENAME2)
@RabbitHandler
public void process2(String message) {
System.out.println("queue:fanout.message2,message:" + message);
}
}

3、生产消息

在Producer类中添加:

        // FanOut
rabbitTemplate.convertAndSend(MQConst.FANOUT_EXCHANGE, "", "fanout");

再次启动主类,控制台输出:

queue:fanout.message2,message:fanout
queue:fanout.message1,message:fanout

源码地址:GitHub


本篇到此结束,如果读完觉得有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

SpringBoot2.0应用(三):SpringBoot2.0整合RabbitMQ的更多相关文章

  1. SpringBoot2.0源码分析(三):整合RabbitMQ分析

    SpringBoot具体整合rabbitMQ可参考:SpringBoot2.0应用(三):SpringBoot2.0整合RabbitMQ RabbitMQ自动注入 当项目中存在org.springfr ...

  2. rabbitMQ教程(三) spring整合rabbitMQ代码实例

    一.开启rabbitMQ服务,导入MQ jar包和gson jar包(MQ默认的是jackson,但是效率不如Gson,所以我们用gson) 二.发送端配置,在spring配置文件中配置 <?x ...

  3. SpringBoot2.0之整合RabbitMQ

    案例: Springboot 对RabbitMQ的支持 公共的pom: <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  4. SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合

    SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合 一.先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid ...

  5. springboot2.04与activiti 6.0集成

    本文就不对activiti做解释,下面直接看项目集成 以下顺序方面根据我的理解来,可以先从第二章看,再看第一张与第三章 增加activiti表的API,备注用. 目录 一.springboot2.X集 ...

  6. Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合例子(附完整的请假流程例子,jbpm基础,常见问题解决)

    Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子). 1.jbpm4.4 测试环境搭建 2.Jbpm4.4+hibernat ...

  7. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(三)

    前两章我为大家详细介绍了如何搭建Maven环境.Spring MVC的流程结构.Spring MVC与Struts2的区别以及示例中的一些配置文件的分析.在这一章,我就对示例的层次结构进行说明,以及M ...

  8. QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案

    QT5.4.0安装以及与VS2010整合安装---64bit操作系统解决方案 注意,目前QT官网不能下载,必须提供注册,然后才可以下载. 网上不同版本安装的细节有差异,特将我的安装相关操作贴出来,希望 ...

  9. Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子

    转自:http://www.blogjava.net/wangxinsh55/archive/2011/07/24/354925.html   Jbpm4.4+hibernate3.5.4+sprin ...

随机推荐

  1. 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验二 面向对象程序设计 实验报告

    20175316 盛茂淞 2018-2019-2 <Java程序设计>实验二 面向对象程序设计 实验报告 (一)单元测试 在 IDEA中我们把产品代码放在src目录中,把测试代码放在tes ...

  2. Js2WordCloud 词云用法

    1.引入 npm 安装: npm install js2wordcloud --save 通过script引入: <script src="dist/js2wordcloud.min. ...

  3. 一个jar包冲突引起的StackOverflowError

    项目运行中错误信息:java.lang.IllegalStateException: Unable to complete the scan for annotations for web appli ...

  4. Codeforces 1091D New Year and the Permutation Concatenation 找规律,数学 B

    Codeforces 1091D New Year and the Permutation Concatenation https://codeforces.com/contest/1091/prob ...

  5. mybatisGenerator代码生成器

    使用mybatisGenerator可以生成实体类,Mapper接口以及对应xml文件.本文介绍如何使用. 可以直接从本人github下载,只需按照如下步骤即可: 1.导入项目至idea中,项目结构如 ...

  6. ADO SQL手写分页

    //实现层 ---------------------------------------------------------分割线---------------------------------- ...

  7. JavaScriptDOM

    DOM简介 1.HTML DOM:网页被加载时,浏览器会创建文档对象模型 2.DOM操作HTML:改变HTML的元素.属性.CSS样式.对所有事件作出反应 DOM操作HTML 1.改变HTML输出流 ...

  8. vue computed计算属性和watch监听属性解疑答惑

    computed计算属性     计算属性类似于方法,用于输出data中定义的属性数据的结果,data数据变化时,计算属性的结果会同步变化,需要注意的是计算属性不可与data定义的属性同名. 相比于方 ...

  9. LeetCode刷题:第一题 两数之和

    从今天开始刷LeetCode 第一题:两数之和 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种 ...

  10. 安装Pangolin

    Pangolin是一个用于OpenGL显示/交互以及视频输出的一个轻量级 快速开发库 一:安装必要的库 1.Glew sudo apt-get install libglew-dev 2.Cmake ...