Ethon

为什么要有方法,因为懒惰是一种美德。

   :: 首页  :: 新随笔  ::  ::  :: 管理

RabbitMQ 是一个消息中间件,以异步的方式处理消息,实现了与业务之间的解耦,同时还有消息分发,消息缓存等功能。

RabbitMQ使用的是AMQP协议,全名是提供统一消息服务的应用层标准高级消息队列协议。默认启动端口 5672。

在RabbitMQ中主要的概念有:交换机,队列,绑定,路由键等

RabbitMQ的应用场景:https://www.cnblogs.com/wakey/p/12698798.html

Springboot整合RabbitMQ【前提已安装RabbitMQ】

1、添加依赖

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

2、配置application.yml

rabbitmq:
  host: 192.168.59.131
  port: 5672
  username: admin
  password: admin
  virtual-host: /admin

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RobbitMQConfiguration {

    //交换机
    public static final String FANOUT_NAME = "goods_fanoutExchange";

    /**
     * 声明队列1
     */
    @Bean
    public Queue getQueue1(){
        return new Queue("goods_queue1");
    }

    /**
     * 声明队列2
     */
    @Bean
    public Queue getQueue2(){
        return new Queue("goods_queue2");
    }

    /**
     * 声明交换机
     */
    @Bean
    public FanoutExchange getFanoutExchange(){
        return new FanoutExchange(FANOUT_NAME);
    }

    /**
     * 将队列绑定到交换机上
     */
    @Bean
    public Binding getBinding1(){
        return BindingBuilder.bind(getQueue1()).to(getFanoutExchange());
    }

    @Bean
    public Binding getBinding2(){
        return BindingBuilder.bind(getQueue2()).to(getFanoutExchange());
    }
}

 

4、新增商品后,将商品信息添加到RabbitMQ中

import com.alibaba.dubbo.config.annotation.Service;
import com.qf.dao.GoodsMapper;
import com.qf.entity.Goods;
import com.qf.service.IGoodsService;
import com.qf.shop_service_goods.RobbitMQConfiguration; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; @Service public class GoodsServiceimpl implements IGoodsService { @Autowired private GoodsMapper goodsMapper; @Autowired private RabbitTemplate rabbitTemplate; @Override public int insert(Goods goods) { //添加商品 int result = goodsMapper.insert(goods); //添加商品的信息放到RobbitMQ中 rabbitTemplate.convertAndSend(RobbitMQConfiguration.FANOUT_NAME,"",goods); return result; } }

 

5、监听RabbitMQ消息1

import com.qf.entity.Goods;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RabbitMQListener { @Autowired private SolrClient solrClient; //监听指定的队列 @RabbitListener(queues = "goods_queue1") public void Msg(Goods goods){ //接收MQ消息 System.out.println("搜索服务接收到MQ消息: "+goods); } }

6、监听RabbitMQ消息2

import com.qf.entity.Goods;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RabbitMQListener { @RabbitListener(queues = "goods_queue2") public void Msg(Goods goods){ System.out.println("页面静态化工程接收到的MQ消息: "+goods); } }

7、Goods实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods implements Serializable {
    @TableId(type = IdType.AUTO)  //主键自动回填
    private int id;
    private String gname;
    private BigDecimal gprice;
    private int gsave;
    private String ginfo;
    private String gimage;
    private int status;
    private Date createtime = new Date();
    private int tid;
}

 

 

 

 ================ linux环境上RabbitMQ的安装===============================

1、安装Rabbitmq的语言环境 - erlang

      rpm -ivh erlang-20.1.7-1.el6.x86_64.rpm
 
2、安装Rabbitmq
      rpm -ivh rabbitmq-server-3.4.1-1.noarch.rpm
 
3、启动/停止rabbitmq的服务
      service rabbitmq-server start/stop/restart
 
4、拷贝并且修改配置文件
        cd /usr/share/doc/rabbitmq-server-3.4.1
        cp rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
 
        修改配置文件:
        vim /etc/rabbitmq/rabbitmq.config
注意:重启rabbitmq的服务
 
开启后台管理页面:
cd /usr/share/doc/rabbitmq-server-3.4.1
rabbitmq-plugins enable rabbitmq_management
 
5、开启两个端口
      5672:是rabbitmq的操作端口
     15672:是rabbitmq后台管理工具的端口
 
6、验证:  xx.xx.xx.xx:15672

 

 

posted on 2019-04-13 15:51  Ethon  阅读(273)  评论(0编辑  收藏  举报