安装rabbitmq 设置用户_104、SpringBoot整合RabbitMQ

本文详细介绍了RabbitMQ的安装过程,包括Erlang环境的配置,RabbitMQ Server的安装与Web管理界面的启用。此外,还讲述了如何创建用户并设置权限。接着,文章通过一个入门案例展示了如何在SpringBoot中使用RabbitMQ,讲解了RabbitMQ的核心概念,如交换器、路由键、绑定等,并详细解释了Direct、Topic和Fanout三种交换器的使用场景和配置。
摘要由CSDN通过智能技术生成

一、 RabbitMQ介绍

1.什么是RabbitMQ?

MQ全称为Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。其中较为成熟的MQ产品有IBM WEBSPHERE MQ等等。

2.为什么要使用 RabbitMQ ?

a60a39f6b85323d889dd3134f9ca48e2.png

6971e2e771f0bd08767ad245757dd589.png

3.RabbitMQ解决了什么问题?

1a57c42f557db155e9df1895ba8056a0.png

二、安装RabbitMQ

1. 安装Erlang

什么是Erlang

Erlang(['ə:læŋ])是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab 开发,目的是创造一种可以应对大规模并发活动的编程语言和运行环境

安装步骤:

上传esl-erlang_21.0-1_centos_6_amd64.rpm

6fa14536996812d226e53fa34e7744f9.png

运行命令:yum install unixODBC unixODBC-devel wxBase wxGTK SDL wxGTK-gl

3e5034f8b40c99829650a51ccd2a9896.png

运行命令:rpm -ivh esl-erlang_21.0-1_centos_6_amd64.rpm

6659d04b45a8c21176af430b76b3e43a.png

运行命令:yum install erlang

771bf6f92bf3aabbf5c38836c2f43203.png

测试:

安装完成后可以用erl命令查看是否安装成功

erl -version

6ba51ae8afb896175d3a714c1d844d95.png

2.安装RabbitMQ Server

上传rabbitmq-server-3.7.18-1.el6.noarch.rpm

运行:rpm -ivh --nodeps rabbitmq-server-3.7.18-1.el6.noarch.rpm

a799ed5055dd921850476c30cd222047.png

启动、停止

service rabbitmq-server start
service rabbitmq-server stop
service rabbitmq-server restart
service rabbitmq-server status

fcc83022e8ab9a9ee208bbda6454c02a.png

044b57b6b6e3ba7b90022e223a0a878b.png

设置开机启动

chkconfig rabbitmq-server on

a59e88a89204a770d1605916eefbed07.png

安装Web管理界面插件

rabbitmq-plugins enable rabbitmq_management

e6798f83eebe3fdf4f1b870b3a382961.png

设置RabbitMQ远程ip登录

这里我们以创建个admin帐号,密码1111为例,创建一个账号并支持远程ip访问。

1)创建账号

rabbitmqctl add_user admin 1111

255c1116d051cf9bcdd437a26ea5c137.png

2)设置用户角色

rabbitmqctl set_user_tags admin administrator

5bbaee973dedc378a0d807e30f529bf6.png

3)设置用户权限

rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"

08b86c6e72761aaa5c05f26efe255d6d.png

4)设置完成后可以查看当前用户和角色(需要开启服务)

rabbitmqctl list_users

c77828c30118b7e73f8ad129c3ce8874.png

账号guest具有所有的操作权限,并且又是默认账号,出于安全因素的考虑,guest用户只能通过localhost登陆使用

5)测试

浏览器输入:serverip:15672。其中serverip是RabbitMQ-Server所在主机的ip,15672是RabbitMQ-Server的端口号

15510fc95972fae6f279cbf1a07c37a5.png

管理界面中的功能

d33391bedcb68d192a46826cd0bd070a.png

21b54b8ab33d2922454422c4dd7857be.png

3.消息队列基础知识

Provider

消息生产者,就是投递消息的程序。

Consumer

消息消费者,就是接受消息的程序。

没有使用消息队列时消息传递方式

8a576c2ae7096f70356f65231d746af0.png

使用消息队列后消息传递方式

354466aa7acccf82770695186d6ae64c.png

什么是队列?

队列就像存放了商品的仓库或者商店,是生产商品的工厂和购买商品的用户之间的中转站

队列里存储了什么?

在 rabbitMQ 中,信息流从你的应用程序出发,来到 Rabbitmq 的队列,所有信息可以只存储在一个队列中。队列可以存储很多信息,因为它基本上是一个无限制的缓冲区,前提是你的机器有足够的存储空间。

队列和应用程序的关系?

多个生产者可以将消息发送到同一个队列中,多个消息者也可以只从同一个队列接收数据。

三、编写RabbitMQ 的入门案例

1.搭建项目

f2e28a2a0e771aa506d8d6bbe9b57317.png

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sxt</groupId>
    <artifactId>springcloud-mq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-mq</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties配置文件

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

2.编写代码

消息队列

/**
 * 创建消息队列
 */
@Configuration
public class QueueConfig {
    //创建对列
    @Bean
    public Queue createQueue(){
        return new Queue("hello-queue");
    }
}

消息发送者

/**
 * 消息发送者
 */
@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;
    /**
     * 发送消息的方法
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数一:队列的名称。
        //参数二:消息
        this.rabbitAmqpTemplate.convertAndSend("hello-queue", msg);
    }
}

消息接收者

/**
 * 消息接受者
 */
@Component
public class Receiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitListener(queues= "hello-queue")
    public void process(String msg){
        System.out.println("receiver:"+msg);
    }
}

启动类

@SpringBootApplication
public class SpringcloudMqApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudMqApplication.class, args);
    }

}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes=SpringcloudMqApplication.class )
public class QueueTest {
    @Autowired
    private Sender sender;

    @Test
    public void test1() {
        this.sender.send("Hello RabbitMQ");
    }

}

1c693644ddfbadb7efd644673378c71b.png

四、RabbitMQ核心概念

b2c4768de6a11935959dafacc030c3e0.png

1. Queue

消息队列。用来保存消息直到发送给消费者。它是消息的容器,也是消息的终点。一

个消息可投入一个或多个队列。消息一直在队列里面,等待消费者链接到这个队列将其取走。

2. Exchange

交换器。用来接收生产者发送的消息并将这些消息路由给服务器中的队列。

三种常用的交换器类型

1)direct(发布与订阅 完全匹配)

2)fanout(广播)

3)topic(主题,规则匹配)

3. Routing-key

路由键。RabbitMQ 决定消息该投递到哪个队列的规则。

队列通过路由键绑定到交换器。

消息发送到 MQ 服务器时,消息将拥有一个路由键,即便是空的,RabbitMQ 也会将其

和绑定使用的路由键进行匹配。

如果相匹配,消息将会投递到该队列。

如果不匹配,消息将会进入黑洞。

4.Binding

绑定。用于消息队列和交换器之间的关联。一个绑定就是基于路由键将交换器和消息

队列连接起来的路由规则,所以可以将交换器理解成一个由绑定构成的路由表。

5.Connection

链接。指 rabbit 服务器和服务建立的 TCP 链接。

6. Channel

信道。

1)Channel 中文叫做信道,是 TCP 里面的虚拟链接。例如:电缆相当于 TCP,信道是

一个独立光纤束,一条 TCP 连接上创建多条信道是没有问题的。

2)TCP 一旦打开,就会创建 AMQP 信道。

3)无论是发布消息、接收消息、订阅队列,这些动作都是通过信道完成的。

五、Rabbit交换器

交换器。用来接收生产者发送的消息并将这些消息路由给服务器中的队列。

三种常用的交换器类型

1. direct(发布与订阅 完全匹配)

2. fanout(广播)

3. topic(主题,规则匹配)

1.Direct交换器( 发布与订阅 完全匹配)

需求:

9b6c9f306b518f089ba88ab9217ef6e4.png

1.1搭建环境

创建项目

601a866c9c744499ab4d9957510b1003.png

修改全局配置文件

修改 Consumer 的配置文件

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

#设置交换器的名称
mq.config.exchange=log.direct

#info队列的名称
mq.config.queue.info=log.info

#info路由键
mq.config.queue.info.routing.key=log.info.routing.key

#error队列名称
mq.config.queue.error=log.error

#error路由键
mq.config.queue.error.routing.key=log.error.routing.key

修改 Provider 的配置文件

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

#设置交换器的名称
mq.config.exchange=log.direct

#info路由键
mq.config.queue.info.routing.key=log.info.routing.key

#error路由键
mq.config.queue.error.routing.key=log.error.routing.key

1.2编写代码

消息接收者

InfoReceiver

package com.sxt.rabbitmqdirectconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener bindings:绑定队列
 *
 * @QueueBinding
 * 				value:绑定队列的名称
 *              exchange:配置交换器
 *              key:路由键值
 *
 * @Queue
 * 		 value:配置队列名称
 *       autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange
 * 			value:为交换器起个名称
 *          type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.info}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.DIRECT),
                key = "${mq.config.queue.info.routing.key}"
        )
)
public class InfoReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("Info................receiver:"+msg);
    }
}

ErrorReceiver

package com.sxt.rabbitmqdirectconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener bindings:绑定队列
 *
 * @QueueBinding
 * 				value:绑定队列的名称
 *              exchange:配置交换器
 *              key:路由键值
 *
 * @Queue
 * 		 value:配置队列名称
 *       autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange
 * 			value:为交换器起个名称
 *          type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.DIRECT),
                key = "${mq.config.queue.error.routing.key}"
        )
)
public class ErrorReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("Error................receiver:"+msg);
    }
}

消息发送者

package com.sxt.rabbitmqdirectprovider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 消息发送者
 */
@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    @Value("${mq.config.exchange}")
    private String exchange;

    @Value("${mq.config.queue.error.routing.key}")
    private String routingkey;
    /**
     * 发送消息的方法
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数一:交换器的名称。
        //参数二:路由键
        //参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,this.routingkey, msg);
    }
}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitmqDirectProviderApplication.class)
public class ProviderTest {

    @Autowired
    private Sender sender;

    @Test
    public void test1() throws Exception{
        while (true){
            Thread.sleep(1000);
            this.sender.send("Hello RabbitMQ");
        }
    }

}

2501902a674bd25de86d4180e2f655bc.png

5d11a0980f4724772a0f4c29ecbf059a.png

六、Topic 交换器( 主题,规则匹配)

1.需求

1046aba3a5d36f08ba85d5a32a3ce42b.png

2.搭建环境

创建项目参考Direct交换器的工程

a58cfe69d703f1f80f9a46153f389ced.png

3.修改全局配置文件

Consumer

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

#设置交换器的名称
mq.config.exchange=log.topic

#info队列的名称
mq.config.queue.info=log.info

#error队列名称
mq.config.queue.error=log.error

#error队列名称
mq.config.queue.logs=log.all

Provider

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

#设置交换器的名称
mq.config.exchange=log.topic

4.编写代码

消息接收者

InfoReceiver

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.info}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.TOPIC),
                key = "*.log.info"
        )
)
public class InfoReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("......Info................receiver:"+msg);
    }
}

ErrorReceiver

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.TOPIC),
                key = "*.log.error"
        )
)
public class ErrorReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("......Error................receiver:"+msg);
    }
}

LogsReceiver

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.logs}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.TOPIC),
                key = "*.log.*"
        )
)
public class IogsReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("......All................receiver:"+msg);
    }
}

消息发送者

UserSender

package com.sxt.rabbitmqtopicprovider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 消息发送者
 */
@Component
public class UserSender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数一:交换器的名称。
        //参数二:路由键
        //参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.debug", "user.log.debug...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.info", "user.log.info...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.warn", "user.log.warn...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.error", "user.log.error...."+msg);
    }
}

ProductSender

package com.sxt.rabbitmqtopicprovider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 消息发送者
 */
@Component
public class ProductSender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数一:交换器的名称。
        //参数二:路由键
        //参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.debug", "product.log.debug...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.info", "product.log.info...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.warn", "product.log.warn...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.error", "product.log.error...."+msg);
    }
}

OrderSender

package com.sxt.rabbitmqtopicprovider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 消息发送者
 */
@Component
public class OrderSender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数一:交换器的名称。
        //参数二:路由键
        //参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.debug", "order.log.debug...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.info", "order.log.info...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.warn", "order.log.warn...."+msg);
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.error", "order.log.error...."+msg);
    }
}

测试

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitmqTopicProviderApplication.class)
public class RabbitmqTopicProviderApplicationTests {

    @Autowired
    private UserSender userSender;
    @Autowired
    private ProductSender productSender;
    @Autowired
    private OrderSender orderSender;

    @Test
    public void test1(){
        this.userSender.send("UserSender......");
        this.productSender.send("ProductSender......");
        this.orderSender.send("OrderSender......");
    }

}

ec5c46b4909b3c8b1ce329863e485752.png

七、Fanout 交换器( 广播)

f1946cb78ee852e67760197ca47629ad.png

af9716d9642ac273c43d9b99981dde21.png

1.搭建环境

创建项目

55a53a99e7901aa5b07e29620d6b1c54.png

2.修改全局配置文件

Consumer

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

#设置交换器的名称
mq.config.exchange=order.fanout

#短信服务队列的名称
mq.config.queue.sms=order.sms

#push服务队列名称
mq.config.queue.push=order.push

Provider

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.226.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111

#设置交换器的名称
mq.config.exchange=order.fanout

2.编写代码

消息接收者

SmsReceiver

package com.sxt.rabbitmqfanoutconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener bindings:绑定队列
 *
 * @QueueBinding
 * 				value:绑定队列的名称
 *              exchange:配置交换器
 *              key:路由键值
 *
 * @Queue
 * 		 value:配置队列名称
 *       autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange
 * 			value:为交换器起个名称
 *          type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.sms}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.FANOUT)
        )
)
public class SmsReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("Sms................receiver:"+msg);
    }
}

PushReceiver

package com.sxt.rabbitmqfanoutconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener bindings:绑定队列
 *
 * @QueueBinding
 * 				value:绑定队列的名称
 *              exchange:配置交换器
 *              key:路由键值
 *
 * @Queue
 * 		 value:配置队列名称
 *       autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange
 * 			value:为交换器起个名称
 *          type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.push}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.FANOUT)
        )
)
public class PushReceiver {
    /**
     * 接收信息的方法。采用消息队列监听机制
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("Push................receiver:"+msg);
    }
}

消息消费者

/**
 * 消息发送者
 */
@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    @Value("${mq.config.exchange}")
    private String exchange;

    /**
     * 发送消息的方法
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数一:交换器的名称。
        //参数二:路由键
        //参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,"", msg);
    }
}

测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitmqFanoutProviderApplication.class)
public class RabbitmqFanoutProviderApplicationTests {

    @Autowired
    private Sender sender;

    @Test
    public void test1() {
            this.sender.send("Hello RabbitMQ");
    }

}

8540d6f1debfffb7f137315d619c7e47.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值