1、引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2、配置文件

rabbitmq:
addresses: 10.0.0.203
port: 5672
username: root
password: 123456
virtual-host: /
listener:
simple:
concurrency: 10
max-concurrency: 10
prefetch: 1
auto-startup: true
default-requeue-rejected: true
template:
retry:
enabled: true
initial-interval: 1000
max-attempts: 3
max-interval: 10000
multiplier: 1

2、注解配置

  1. package dhht.seal.hn.gsgate.rabbitmq;
  2.  
  3. import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.FanoutExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
    import org.springframework.amqp.rabbit.connection.ConnectionFactory;
    import org.springframework.amqp.rabbit.core.RabbitAdmin;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
  4.  
  5. /**
    * @Author: sh
    * @Description: RabbitMqConfig
    * @Date: 10:33 2019/11/4
    */
    @Configuration
    public class RabbitMqConfig {
  6.  
  7. public static final String QUEUE = "hnyz_gs_queue";
  8.  
  9. @Value("${spring.rabbitmq.addresses}")
    private String host;
  10.  
  11. @Value("${spring.rabbitmq.port}")
    private int port;
  12.  
  13. @Value("${spring.rabbitmq.username}")
    private String username;
  14.  
  15. @Value("${spring.rabbitmq.password}")
    private String password;
  16.  
  17. @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;
  18.  
  19. public static final String GS_EXCHANGE ="gs_exchange";
  20.  
  21. /**
    * 处理连接端口
    * @return
    */
    @Bean
    public CachingConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
    connectionFactory.setUsername(username);
    connectionFactory.setPassword(password);
    connectionFactory.setVirtualHost(virtualHost);
    connectionFactory.setPublisherConfirms(true);
    return connectionFactory;
    }
  22.  
  23. @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    admin.setAutoStartup(true);
    return admin;
    }
  24.  
  25. @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
    RabbitTemplate template =new RabbitTemplate(connectionFactory);
    template.setUseDirectReplyToContainer(false);
    template.setReplyTimeout(-1);
    return template;
    }
  26.  
  27. @Bean
    public Queue cretaeQueue(){
    return new Queue(QUEUE,true);
    }
  28.  
  29. @Bean(name="gs_exchange")
    public FanoutExchange getGsExchange() {
    return new FanoutExchange("gs_exchange", true, false, null);
    }
  30.  
  31. @Bean
    public Binding getFauoutBinding() {
    return BindingBuilder.bind(cretaeQueue()).to(getGsExchange());
    }
    }
  1. package dhht.seal.hn.gsgate.rabbitmq;
  2.  
  3. import com.alibaba.fastjson.JSON;
    import dhht.seal.hn.gsgate.model.pojo.CropQueryVO;
    import dhht.seal.hn.gsgate.service.CropQueryService;
    import dhht.seal.hn.gsgate.service.impl.CropServiceImpl;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessageProperties;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.ParameterizedTypeReference;
    import org.springframework.stereotype.Service;
  4.  
  5. import javax.annotation.Resource;
    import java.io.UnsupportedEncodingException;
    import java.lang.reflect.ParameterizedType;
  6.  
  7. /**
    * @Author: sh
    * @Description: MqSender
    * @Date: 10:34 2019/11/4
    */
    @Slf4j
    @Service
    public class MqSenderService {
  8.  
  9. @Autowired
    AmqpTemplate amqpTemplate;
  10.  
  11. @Autowired
    RabbitTemplate rabbitTemplate;
  12.  
  13. @Resource
    private CropServiceImpl cropImplService;
  14.  
  15. public String sendMsgToQueue(Object message) {
    try {
    String msg = beanToString(message);
    log.info("【发送的消息-社会信用代码】:" + msg);
    Message mm = rabbitTemplate.sendAndReceive(RabbitMqConfig.QUEUE, new Message(msg.getBytes("UTF-8"),new MessageProperties()));
    String msgResult = new String(mm.getBody());
    log.info("【同步消息返回结果-msgResult】:"+msgResult);
    CropQueryVO cropQueryVO = cropImplService.cropQueryVO(msg);
    log.info("【消息发送后-sql查询结果-CropQueryVO】:"+JSON.toJSONString(cropQueryVO));
    return JSON.toJSONString(cropQueryVO);
    //amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
    //ParameterizedTypeReference param = new ParameterizedTypeReference<Object>(){};
    //return amqpTemplate.convertSendAndReceiveAsType(RabbitMqConfig.QUEUE, beanToString(message),param).toString();
    //return amqpTemplate.convertSendAndReceive(RabbitMqConfig.QUEUE, message).toString();
    } catch (UnsupportedEncodingException e) {
    log.info(e.getMessage());
    return null;
    }
  16.  
  17. }
  18.  
  19. public void sendMsg(Object message) {
    String msg = beanToString(message);
    log.info("send message:" + msg);
    amqpTemplate.convertAndSend(RabbitMqConfig.QUEUE, msg);
    log.info("sendMsg()---消息发送成功!");
  20.  
  21. }
  22.  
  23. public static <T> String beanToString(T value) {
    if (value == null) {
    return null;
    }
    Class<?> clazz = value.getClass();
    if (clazz == int.class || clazz == Integer.class) {
    return "" + value;
    } else if (clazz == String.class) {
    return (String) value;
    } else if (clazz == long.class || clazz == Long.class) {
    return "" + value;
    } else {
    return JSON.toJSONString(value);
    }
    }
  24.  
  25. }
  1. package dhht.seal.hn.gsgate.rabbitmq;
  2.  
  3. import dhht.seal.hn.gsgate.service.CropQueryService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.messaging.handler.annotation.SendTo;
    import org.springframework.stereotype.Service;
  4.  
  5. import javax.annotation.Resource;
    import java.io.UnsupportedEncodingException;
  6.  
  7. /**
    * @Author: sh
    * @Description: MqReceiver
    * @Date: 10:40 2019/11/4
    */
    @Service
    public class MqReceiverService {
  8.  
  9. private static Logger log = LoggerFactory.getLogger(MqReceiverService.class);
  10.  
  11. @Resource
    private CropQueryService cropQueryService;
  12.  
  13. @RabbitListener(queues = RabbitMqConfig.QUEUE)
    @SendTo(RabbitMqConfig.QUEUE)
    public String receiveQueueMsg(/*String message*/Message message) {
    try {
    log.info("接收到队列消息:" + new String(message.getBody()));
    // 业务处理代码,工商拉取入库
    String resJson = cropQueryService.crropQuery(new String(message.getBody(),"UTF-8"));
    return resJson;
    } catch (UnsupportedEncodingException e) {
    log.error(e.getMessage());
    return null;
    }
    }
    }
  1.  
  1.  

springboot rabbitmq消息同步用作接口调用的更多相关文章

  1. SpringBoot 动态代理实现三方接口调用

    目录 一.定义注解 二.建立动态代理类 三.注入spring容器 四.编写拦截器 五.创建客户端调用类 六.main方法测试 七.启动项目 在某些业务场景中,我们只需要业务代码中定义相应的接口或者相应 ...

  2. Springboot+Dubbo使用Zipkin进行接口调用链路追踪

    Zipkin介绍: Zipkin是一个分布式链路跟踪系统,可以采集时序数据来协助定位延迟等相关问题.数据可以存储在cassandra,MySQL,ES,mem中.分布式链路跟踪是个老话题,国内也有类似 ...

  3. SpringBoot之RESTFul风格的接口调用(jQuery-Ajax)

    一.Get $.ajax({ type: "get", url: "url地址", async: true, dataType:"json" ...

  4. springboot + rabbitmq 用了消息确认机制,感觉掉坑里了

    本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 最近部门号召大伙多组织一些技术分享会,说是要活跃公司的技术氛围,但早就看穿一切的我知道,这 T M 就是为了刷KPI ...

  5. 刚体验完RabbitMQ?一文带你SpringBoot+RabbitMQ方式收发消息

    人生终将是场单人旅途,孤独之前是迷茫,孤独过后是成长. 楔子 这篇是消息队列RabbitMQ的第二弹. 上一篇的结尾我也预告了本篇的内容:利用RabbitTemplate和注解进行收发消息,还有一个我 ...

  6. SpringBoot+RabbitMQ 方式收发消息

    本篇会和SpringBoot做整合,采用自动配置的方式进行开发,我们只需要声明RabbitMQ地址就可以了,关于各种创建连接关闭连接的事都由Spring帮我们了~ 交给Spring帮我们管理连接可以让 ...

  7. (转)RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)

    在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇文章中,我们将会 ...

  8. 消息同步调用-- ESFramework 4.0 进阶(07)

    分布式系统的构建一般有两种模式,一是基于消息(如Tcp,http等),一是基于方法调用(如RPC.WebService.Remoting).深入想一想,它们其实是一回事.如果你了解过.NET的Prox ...

  9. RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)

            在云计算环境中,很多时候需要用它其他机器的计算资源,我们有可能会在接收到Message进行处理时,会把一部分计算任务分配到其他节点来完成.那么,RabbitMQ如何使用RPC呢?在本篇 ...

随机推荐

  1. NIO三大组件简介

    NIO简介 NIO 是面向缓冲区(或者说面向块)编程的, 因为Buffer底层本质上就是内存块.数据被读取到一个缓冲区, 稍后再被它处理, 需要时数据可在缓冲区前后移动, 从而增加了处理过程中的灵活性 ...

  2. Python中语法糖及带参语法糖

    在python中,@符号常被称作语法糖(装饰器),在某函数定义时,用以包装该函数,以达到截取,控制该函数的目的. def d(f): print('d...') k=f #此处保留了传进来的原函数 f ...

  3. sql server 2012插入排序后的数据到临时表无效

    IF OBJECT_ID('TEMPDB..#list') IS NOT NULLBEGIN DROP TABLE TEMPDB.#list END CREATE TABLE #list(OFC_ID ...

  4. 小米百元NFC智能神器来了:必成爆款!

    6月14日,小米手环4全渠道首卖,其中电商平台今日10:00开卖,线下小米之家需要顾客持本人身份证限购一台. 首销结束后,米家官微宣布,小米手环4下一轮开售时间在6月18日上午10:00,届时小米手环 ...

  5. Live555研究之一 源代码编译

    Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现了对多种音视频编码格式的音视频数据的流化 ...

  6. Redis 详解 (二) redis的配置文件介绍

    目录 1.开头说明 2.INCLUDES 3.MODULES 4.NETWORK 5.GENERAL 6.SNAPSHOTTING 7.REPLICATION 8.SECURITY 9.CLIENTS ...

  7. Spring-IOC(基于XML配置)

    什么是IOC? IOC: Inversion of Control(控制反转). 控制反转:将对象的创建权反转给(交给)Spring. 在使用Spring框架之后,对象的实例不再由调用者来创建,而 是 ...

  8. HDU_4965 Fast Matrix Calculation 2014多校9 矩阵快速幂+机智的矩阵结合律

    一开始看这个题目以为是个裸的矩阵快速幂的题目, 后来发现会超时,超就超在  M = C^(N*N). 这个操作,而C本身是个N*N的矩阵,N最大为1000. 但是这里有个巧妙的地方就是 C的来源其实 ...

  9. express连接数据库 读取表

    connection 连接数据库    connection.query 查询表   1.依赖 const mysql = require('mysql'); 连接数据库代码 var connecti ...

  10. SMPL模型Shape和Pose参数

    两部分 1.Pose参数 2.Shape参数 一 Pose参数 共24个关节点,对应idx从0到23,图中3个小图分别表示zero shape只有idx节点分别绕x/y/z轴旋转. 其中蓝色线表示-p ...