《RabbitMQ系列教程-第六章-SpringBoot整合RabbitMQ》_rabbitmessagingtemplate rabbittemplate



第六章 SpringBoot整合RabbitMQ

6.1 搭建消息生产者

6.1.1 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.lscl</groupId>
    <artifactId>05_springboot_rabbitmq_producer</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <!-- springboot 工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- rabbitmq场景启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!-- 测试环境场景启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>

6.1.2 application.yml:
spring:
  rabbitmq:
    host: 192.168.40.139
    port: 5672
    username: lscl
    password: admin
    virtual-host: /lscl

6.1.3 引导类:
package com.lscl.rabbitmq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class);
    }
}

6.1.4 配置消息队列:
6.1.4.1 Work模式:

SpringBoot中提供有两种构建Queue的方式,一种是通过new Queue()的方式,另一种采用QueueBuilder构建器构建一个Queue

package com.lscl.rabbitmq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config\_01\_Work {

    /\*\*
 \* 定义一个队列
 \* @return
 \*/
    @Bean("bootWorkQueue")
    public Queue bootWorkQueue() {
        /\*
 第一种方式:
 durable():代表需要持久化
 exclusive(): 代表该队列独占(只允许有一个consumer监听)
 autoDelete(): 代表需要自动删除(没有consumer自动删除)
 withArgument(): 队列的其他参数

 \*/
// return QueueBuilder.durable("boot\_work\_queue").exclusive().autoDelete().withArgument("key", "val").build();
        /\*
 第二种方式:
 通过new Queue对象来创建队列
 参数1: 队列名称
 参数2: 是否持久化(默认:true)
 参数3: 是否独占(默认:false)
 参数4: 是否自动删除(默认:false)
 参数5: 队列的其他参数
 \*/
        return new Queue("boot\_work\_queue", true, false, false,null);
    }
}

6.1.4.2 Pub/Sub模式:
package com.lscl.rabbitmq.config;

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

/\*\*
 \* Pub/Sub模式(交换机类型为Fanout)
 \*/

@Configuration
public class Config\_03\_Fanout {

    @Bean("bootFanoutQueue1")
    public Queue bootFanoutQueue1(){
        return QueueBuilder.durable("boot\_fanout\_queue1").build();
    }

    @Bean("bootFanoutQueue2")
    public Queue bootFanoutQueue2(){
        return QueueBuilder.durable("boot\_fanout\_queue2").build();
    }

    // fanout类型交换机
    @Bean("bootFanoutExchange")
    public Exchange bootFanoutExchange(){
        return ExchangeBuilder.fanoutExchange("boot\_fanout\_exchange").durable(true).build();
    }

    /\*\*
 \* 交换机与队列进行绑定
 \*/
    @Bean
    public Binding bindFanout1(){

        Queue bootFanoutQueue1 = bootFanoutQueue1();
        Exchange bootFanoutExchange = bootFanoutExchange();

        // fanout类型交换机routing key为 ""
        return BindingBuilder.bind(bootFanoutQueue1).to(bootFanoutExchange).with("").noargs();
    }

    /\*\*
 \* 交换机与队列进行绑定
 \* @return
 \*/
    @Bean
    public Binding bindFanout2(){
        Queue bootFanoutQueue2 = bootFanoutQueue2();
        Exchange bootFanoutExchange = bootFanoutExchange();

        return BindingBuilder.bind(bootFanoutQueue2).to(bootFanoutExchange).with("").noargs();
    }

}

6.1.4.3 Routing模式:
package com.lscl.rabbitmq.config;

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

/\*\*
 \* routing模式(交换机类型为Direct)
 \*/
@Configuration
public class Config\_02\_Direct {

    /\*\*
 \* 准备两个队列boot\_direct\_queue1、boot\_direct\_queue2
 \* @return
 \*/
    @Bean("bootDirectQueue1")
    public Queue bootDirectQueue1(){
        return QueueBuilder.durable("boot\_direct\_queue1").build();
    }

    @Bean("bootDirectQueue2")
    public Queue bootDirectQueue2(){
        return QueueBuilder.durable("boot\_direct\_queue2").build();
    }

    // direct类型交换机
    @Bean("bootDirectExchange")
    public Exchange bootDirectExchange(){
        /\*
 第一种方式: 通过ExchangeBuilder构建交换机
 durable: 是否持久化
 autoDelete: 是否自动删除
 withArgument: 交换机其他参数
 \*/
// return ExchangeBuilder.directExchange("boot\_direct\_exchange").durable(true).autoDelete().withArgument("key","val").build();

        /\*
 第二种方式:
 参数1: 是否持久化(默认false)
 参数2: 是否自动删除(默认false)
 参数3: 其他参数
 \*/
        return new DirectExchange("boot\_direct\_exchange",true,false,null);
    }

    /\*\*
 \* 交换机与队列进行绑定
 \*/
    @Bean
    public Binding bindDirect1(){

        Queue bootDirectQueue1 = bootDirectQueue1();
        Exchange bootDirectExchange = bootDirectExchange();


        /\*
 第一种方式:
 bind(Queue): 需要绑定的queue
 to(Exchange): 需要绑定到哪个交换机
 with(String): routing key
 noargs(): 进行构建
 \*/
// return BindingBuilder.bind(bootDirectQueue1).to(bootDirectExchange).with("article").noargs();

        /\*
 第一种方式:
 参数1: 绑定的队列
 参数2: 绑定的类型 Binding.DestinationType.QUEUE: 绑定的类型为queue(交换机不仅可以绑定queue还可以绑定exchange)
 参数3: 哪个交换机需要绑定
 参数4: routing key
 参数5: 其他参数
 \*/
         return new Binding("boot\_direct\_queue1", Binding.DestinationType.QUEUE,"boot\_direct\_exchange","red",null);
    }

    /\*\*
 \* 交换机与队列进行绑定
 \* @return
 \*/
    @Bean
    public Binding bindDirect2(){
        Queue bootDirectQueue2 = bootDirectQueue2();
        Exchange bootDirectExchange = bootDirectExchange();

        return BindingBuilder.bind(bootDirectQueue2).to(bootDirectExchange).with("green").noargs();
    }
}

6.1.4.4 Topics模式
package com.lscl.rabbitmq.config;

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

/\*\*
 \* Topics模式(交换机类型为Topic)
 \*/
@Configuration
public class Config\_04\_Topic {

    @Bean("bootTopicQueue1")
    public Queue bootTopicQueue1(){
        return QueueBuilder.durable("boot\_topic\_queue1").build();
    }

    @Bean("bootTopicQueue2")
    public Queue bootTopicQueue2(){
        return QueueBuilder.durable("boot\_topic\_queue2").build();
    }

    // topic类型交换机
    @Bean("bootTopicExchange")
    public Exchange bootTopicExchange(){

        return ExchangeBuilder.topicExchange("boot\_topic\_exchange").durable(true).build();
    }

    /\*\*
 \* 交换机与队列进行绑定
 \*/
    @Bean
    public Binding bindTopic1(){

        Queue bootTopicQueue1 = bootTopicQueue1();
        Exchange bootTopicExchange = bootTopicExchange();

        return BindingBuilder.bind(bootTopicQueue1).to(bootTopicExchange).with("red.#").noargs();
    }

    /\*\*
 \* 交换机与队列进行绑定
 \* @return
 \*/
    @Bean
    public Binding bindTopic2(){
        Queue bootTopicQueue2 = bootTopicQueue2();
        Exchange bootTopicExchange = bootTopicExchange();
        return BindingBuilder.bind(bootTopicQueue2).to(bootTopicExchange).with("green.\*").noargs();
    }
}

6.1.5 测试类:
package com.lscl.rabbitmq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = ProducerApplication.class)
@RunWith(SpringRunner.class)
public class ProducerTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /\*\*
 \* work 模式
 \* @throws Exception
 \*/
    @Test
    public void testWork() throws Exception{
        rabbitTemplate.convertAndSend("boot\_work\_queue","boot work...");
    }

    /\*\*
 \* Routing 模式(交换机类型为Direct)


## 最后

分享一套我整理的面试干货,这份文档结合了我多年的面试官经验,站在面试官的角度来告诉你,面试官提的那些问题他最想听到你给他的回答是什么,分享出来帮助那些对前途感到迷茫的朋友。

#### 面试经验技巧篇

* 经验技巧1 如何巧妙地回答面试官的问题
* 经验技巧2 如何回答技术性的问题
* 经验技巧3 如何回答非技术性问题
* 经验技巧4 如何回答快速估算类问题
* 经验技巧5 如何回答算法设计问题
* 经验技巧6 如何回答系统设计题
* 经验技巧7 如何解决求职中的时间冲突问题
* 经验技巧8 如果面试问题曾经遇见过,是否要告知面试官
* 经验技巧9 在被企业拒绝后是否可以再申请
* 经验技巧10 如何应对自己不会回答的问题
* 经验技巧11 如何应对面试官的“激将法”语言
* 经验技巧12 如何处理与面试官持不同观点这个问题
* 经验技巧13 什么是职场暗语

![](https://img-blog.csdnimg.cn/img_convert/ed49e196c96c402fb80dc11f6fab82ee.webp?x-oss-process=image/format,png)

#### 面试真题篇

* 真题详解1 某知名互联网下载服务提供商软件工程师笔试题
* 真题详解2 某知名社交平台软件工程师笔试题
* 真题详解3 某知名安全软件服务提供商软件工程师笔试题
* 真题详解4 某知名互联网金融企业软件工程师笔试题
* 真题详解5 某知名搜索引擎提供商软件工程师笔试题
* 真题详解6 某初创公司软件工程师笔试题
* 真题详解7 某知名游戏软件开发公司软件工程师笔试题
* 真题详解8 某知名电子商务公司软件工程师笔试题
* 真题详解9 某顶级生活消费类网站软件工程师笔试题
* 真题详解10 某知名门户网站软件工程师笔试题
* 真题详解11 某知名互联网金融企业软件工程师笔试题
* 真题详解12 国内某知名网络设备提供商软件工程师笔试题
* 真题详解13 国内某顶级手机制造商软件工程师笔试题
* 真题详解14 某顶级大数据综合服务提供商软件工程师笔试题
* 真题详解15 某著名社交类上市公司软件工程师笔试题
* 真题详解16 某知名互联网公司软件工程师笔试题
* 真题详解17 某知名网络安全公司校园招聘技术类笔试题
* 真题详解18 某知名互联网游戏公司校园招聘运维开发岗笔试题

![](https://img-blog.csdnimg.cn/img_convert/419d59de450f9da8453c18391f1dbdb9.webp?x-oss-process=image/format,png)

资料整理不易,点个关注再走吧

笔试题
* 真题详解6 某初创公司软件工程师笔试题
* 真题详解7 某知名游戏软件开发公司软件工程师笔试题
* 真题详解8 某知名电子商务公司软件工程师笔试题
* 真题详解9 某顶级生活消费类网站软件工程师笔试题
* 真题详解10 某知名门户网站软件工程师笔试题
* 真题详解11 某知名互联网金融企业软件工程师笔试题
* 真题详解12 国内某知名网络设备提供商软件工程师笔试题
* 真题详解13 国内某顶级手机制造商软件工程师笔试题
* 真题详解14 某顶级大数据综合服务提供商软件工程师笔试题
* 真题详解15 某著名社交类上市公司软件工程师笔试题
* 真题详解16 某知名互联网公司软件工程师笔试题
* 真题详解17 某知名网络安全公司校园招聘技术类笔试题
* 真题详解18 某知名互联网游戏公司校园招聘运维开发岗笔试题

[外链图片转存中...(img-803ch6WM-1714486471901)]

资料整理不易,点个关注再走吧

> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/topics/618154847)收录**
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值