à悟空学院:https://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/A6ZagYTi
Spring Cloud视频:http://t.cn/A6ZagxSR
SpringBoot Shiro视频:http://t.cn/A6Zag7IV
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/A6Zad1OH
SpringSecurity5.0视频:http://t.cn/A6ZadMBe
Sharding-JDBC分库分表实战:http://t.cn/A6ZarrqS
分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr
这一节我们介绍下Spring Boot整合RabbitMQ,对于RabbitMQ这里不过多的介绍,大家可以参考网络上的资源进行安装配置,本节重点是告诉大家如何在Spring Boot中使用RabbitMQ,那么本节分如下几个步骤:
(1) 新建Maven Java Project;
(2) 在pom.xml添加相关依赖;
(3) 编程+测试
(4) 配置信息
接下来看看每个步骤是怎么操作的。
(1) 新建Maven Java Project;
新建一个Maven Java Project项目,取名为spring-boot-rabbitmq
(2) 在pom.xml添加相关依赖;
这里需要加入基本的依赖以及rabbitmq相关的依赖,具体如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kfit</groupId>
<artifactId>spring-boot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-rabbitmq</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk版本号,Angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
<java.version>1.8</java.version>
</properties>
<!--
spring boot 父节点依赖,
引入这个之后相关的引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot web支持:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
(3) 编程+测试
在这里我们为了方便直接在启动类App.java进行编码,先提供代码:
package com.kfit;
import java.util.Date;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年8月23日
*/
@SpringBootApplication
@EnableScheduling//启用任务调度.
@RabbitListener(queues="foo")//启用Rabbit队列监听foo key.
public class App {
//rabbit操作类;
@Autowired
private RabbitTemplate rabbitTemplate;
@Scheduled(fixedDelay=3000)//3s执行1次此方法;
public void send(){
rabbitTemplate.convertAndSend("foo","zhang");
}
@Bean
public Queue fooQueue(){
returnnew Queue("foo");
}
//接收到消息处理.
@RabbitHandler
public void onMessage(@Payload String foo){
System.out.println(" >>> "+new Date() + ": " + foo);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
好了,到这里就可以运行测试了,观察控制台的打印信息:
>>> Tue Aug 23 15:06:54 CST 2016: zhang
>>> Tue Aug 23 15:06:57 CST 2016: zhang
>>> Tue Aug 23 15:07:00 CST 2016: zhang
看到如上信息,恭喜你简单的例子编写成功了。好了,上面的源代码还是有必要进行简单的讲解下。
首先我们需要清除RabbitMQ的话有这么几个角色,消息生产者或者说消息提供者(sender);其次就是消息队列提供存放消息的对方(queue);消息消费者或者说消息接收者(receiver)。知道这些概念之后在看代码就好理解很多了。
我们先看消息提供者:send()方法,这里使用Spring 提供的RabbitTemplate 进行操作消息,调用convertAndSend方法将消息发布到对应的消息频道上,这里也就是使用Queue进行存储;然后我们注意到这个方法不是我们人为执行的方法而是采用定时执行的方式进行3s一次进行发布消息,所以你会看到send方法上有一个注解@Scheduled,那么使用该注解的话,相应的类上上面就需要@EnableScheduling启用注解。
在上面的分析中我们说消息被发布到队列中,所以我们需要创建一个Queue进行存放,这里需要注意Queue的包路径是org.springframework.amqp.core.Queue。
这里我们注入了fooQueue 方法指定队列的名称是foo,那么此队列只会处理key为foo的消息,其它的key不会处理。
最后就是消息的接收者了:这里我们使用了@RabbitHandler定义了一个消息接收者onMessage,其中这个方法名可以随意取名,再者我们需要启用Rabbit监听,所以需要在类上添加@RabbitListener,这样编写完就能按照我们的想法执行了。那么如果我们编写了两个消息接收者的话,可以正常运行嘛,两个接收者都能接收到消息嘛,答案是肯定的,因为我们以上的编码是发布(Pub)/订阅(Sub)消息模型。
很有意思的是,这个方法还可以在简化下,将类上的@RabbitListener直接转移到方法上,去掉@RabbitHandler注解,直接为如下:
//接收到消息处理.
@RabbitListener(queues = "foo")
public void onMessage(@Payload String foo){
System.out.println(" >new>> "+new Date() + ": " + foo);
}
视频&交流平台:
http://study.163.com/course/introduction.htm?courseId=1004329008
http://412887952-qq-com.iteye.com/blog/2321532
(4) 配置信息
在以上的代码你会发现我们根本没有添加什么配置信息,所以spring boot提供了默认的配置,那么我们应该怎么修改配置呢,只需要在application.properties进行配置即可:
# RABBIT (RabbitProperties)
spring.rabbitmq.host= # connection host
spring.rabbitmq.port=# connection port
spring.rabbitmq.addresses= # connection addresses (e.g. myhost:9999,otherhost:1111)
spring.rabbitmq.username= # login user
spring.rabbitmq.password= # login password
spring.rabbitmq.virtualhost=
spring.rabbitmq.dynamic=
这里可以配置主机地址,端口号,用户名密码(默认使用guest/guest)。
相关推荐
Spring Boot 整合 RabbitMQ 案例.zipSpring Boot 整合 RabbitMQ 案例.zipSpring Boot 整合 RabbitMQ 案例.zipSpring Boot 整合 RabbitMQ 案例.zipSpring Boot 整合 RabbitMQ 案例.zipSpring Boot 整合 RabbitMQ 案例...
介绍Spring Cloud Stream与RabbitMQ集成的代码示例。Spring Cloud Stream是一个建立在Spring Boot和Spring Integration之上的框架,有助于创建事件驱动或消息驱动的微服务。
简单介绍了如何在Spring Cloud中使用RabbitMQ和Kafka来完成消息发送与接收
Spring Boot 集成各种框架 使用案例(spring-boot-rabbitmq、spring-boot-mail、spring-boot-thymeleaf、spring-boot-shiro)
NULL 博文链接:https://hbxflihua.iteye.com/blog/2407915
主要给大家介绍了关于spring boot集成rabbitmq的相关资料,springboot集成RabbitMQ非常简单,文中通过示例代码介绍的非常详细,需要的朋友们可以参考借鉴,下面随着小编来一起学习学习吧。
Spring Boot + RabbitMQ发生邮件(保证消息 100% 投递成功并被消费
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-boot-...
springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持。下面通过本文给大家介绍下spring boot整合RabbitMQ(Direct模式),需要的朋友可以...
spring boot和rabbitMQ最佳实践 1、JSON序列化与反序列化 要实现消息跨平台,需要配置消息JSON序列化(配置见代码实现),就可以实现不同语言之间互相发送/接收消息,还可以直接用RabbitMQ控制台发送消息。 2、...
mysql需开启binlog 查看是否开启binlog ...3.3 rabbitmq配置 在virtualHost:/ 下新增Exchanges: canal.exchange 新增队列:test.queue, 绑定canal.queue, RoutingKey:canal.routing.key canal下载及配置 ...
rabbitmq spring rabbitmq spring rabbitmq spring rabbitmq spring http://knight-black-bob.iteye.com/blog/2304089
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-...
本篇文章主要介绍了Spring boot集成RabbitMQ的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://dwj147258.iteye.com/blog/2430847
RabbitMQ 是一个开源的消息代理中间件,广泛用于构建分布式应用程序中的消息系统。在 Spring Boot 项目中,通过集成 RabbitMQ,可以实现异步消息传递、消息队列等功能,提高系统的可靠性和扩展性。
26.Spring Cloud Bus之RabbitMQ初窥 27.Spring Cloud Bus整合RabbitMQ 28.Spring Cloud Bus整合Kafka 29.Spring Cloud Stream初窥 30.Spring Cloud Stream使用细节 31.Spring Cloud系列勘误
rabbitmq-server-3.9.12
RabbitMQ作为AMQP的代表性产品,在项目中大量使用。结合现在主流的spring boot,极大简化了开发过程中所涉及到的消息通信问题。