最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合消息隊(duì)列RabbitMQ

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

簡(jiǎn)介

在Spring項(xiàng)目中,可以使用Spring-Rabbit去操作RabbitMQ

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

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

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

生產(chǎn)者工程:

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

消費(fèi)者工程:

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

生產(chǎn)端

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

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

添加依賴

修改pom.xml文件內(nèi)容為如下:

<?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>

啟動(dòng)類

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,內(nèi)容如下:

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

綁定交換機(jī)和隊(duì)列

創(chuàng)建RabbitMQ隊(duì)列與交換機(jī)綁定的配置類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";
    // 交換機(jī)
    @Bean("bootExchange")
    public Exchange bootExchange(){
        // 構(gòu)建交換機(jī)對(duì)象
        return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build();
    }
    //Queue 隊(duì)列
    @Bean("bootQueue")
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }
    //隊(duì)列和交換機(jī)的關(guān)系 Binding
    /**
     * 1 知道那個(gè)隊(duì)列
     * 2 知道那個(gè)交換機(jī)
     * 3 routingKey
     */
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
}

搭建消費(fèi)者工程

創(chuàng)建工程

生產(chǎn)端

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

2. 引入start,依賴坐標(biāo)

org.springframework.boot

spring-boot-starter-amqp

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

添加依賴

修改pom.xml文件內(nèi)容為如下:

<?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>

啟動(dòng)類

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,內(nèi)容如下:

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)聽某個(gè)隊(duì)列的消息
     * @param message 接收到的消息
     */
    @RabbitListener(queues = "item_queue")
    public void myListener1(String message){
        System.out.println("消費(fèi)者接收到的消息為:" + message);
    }
}

測(cè)試

在生產(chǎn)者工程springboot-rabbitmq-producer中創(chuàng)建測(cè)試類,發(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");
    }
}

先運(yùn)行上述測(cè)試程序(交換機(jī)和隊(duì)列才能先被聲明和綁定),然后啟動(dòng)消費(fèi)者;在消費(fèi)者工程springboot-rabbitmq-consumer中控制臺(tái)查看是否接收到對(duì)應(yīng)消息。

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

基本信息再yml中配置,隊(duì)列交互機(jī)以及綁定關(guān)系在配置類中使用Bean的方式配置

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

消費(fèi)端直接使用@RabbitListener完成消息接收

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

相關(guān)文章

最新評(píng)論

柞水县| 安吉县| 平武县| 太仆寺旗| 麻阳| 方山县| 民和| 乃东县| 红原县| 梁山县| 芜湖县| 长泰县| 平阴县| 砚山县| 城固县| 历史| 恩施市| 兴化市| 汉川市| 宣武区| 七台河市| 益阳市| 伊吾县| 正蓝旗| 元氏县| 宁德市| 鹤山市| 梁平县| 台州市| 临漳县| 特克斯县| 越西县| 永兴县| 黄浦区| 大洼县| 静乐县| 象州县| 瑞丽市| 白水县| 开鲁县| 平阳县|