springboot入门系列(三):SpringBoot教程之RabbitMQ示例
SpringBoot教程之RabbitMQ示例
SpringBoot框架已经提供了RabbitMQ的使用jar包,开发人员在使用RabbitMQ的时候只需要引用jar包简单的配置一下就可以使用RabbitMQ,这极大的简化了开发人员的开发成本,提升开发效率。
话不多说,直接上代码:
先在pom.xml
文件添加依赖spring-boot-starter-amqp
如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-boot-rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<description>springboot整合RabbitMQ的示例</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在application.properties
文件中配置:
server.port=8085
spring.rabbitmq.host=host
spring.rabbitmq.port=5672
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.virtual-host=virtual-host
我们以topic模式为例,springboot提供了一种用bean的方式,在代码里配置绑定队列和交换机:
package com.example.topic;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* create rabbitmq queue exchange and bind by routingKey
*/
@Configuration
public class TopicRabbitMQConfig {
// 队列:queue.example.topic.new
@Bean
public Queue topicQueue() {
return new Queue("queue.example.topic.new");
}
// 交换机:exchange.topic.example.new
@Bean
TopicExchange topicExchange() {
return new TopicExchange("exchange.topic.example.new");
}
// 绑定关系:routing.key.example.new
@Bean
Binding bindingTopicExchange() {
return BindingBuilder
.bind(topicQueue())
.to(topicExchange())
.with("routing.key.example.new");
}
}
生产者:
package com.example.topic;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TopicProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessageByTopic() {
String content = "This is a topic type of the RabbitMQ message example";
this.rabbitTemplate.convertAndSend(
"exchange.topic.example.new",
"routing.key.example.new",
content);
}
}
消费者:
package com.example.topic;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "queue.example.topic.new")
public class TopicConsumer {
@RabbitHandler
public void consumer(String message) {
System.out.println(message);
}
}
写一段测试代码测试一下,RabbitMQ的生产消费:
package com.example;
import com.example.direct.DirectProducer;
import com.example.fanout.FanoutProducer;
import com.example.simple.SimpleProducer;
import com.example.topic.TopicProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {
@Autowired
private TopicProducer topicProducer;
@Test
public void topicProducerTest() {
topicProducer.sendMessageByTopic();
}
}
这样就能在SpringBoot中使用RabbitMQ了!
外三种模式:direct
、fanout
、head
的代码我放在了github上,
地址为:
联系我
关注:java之旅
springboot入门系列(三):SpringBoot教程之RabbitMQ示例的更多相关文章
- SpringBoot入门(三)——入口类解析
本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...
- SpringBoot入门系列(转)
SpringBoot入门系列:第一篇 Hello World http://blog.csdn.net/lxhjh/article/details/51711148
- SpringBoot入门系列(十一)统一异常处理的实现
前面介绍了Spring Boot 如何整合定时任务已经Spring Boot 如何创建异步任务和定时任务.不清楚的朋友可以看看之前的文章:<Spring Boot 入门系列文章> 接下来主 ...
- SpringBoot入门系列(十二)统一日志收集
前面介绍了Spring Boot 异常处理,不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html. 今 ...
- mybatis入门系列三之类型转换器
mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...
- C# 互操作性入门系列(三):平台调用中的数据封送处理
好文章搬用工模式启动ing ..... { 文章中已经包含了原文链接 就不再次粘贴了 言明 改文章是一个系列,但只收录了2篇,原因是 够用了 } --------------------------- ...
- [转]C# 互操作性入门系列(三):平台调用中的数据封送处理
参考网址:https://www.cnblogs.com/FongLuo/p/4512738.html C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列( ...
- SpringBoot入门系列~Spring-Data-JPA自动建表
1.pom.xml引入Spring-Data-Jpa和mysql依赖 <!-- Spring-data-jpa依赖 --> <dependency> <groupId&g ...
- springboot入门系列(二):SpringBoot整合Swagger
上一篇<简单搭建SpringBoot项目>讲了简单的搭建SpringBoot 项目,而 SpringBoot 和 Swagger-ui 搭配在持续交付的前后端开发中意义重大,Swagger ...
随机推荐
- [Java核心技术]第九章-集合(Java集合框架、具体的集合、映射)
9.1Java集合框架 一些有的没的 可以使用接口类型存放集合的引用.一旦改变了想法,只需要在调用构造函数的地方做一处修改. add方法用于向集合添加元素,如果添加元素确实改变了集合就返回true. ...
- python字符串和列表小案例
python 目录 python 一.字符串 1.给定一个字符串,利用切片将字符串反转 2.给定一个字符串,将空格替换为逗号 3.给定一个字符串,大写改为小写 4.str = '' ,li = ['l ...
- .net 中的 StringBuilder 和 TextWriter 区别
最近闲来之余,看了一些开源的类库,看到有些类库喜欢用TextWriter类来记录相关的字符串数据,感到比较好奇,为啥不用StringBuilder类对象.于是在网上搜索了一番,总结了相关笔记. Str ...
- 聊聊分布式下的WebSocket解决方案
前言 最近王子自己搭建了个项目,项目本身很简单,但是里面有使用WebSocket进行消息提醒的功能,大体情况是这样的. 发布消息者在系统中发送消息,实时的把消息推送给对应的一个部门下的所有人. 这里面 ...
- Java JVM参数在idea里面配置
找到你需要配置的启动类选择 接着点击Edit Configurations 在VM options里面配置就完成了
- 刷题[网鼎杯 2020 朱雀组]phpweb
解题思路 打开是一个蛮有意思的背景,众生皆懒狗,是自己没错了.源代码看一看,啥都没有.抓个包 诶,一看到func和p两个参数,想到了call_user_func(). 尝试着把date改成system ...
- 在Linux系统下搭建和配置一个minio文件服务器(二)
上一篇主要讲述了在linux系统中搭建一个minio文件服务器,那么这一篇则用来整合java代码中使用,我之前自己已经搭建好了一个springboot项目,那么这一篇将详细讲述如何把minio整合进s ...
- Spring Boot第四弹,一文教你如何无感知切换日志框架?
持续原创输出,点击上方蓝字关注我吧 目录 前言 Spring Boot 版本 什么是日志门面? 如何做到无感知切换? 如何切换? 引入依赖 指定配置文件 日志如何配置? 总结 前言 首先要感谢一下读者 ...
- Leetcode-递归&分治
50. Pow(x, n) https://leetcode-cn.com/problems/powx-n/ 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 说明: -100.0 < ...
- 卧槽!最新编程语言排名,Java 沦为老二。。
2020 年 9 月刚过去,栈长看了下最新的 tiobe 编程语言榜,牛逼啊,C 语言居然登顶了,Java 下降 3 个点,沦为老二的位置. 数据来源TIOBE: https://www.tiobe. ...