RabbitMQ的Boot/Cloud使用方式

蚊子 2023年03月24日 424次浏览

前言

看此文章前,请先观看:
配置RabbitMQ环境:https://www.0po.cn/archives/36
原生JAVA使用(可稍微看看):https://www.0po.cn/archives/37

因为Boot/Cloud系统内置整合了RabbitMQ,所以使用方法,超级简单


1.准备工作

依赖

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

配置文件

application.yml

spring:
  rabbitmq:
    host: 127.0.0.1
    username: guest
    password: guest
    virtual-host: /
    port: 5672

2.开始

1.编写使用的交换机等信息

MqConfig类

package com.zb.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {

    //交换机名字
    public static final String TestExchange = "TestExchange";
    //队列名字
    public static final String TestQueue = "TestQueue";
    //绑定一起的名字
    public static final String TestRouting = "TestRouting";


    //bean名字,可随意,这里懒省事,直接用交换机名字
    @Bean(TestExchange)
    public Exchange createPayExchange() {
        //定义top(匹配/包含)交换机方式
        return ExchangeBuilder.topicExchange(TestExchange).durable(true).build();
    }


    //bean名字,可随意,这里懒省事,直接用队列名字
    @Bean(TestQueue)
    public Queue createPayQueue() {
        //定义队列信息
        //参1:起个名字
        //参2:是否持久化(true=不会丢失,数据放Erlang自带的Mnesia数据库,false=数据放内存,重启RabbitMQ数据丢置)正常业务设置:true
        //参3:是否排外的(true=仅第一个链接的通道使用,断开候会删除,false=所有通道都可以用)正常业务设置:false
        //参4:是否自动删除(true=当接受与队列断开后,会自动删除,false=不删除)正常业务设置:false
        //参5:设置消息的参数(x-rnessage-ttl等,基本用不上)正常业务设置:null
        return new Queue(TestQueue, true, false, false, null);
    }


    //bean名字,可随意,这里懒省事,直接用绑定一起的名字
    //--------------------------------------------
    //参数:注解使用@Qualifier,将上面2个方法注入到形参
    @Bean(TestRouting)
    public Binding bindingPayQueueToExchange(@Qualifier(TestExchange) Exchange exchange, @Qualifier(TestQueue) Queue queue) {
        //启动交换机/绑定参数
        //queue:绑定那个队列
        //exchange:使用那个交换机
        //TestRouting:起个名字,这里使用16行定义的变量
        return BindingBuilder.bind(queue).to(exchange).with(TestRouting).noargs();
    }


}

2.发送端

FaSongController类

package com.zb.pay;

import com.zb.config.MqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FaSongController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void faso() {
        //定义要发送的东西
        String name = "你好,世界";

        //参1:使用那个交换机
        //参2:去那个绑定好的名字里面寻找信息
        //参3:要发送什么信息
        rabbitTemplate.convertAndSend(MqConfig.TestExchange, MqConfig.TestRouting, name);
        System.out.println("发送完毕");
    }
}

3.接受端

JieShouService类

package com.zb.listener;

import com.zb.config.MqConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class JieShouService {

    //@RabbitListener:告诉我你要监听那个队列的名字
    @RabbitListener(queues = MqConfig.TestQueue)
    //msg:负责接收发过来的信息
    public void jieshou(String msg){
        System.out.println(msg);
    }

}

4.启动类

package com.zb;

import com.zb.pay.FaSongController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Test {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(Test.class, args);
        FaSongController bean = run.getBean(FaSongController.class);
        bean.faso();
    }
}


5.目录&效果图

目录:
image-1679668578895

效果:
image-1679668595257