亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot整合消息隊列RabbitMQ

 更新時間:2023年03月20日 08:47:30   作者:小乞丐程序員  
SpringBoot整合RabbitMQ很容易,但是整合的目的是為了使用,那要使用RabbitMQ就要對其有一定的了解,不然容易整成一團漿糊。因為說到底,SpringBoot只是在封裝RabbitMQ的API,讓其更容易使用而已,廢話不多說,讓我們一起整它

簡介

在Spring項目中,可以使用Spring-Rabbit去操作RabbitMQ

https://github.com/spring-projects/spring-amqp

尤其是在spring boot項目中只需要引入對應的amqp啟動器依賴即可,方便的使用RabbitTemplate發(fā)送消息,使用注解接收消息。

一般在開發(fā)過程中:

生產者工程:

  • application.yml文件配置RabbitMQ相關信息;
  • 在生產者工程中編寫配置類,用于創(chuàng)建交換機和隊列,并進行綁定
  • 注入RabbitTemplate對象,通過RabbitTemplate對象發(fā)送消息到交換機

消費者工程:

  • application.yml文件配置RabbitMQ相關信息
  • 創(chuàng)建消息處理類,用于接收隊列中的消息并進行處理

生產端

1. 創(chuàng)建生產者SpringBoot工程(maven)
2. 引入start,依賴坐標
<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
     </dependency>

3. 編寫yml配置,基本信息配置
4. 定義交換機,隊列以及綁定關系的配置類
5. 注入RabbitTemplate,調用方法,完成消息發(fā)送

添加依賴

修改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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springboot-rabbitmq-producer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <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>

啟動類

package com.itheima.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);
    }
}

配置RabbitMQ

配置文件

創(chuàng)建application.yml,內容如下:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /itcast
    username: heima
    password: heima

綁定交換機和隊列

創(chuàng)建RabbitMQ隊列與交換機綁定的配置類com.itheima.rabbitmq.config.RabbitMQConfig

package com.itheima.rahhitmq.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 RabbitMQConfig {
    public static final String EXCHAGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";
    // 交換機
    @Bean("bootExchange")
    public Exchange bootExchange(){
        // 構建交換機對象
        return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build();
    }
    //Queue 隊列
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
    //隊列和交換機的關系 Binding
    /**
     * 1 知道那個隊列
     * 2 知道那個交換機
     * 3 routingKey
     */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}

搭建消費者工程

創(chuàng)建工程

生產端

1. 創(chuàng)建生產者SpringBoot工程

2. 引入start,依賴坐標

org.springframework.boot

spring-boot-starter-amqp

編寫yml配置,基本信息配置
定義交換機,隊列以及綁定關系的配置類
注入RabbitTemplate,調用方法,完成消息發(fā)送

添加依賴

修改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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springboot-rabbitmq-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>
</project>

啟動類

package com.itheima.rabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }
}

配置RabbitMQ

創(chuàng)建application.yml,內容如下:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    virtual-host: /itcast
    username: heima
    password: heima

消息監(jiān)聽處理類

編寫消息監(jiān)聽器com.itheima.rabbitmq.listener.MyListener

package com.itheima.rabbitmq.listener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListener {
    /**
     * 監(jiān)聽某個隊列的消息
     * @param message 接收到的消息
     */
    @RabbitListener(queues = "item_queue")
    public void myListener1(String message){
        System.out.println("消費者接收到的消息為:" + message);
    }
}

測試

在生產者工程springboot-rabbitmq-producer中創(chuàng)建測試類,發(fā)送消息:

package com.itheima.rabbitmq;
import com.itheima.rabbitmq.config.RabbitMQConfig;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void test(){
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.insert", "商品新增,routing key 為item.insert");
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.update", "商品修改,routing key 為item.update");
        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.delete", "商品刪除,routing key 為item.delete");
    }
}

先運行上述測試程序(交換機和隊列才能先被聲明和綁定),然后啟動消費者;在消費者工程springboot-rabbitmq-consumer中控制臺查看是否接收到對應消息。

SpringBoot提供了快速整合RabbitMQ的方式

基本信息再yml中配置,隊列交互機以及綁定關系在配置類中使用Bean的方式配置

生產端直接注入RabbitTemplate完成消息發(fā)送

消費端直接使用@RabbitListener完成消息接收

到此這篇關于SpringBoot整合消息隊列RabbitMQ的文章就介紹到這了,更多相關SpringBoot整合RabbitMQ內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java字符串替換函數replace()用法解析

    Java字符串替換函數replace()用法解析

    這篇文章主要介紹了Java字符串替換函數replace()用法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • Java 守護線程_動力節(jié)點Java學院整理

    Java 守護線程_動力節(jié)點Java學院整理

    Java語言機制是構建在JVM的基礎之上的,意思是Java平臺把操作系統(tǒng)的底層給屏蔽起來,所以它可以在它自己的虛擬的平臺里面構造出對自己有利的機制,而語言或者說平臺的設計者多多少少是收到Unix思想的影響,而守護線程機制又是對JVM這樣的平臺湊合,于是守護線程應運而生
    2017-05-05
  • 淺談Springboot下引入mybatis遇到的坑點

    淺談Springboot下引入mybatis遇到的坑點

    這篇文章主要介紹了Springboot下引入mybatis遇到的坑點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 相冊管理系統(tǒng)(Java表單+xml數據庫存儲)

    相冊管理系統(tǒng)(Java表單+xml數據庫存儲)

    這篇文章主要為大家詳細介紹了相冊管理系統(tǒng)的實現(xiàn)步驟,Java表單的文件上傳和下載,xml數據庫存儲信息,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 解析如何用兩個棧來實現(xiàn)隊列的方法

    解析如何用兩個棧來實現(xiàn)隊列的方法

    本篇文章是對如何用兩個棧實現(xiàn)隊列的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Java 通過API操作GraphQL

    Java 通過API操作GraphQL

    這篇文章主要介紹了Java 通過API操作GraphQL的方法,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下
    2021-05-05
  • Lucene?索引刪除策略源碼解析

    Lucene?索引刪除策略源碼解析

    這篇文章主要為大家介紹了Lucene?索引刪除策略源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Java監(jiān)聽POST請求的示例詳解

    Java監(jiān)聽POST請求的示例詳解

    要監(jiān)聽POST請求,我們可以使用Java中的HttpServlet類,以下是一個使用Servlet API監(jiān)聽POST請求的完整示例,通過代碼示例講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-12-12
  • SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog的詳細過程

    SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog的詳細過程

    分布式追蹤系統(tǒng)是一個最終的解決方案,如果您的公司已經上了分布式追蹤系統(tǒng),這篇文章主要介紹了SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog,需要的朋友可以參考下
    2022-10-10
  • SpringAop切入點execution表達式的深入講解

    SpringAop切入點execution表達式的深入講解

    Spring AOP 可能會經常使用 execution切入點指示符,下面這篇文章主要給大家介紹了關于SpringAop切入點execution表達式的相關資料,需要的朋友可以參考下
    2021-08-08

最新評論