springboot引入rabbit mq
spring boot 集成 rabbit mq
第一步引入springboot 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
第二步添加rabbitmq配置
spring:rabbitmq:host: 127.0.0.1port: 17004username: adminpassword: ******virtual-host: /nonautoexchange: EX_ZY_PROPOSALSTATUSroutingKey: ZY_PROPOSALSTATUSqueue: Q_ZY_PROPOSALSTATUS
第三步添加rabbitmq配置类
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;
/*** @ClassName RabbitConfig* @Description rabbit mq 配置类* @Author God丶Man* @Date 2022/3/19 11:13* @Version 1.0**/
@Configuration
public class RabbitConfig {@Value("${spring.rabbitmq.exchange}")private String exchange;@Value("${spring.rabbitmq.routingKey}")private String routingKey;@Value("${spring.rabbitmq.queue}")private String queue;@Beanpublic Queue proposalQueue() {return new Queue(queue, true);}@Beanpublic DirectExchange proposalExchange() {return new DirectExchange(exchange, true, false);}@Beanpublic Binding bindingDirect() {return BindingBuilder.bind(proposalQueue()).to(proposalExchange()).with(routingKey);}
}
第四步消息生产者消费者测试类
import cn.hutool.json.JSONUtil;
import com.dh.marketplatform.vehicle.BaseTest;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;import java.util.HashMap;
import java.util.Map;/*** @ClassName RabbitMqTest* @Description 消息生产者测试类* @Author God丶Man* @Date 2022/3/19 11:31* @Version 1.0**/
public class RabbitMqTest extends BaseTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void testMq() {// 发送消息 商业险Map<String, Object> paramMap = getStringObjectMap();String reqStr = JSONUtil.toJsonStr(paramMap);System.out.println(reqStr);/*** Convert a Java object to an Amqp {@link Message} and send it to a specific exchange* with a specific routing key.** @param exchange the name of the exchange* @param routingKey the routing key* @param message a message to send* @throws AmqpException if there is a problem*/rabbitTemplate.convertAndSend("EX_ZY_PROPOSALSTATUS", "ZY_PROPOSALSTATUS", reqStr);}private Map<String, Object> getStringObjectMap() {Map<String,Object> paramMap = new HashMap<>();paramMap.put("proposalNo","110250020221230000003");paramMap.put("proposalStatus","0");paramMap.put("riskCode","1230");return paramMap;}}
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.List;/*** @ClassName RabbitMq* @Description 消息消费者测试类* @Author God丶Man* @Date 2022/3/19 11:33* @Version 1.0**/
@Component
@Slf4j
public class RabbitMqConsumer {@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "${spring.rabbitmq.queue}", autoDelete = "false"),exchange = @Exchange(value = "${spring.rabbitmq.exchange}")))public void messagerevice(String message) {log.info("========接受消息打印: " + message);}}
springboot引入rabbit mq相关推荐
- springboot+Rabit实战一:(Rabbit MQ windows 环境搭建)
一:下载erlang 语言并安装: 地址:http://www.erlang.org/downloads 安装时,是可视化界面一步步安装即可,如果没有配置ERLANG_HOME,则需要配置,并在pat ...
- springboot+Rabit实战二:(Rabbit MQ web 界面管理)
本章基于博主上一篇文章:springboot+Rabit实战一:(Rabbit MQ windows 环境搭建)继续深入,介绍rabbit MQ web界面管理操作 一:先了解Rabbit MQ 中一 ...
- Spring Boot:使用Rabbit MQ消息队列
综合概述 消息队列 消息队列就是一个消息的链表,可以把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程可以向消息队列中按照一定的规则添加新消息,对消息队列有读权限的进程则可以 ...
- spring-boot 引入xml注入bean
2019独角兽企业重金招聘Python工程师标准>>> spring-boot 引入xml注入bean 配置 public class TestServiceImpl impleme ...
- Rabbit MQ 学习 (一)Window安装Erlang环境
之前也没有用过Rabbit MQ ,最近正在学习中,记性不好,特意记一下. 百度一下 先得 安装 Erlang 并且 设置环境变量. 在Erlang 官网去下载,那个慢呀... 还好CSDN 里有人提 ...
- spring集成mq_使用Spring Integration Java DSL与Rabbit MQ集成
spring集成mq 我最近参加了在拉斯维加斯举行的2016年Spring大会 ,很幸运地看到了我在软件世界中长期敬佩的一些人. 我亲自遇到了其中的两个人,他们实际上合并了几年前我与Spring In ...
- Rabbit MQ windows下安装
Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接可以下载安装最新的版本: 下载并安装 Eralng OTP For W ...
- springboot引入外部yml配置文件
本文记录下springboot引入外部yml配置文件 文章目录 概述 本文小结 概述 如果不想把所有的配置都写在application.yml文件中,把它拆分成多个yml文件,并在applicatio ...
- Rabbit MQ 配置
主要介绍Rabbit MQ在Aliyun实例中的安装配置 1.安装erlang, Rabbitmq. 1.1 安装erlang a.执行sudo apt-get update b.执行sudo apt ...
最新文章
- 如何实现在H5里调起高德地图APP?(下)
- strstr php文档,php字符串函数学习之strstr()
- R语言ggplot2可视化:为可视化图像添加多行标题(multi line title)并将多行标题居中对齐(center align)
- linux 清空nat,linux 命令iptables -t nat
- sublime运行python代码_怎么用sublime运行python
- 如何用 Linux 技巧大大提高工作效率?
- 【转】java枚举类型ENUM
- C语言图形库简单对比及EGE库的安装小手册
- 如何运营ASO积分墙用户,aso积分墙是什么意思
- USB,蓝牙,以太网,还是WIFI?
- 2021年中国全自动棉纱缠绕机市场趋势报告、技术动态创新及2027年市场预测
- mac上免费的音频剪辑软件在哪里可以快速下载
- Mean-Shift算法
- android 标注 比例换算,android APP UI设计图标注、换算
- python飞机大战简单的实现
- Github Arctic Code Vault 哈哈 自己的项目被存储在北极 1000年
- shell python运维脚本_【Python运维】最简单的Python运维脚本
- XechWic工作室,视频会议,p2p开发库
- 简单介绍一下微信直播
- 温德姆酒店集团计划未来三年内在中国开设500家酒店