如何整合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. Day12 (黑客成长日记) 函数

    一.递归函数: 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. #计算阶乘: def fact(n): if n == 1: return 1 return n ...

  2. spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程

    RequestMappingHandlerMapping getMappingForMethod RequestMappingHandlerMapping 继承于 AbstractHandlerMet ...

  3. php中测试运行的时间,从而选择得出优化程序

    对于新手来说,优化代码的习惯十分重要, 测试运行的时间,从而得出最好的一个 <?php $t1=microtime(true);   //获取程序1,开始的时间 程序1(代码...) $t2=m ...

  4. 洛谷P1725--琪露诺(单调队列)

    https://www.luogu.org/problemnew/show/P1725 关于滑动窗口的解释https://www.cnblogs.com/albert67/p/10449039.htm ...

  5. Oracle 12c client with .NET legacy Oracle driver

    如果使用Oracle 12c Client和.NET的Oracle driver,你很可能会碰到跟下面一样的问题: https://www.codeproject.com/Questions/8767 ...

  6. You just run!

    第一篇博客,无关技术,有关身体. 写一篇跑步干货 装备篇 用过的鞋: 光脚,拖鞋,人字拖,回力板鞋,皮鞋,特步,鸿星尔克,李宁超轻13,ASICS  gt2000,阿迪低端. 1,非常推荐攒钱买一双a ...

  7. ecshop自动确认收货(无其他商家)

    1.创建文件 includes/modules/auto_order_confirm.php 代码:(思路:对已经发货和已经付款的订单检索,对比发货时间与当前时间的间隔,达到设定时间则自动收货) &l ...

  8. IntelliJ IDEA小问题通过操作软件解决

    Diamond types are not supported at this language level http://blog.csdn.net/qq_34884729/article/deta ...

  9. linux 性能优化

    linux的性能优化: 1.CPU,MEM 2.DISK--RAID 3.网络相关的外设,网卡 linux系统性能分析: top:linux系统的负载,CPU,MEM,SWAP,占用CPU和内存比较的 ...

  10. Tmux会话的使用

    不想看废话的直接拖到下面看干货部分! 我们管理Linux服务器通常是通过ssh远程连接过去,如果在服务器上执行比较耗时的操作,比如下载安装软件.编译等等,如果需要数个小时来完成这些工作,但是又不得不关 ...