Spring boot 集成MQ
import lombok.extern.java.Log;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: RabbitConfig
* @description:
* @date 2018/6/22:10:00
*/
@Configuration
@Log
public class RabbitConfig {
@Qualifier("firstConnectionFactory")
@Autowired
public ConnectionFactory connectionFactory; @Autowired
public MQProperties mqProperties; @Bean(name="firstRabbitTemplate")
public RabbitTemplate firstRabbitTemplate(){
log.info("########################初始化rabitTemplate################################");
RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
return firstRabbitTemplate;
} @Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
return factory;
} @Bean
public TopicExchange defaultExchange() {
return new TopicExchange(mqProperties.getCar_exchange());
}
package com.car.config; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: ConnectionFactoryConfig
* @description:初始化数据源
* @date 2018/6/22:11:30
*/
@Configuration
public class ConnectionFactoryConfig {
@Bean(name="firstConnectionFactory")
public ConnectionFactory firstConnectionFactory(@Value("${spring.rabbitmq.car_host}") String host,
@Value("${spring.rabbitmq.car_port}") int port,
@Value("${spring.rabbitmq.car_username}") String username,
@Value("${spring.rabbitmq.car_password}") String password,
@Value("${spring.rabbitmq.car_vhost}") String virtualhost
){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualhost);
return connectionFactory;
}
}
package com.car.config; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: RabbitMQ_Queue
* @description:创建队列
* @date 2018/6/22:13:09
*/
@Configuration
@Component
public class RabbitMQQueue extends RabbitConfig{
Logger logger = LoggerFactory.getLogger(RabbitMQQueue.class); public RabbitMQQueue(){
logger.info("############### RabbitMQ Queue 创建 ###############");
} @Bean
public Queue CreationCarQueue() {
logger.info("创建队列:{}", mqProperties.getCar_queuename());
return new Queue(mqProperties.getCar_queuename());
} @Bean
public Binding binding() {
logger.info("绑定队列,exchange:{},routingKey:{},queueName:{}", mqProperties.getCar_exchange(), mqProperties.getCar_routingkey(), mqProperties.getCar_queuename());
return BindingBuilder.bind(CreationCarQueue()).to(defaultExchange()).with(mqProperties.getCar_routingkey());
}
}
package com.car.modules.MqMsg.mq; import com.carloan.feign.info.InserttheorderServiceFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: ReqCarMqMsg
* @description:配置监听
* @date 2018/5/28:10:50
*/ @Component
public class ReqCarMqMsg{ private Logger logger = LoggerFactory.getLogger(ReqCarMqMsg.class); @RabbitListener(queues = "${spring.rabbitmq.car_queuename}", containerFactory = "rabbitListenerContainerFactory")
public void process(@Payload byte[] bytes) throws UnsupportedEncodingException {
String result = null;
try {
result = new String(bytes, "UTF8");
logger.info("监听到消息==========="+ result);
} catch (UnsupportedEncodingException e) {
logger.error("监听车贷门店Mq: insuranceTrial UnsupportedEncodingException error :{}",e);
} catch (Exception e){
logger.error("监听车贷门店Mq: insuranceTrial Exception error :{}",e);
}
}
}
package com.car.modules.MqMsg.mq; import com.car.config.MQProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import java.util.UUID; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: PushMq
* @description:推送消息
* @date 2018/6/25:9:52
*/
@Component
public class PushMq {
Logger logger = LoggerFactory.getLogger(PushMq.class); @Qualifier("firstRabbitTemplate")
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
public MQProperties mqProperties; public void pushmessage(String message){
try{
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(mqProperties.getCar_exchange(), mqProperties.getCar_put_routingkey(), message,correlationId);
logger.info("推送消息:{}", message);
}catch (Exception e){
logger.error("推送异常:{}",e);
} }
}
Spring boot 集成MQ的更多相关文章
- Spring boot集成Rabbit MQ使用初体验
Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...
- Spring boot集成RabbitMQ(山东数漫江湖)
RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
- Spring Boot 集成 RabbitMQ 实战
Spring Boot 集成 RabbitMQ 实战 特别说明: 本文主要参考了程序员 DD 的博客文章<Spring Boot中使用RabbitMQ>,在此向原作者表示感谢. Mac 上 ...
- Spring Boot集成Jasypt安全框架
Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...
- Spring boot集成swagger2
一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...
- Spring Boot 集成 Swagger,生成接口文档就这么简单!
之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...
- spring boot 集成 zookeeper 搭建微服务架构
PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...
- Spring Boot 集成Swagger
Spring Boot 集成Swagger - 小单的博客专栏 - CSDN博客https://blog.csdn.net/catoop/article/details/50668896 Spring ...
随机推荐
- .NET C#中处理Url中文编码问题
近些日子在做一个用C#访问webservise的程序,由于需要传递中文参数去请求网站,所以碰到了中文编码问题.我们知道像百度这种搜索引擎中,当用户输入中文关键字后,它会把中文转码,以确保在Url中不会 ...
- shell脚本学习之6小时搞定(1)
shell脚本学习之6小时搞定(1) 简介 Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把 ...
- CTF常见编码及加解密(超全)
@ 目录 前言 常见CTF编码及加解密 补充 ASCII编码 base家族编码 MD5.SHA1.HMAC.NTLM等类似加密型 1.MD5 2.SHA1 3.HMAC 4.NTLM 5.类似加密穷举 ...
- Centos 6.5 Rabbitmq 安装和集群,镜像部署
centos 6.5 rabbitmq 安装和集群,镜像部署 安装erlang: yum install gcc glibc-devel make ncurses-devel openssl-deve ...
- ip访问本机vs调试项目
环境:win10 vs2019 webapi F5启动调试. 问题:localhost可以访问,127.0.0.1和本机ip访问不了.比如想让别人浏览一下看效果,或者测试人员测试功能,每次修改都有重新 ...
- 【System】I/O密集型和CPU密集型工作负载之间有什么区别
CPU密集型(CPU-bound) CPU密集型也叫计算密集型,指的是系统的硬盘.内存性能相对CPU要好很多,此时,系统运作大部分的状况是CPU Loading 100%,CPU要读/写I/O(硬盘/ ...
- 理解C#中的 async await
前言 一个老掉牙的话题,园子里的相关优秀文章已经有很多了,我写这篇文章完全是想以自己的思维方式来谈一谈自己的理解.(PS:文中涉及到了大量反编译源码,需要静下心来细细品味) 从简单开始 为了更容易理解 ...
- Linux内核分析_课程学习总结报告
请您根据本课程所学内容总结梳理出一个精简的Linux系统概念模型,最大程度统摄整顿本课程及相关的知识信息,模型应该是逻辑上可以运转的.自洽的,并举例某一两个具体例子(比如读写文件.分配内存.使用I/O ...
- python-列表包字典-根据字典的某一个键的值来进行排序
python-列表包字典-根据字典的某一个键的值来进行排序 列表包字典的数据结构 要实现按照字典中的某一个键所对应的值进行排序 有两种办法 方法一,使用列表的sort方法 由小到大排 列表.sort( ...
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
作为后台服务开发,在日常工作中我们天天都在跟数据库打交道,一直在进行各种CRUD操作,都会使用到数据库连接池.按照发展历程,业界知名的数据库连接池有以下几种:c3p0.DBCP.Tomcat JDBC ...