SpringBoot集成RabbitMQ 五种工作模式简单使用

SpringBoot-RabbitMQ

一、开启rabbitMq

启动:service rabbitmq-server start

关闭:service rabbitmq-server stop

重启:service rabbitmq-server restart

rabbitmq-可视化界面:http://ip:15672

:guest 账号只能在localhost 登录,倘若安装成功但进不去页面看看是不是虚拟机端口未开放

开放15672端口:firewall-cmd --zone=public --add-port=15672/tcp --permanent

关闭15672端口:firewall-cmd --zone=public --remove-port=15672/tcp --permanent

配置立即生效 :firewall-cmd --reload

执行关闭防火墙命令: systemctl stop firewalld.service

执行开启防火墙命令:systemctl start firewalld.service

在这里插入图片描述

二、环境搭建

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rabbitmq</groupId>
    <artifactId>springboot-rabbitmq</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
    </parent>

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

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

        <!--springboot集成了单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

</project>

application.yml

spring:
  application:
    name: springboot-rabbitmq
   #配置rabbitMq 服务器
  rabbitmq:
    host: 192.168.128.244
    port: 5672 # AMQP协议端口
    username: admin
    password: admin
    virtual-host: /admin-host

RabbitTemplate : 用来简化操作, 使用时直接注入

三、五种工作模式


本文只测试前五种

1、hello word

模式简介

img

一个生产者对应一个消费者!!!

消息生产者

@SpringBootTest(classes = SpringBootStart.class)
@RunWith(SpringRunner.class)
public class SpringBootStartTest {

    @Resource
    RabbitTemplate rabbitTemplate;

    @Test
    public void helloWord(){
        rabbitTemplate.convertAndSend("hello","hello rabbitmq");
    }
}

消息消费者

package com.application.consumer;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component
@RabbitListener(queuesToDeclare = 
  // value 消息队列名字, durable 队列是否持久化, exclusive 队列是否独占,autoDelete 是否自动删除
                @Queue(value="hello",durable="false",exclusive="false",autoDelete="true")
               )
public class HelloConsumer {

    @RabbitHandler
    public void receive(String message){
        System.out.println(message);
    }
}

测试结果:

在这里插入图片描述

2、Work (工作模式)

模式简介

在这里插入图片描述

一个生产者对应多个消费者,但是一条消息只能有一个消费者获得消息!!!

  • 公平模式

    生产者:

    //work
    @Test
    public void work(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","work message"+ i );
        }
    }
    

    消费者:

    @Component
    public class WorkConsumer {
    
        @RabbitListener(queuesToDeclare = @Queue("work"))
        public void receive(String message){
            System.out.println("work1 = "+message);
        }
    
        @RabbitListener(queuesToDeclare = @Queue("work"))
        public void receive2(String message){
            System.out.println("work2 = "+message);
        }
    }
    

    测试结果:

    在这里插入图片描述

3、 Publish(广播模式)

模式简介:

img

一个消费者将消息首先发送到交换器,交换器绑定到多个队列,然后被监听该队列的消费者所接收并消费。

ps:X表示交换器,在RabbitMQ中,交换器主要有四种类型:direct、fanout、topic、headers,这里的交换器是 fanout。

消息生产者:

//fanout
@Test
public void fanout(){
    rabbitTemplate.convertAndSend("logs","","work message");
}

消息消费者:

@Component
public class FanoutConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(exchange = @Exchange(value = "logs",type = "fanout"),//绑定交换机 类型为fanout
                    value=@Queue // 创建临时队列
            )
    })
    public void receive(String message){
        System.out.println("receive1 =" +message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(exchange = @Exchange(value = "logs",type = "fanout"),//绑定交换机 类型为fanout
                    value=@Queue // 创建临时队列
            )
    })
    public void receive2(String message){
        System.out.println("receive2 =" +message);
    }
}

结果:

在这里插入图片描述

4 route(路由模式)

模式简介:

img

生产者将消息发送到direct交换器,在绑定队列和交换器的时候有一个路由key,生产者发送的消息会指定一个路由key,那么消息只会发送到相应key相同的队列,接着监听该队列的消费者消费消息。

也就是让消费者有选择性的接收消息。

消息生产者

//route
@Test
public void route(){
    rabbitTemplate.convertAndSend("route","info","info message");
    rabbitTemplate.convertAndSend("route","error","error message");
}

消息消费者

@Component
public class RouteConsumer {
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "route",type = "direct"),
                    key = {"info","error"}
            )
    )
    public void receive(String mes){
        System.out.println("receive1 = " + mes);
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "route",type = "direct"),
                    key = {"error"}
            )
    )
    public void receive1(String mes){
        System.out.println("receive2 = " + mes);
    }
}
5、Topics(主题模式)

模式简介:

topic

上面的路由模式是根据路由key进行完整的匹配(完全相等才发送消息),这里的通配符模式通俗的来讲就是模糊匹配。

通配符作用
*一次匹配一个单词
#一次匹配多个单词
.单词分割

消息生产者:

//Topics
@Test
public void topics(){
    rabbitTemplate.convertAndSend("Topics","user.he.info","user.he.info");
    rabbitTemplate.convertAndSend("Topics","user.hell.zz.info","user.hell.zz.info");
}

消息消费者:

@Component
public class TopicConsumer {

    @RabbitListener( bindings = @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "Topics", type = "topic"),
                    key = {"user.hell.info"}
            )
    )
    public void receive1(String message){
        System.out.println("receive 1 = " + message);
    }
    @RabbitListener( bindings = @QueueBinding(
            value = @Queue, //创建临时队列
            exchange = @Exchange(value = "Topics", type = "topic"),
            key = {"user.*.info"}
        )
    )
    public void receive2(String message){
        System.out.println("receive 2 = " + message);
    }


    @RabbitListener( bindings = @QueueBinding(
            value = @Queue, //创建临时队列
            exchange = @Exchange(value = "Topics", type = "topic"),
            key = {"user.#.info"}
    )
    )
    public void receive3(String message){
        System.out.println("receive 3 = " + message);
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值