温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot整合rabbitmq(Springboot2.X)

发布时间:2020-06-06 23:52:20 来源:网络 阅读:908 作者:nineteens 栏目:编程语言

  一、整合前的准备

  1.1、安装windows/linux系统版本的rabbitmq。

  1.2、此次整合使用eclipse

  二、springboot整合rabbitmq

  2.1、pom依赖引入

  org.springframework.boot

  spring-boot-starter-amqp

  2.2、yml配置和configer文件编写

  spring:

  rabbitmq:

  host: 127.0.0.1

  port: 5672

  username: xiangjiao

  password: bunana

  virtual-host: /xiangjiao

  #publisher-confirms: true #消息发送到转发器确认机制,是都确认回调

  #publisher-returns: true

  import org.springframework.amqp.core.Binding;

  import org.springframework.amqp.core.BindingBuilder;

  import org.springframework.amqp.core.DirectExchange;

  import org.springframework.amqp.core.Queue;

  import org.springframework.context.annotation.Bean;

  import org.springframework.context.annotation.Configuration;

  @Configuration

  public class MQConfiguration {

  //队列名称

  public static final String QUEUQ_NAME = "xiangjiao.queue";

  //交换器名称

  public static final String EXCHANGE = "xiangjiao.exchange";

  //路由key

  public static final String ROUTING_KEY = "xiangjiao.routingKey";

  //创建队列

  @Bean

  public Queue getQueue(){

  return new Queue(QUEUQ_NAME);

  }

  //实例化交换机

  @Bean

  public DirectExchange getDirectExchange(){

  //DirectExchange(String name, boolean durable, boolean autoDelete)

  return new DirectExchange(EXCHANGE, true, true);

  }

  //绑定消息队列和交换机

  @Bean

  public Binding bindExchangeAndQueue(DirectExchange exchange,Queue queue){

  return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);

  }

  }

  2.3、消息消费者监听创建

  import org.springframework.amqp.rabbit.annotation.RabbitListener;

  import org.springframework.stereotype.Service;

  import cn.linkpower.config.MQConfiguration;

  @Service郑州人流医院 http://m.zzzy120.com/

  public class MessageConsumerService {

  @RabbitListener(queues=MQConfiguration.QUEUQ_NAME)

  public void receiveMessage(String msg){

  System.err.print("红色 接收消息----"+String.valueOf(msg));

  }

  }

  2.4、消息生产者

  2.4.1、创建消息生产者接口

  /**

  * 定义消息发送接口

  * @author 765199214

  *

  */

  public interface IMessageProductService {

  public void sendMessage(String msg);

  }

  2.4.2、创建消息消费者实现类

  import org.springframework.amqp.rabbit.core.RabbitTemplate;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.stereotype.Service;

  import cn.linkpower.config.MQConfiguration;

  import cn.linkpower.service.IMessageProductService;

  @Service

  public class MessageProductServiceImpl implements IMessageProductService {

  @Autowired

  private RabbitTemplate rabbitTemplate;

  @Override

  public void sendMessage(String msg) {

  rabbitTemplate.convertAndSend(MQConfiguration.EXCHANGE,

  MQConfiguration.ROUTING_KEY,

  msg);

  }

  }

  2.5、创建控制器

  @RequestMapping("/sendMsg2")

  @ResponseBody

  public String sendMsg2(HttpServletRequest request){

  String msg = request.getParameter("msg");

  log.info("sendMsg --- msg--"+String.valueOf(msg));

  messageProductServiceImpl.sendMessage(msg);

  return "send ok";

  }

  三、测试

  控制台输出:

  

springboot整合rabbitmq(Springboot2.X)


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI