欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

rabbitmq使用springboot實(shí)現(xiàn)direct模式(最新推薦)

 更新時(shí)間:2023年07月10日 10:26:31   作者:p&f°  
這篇文章主要介紹了rabbitmq使用springboot實(shí)現(xiàn)direct模式,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、 Direct模式

  • 類型:direct
  • 特點(diǎn):Direct模式是fanout模式上的一種疊加,增加了路由RoutingKey的模式。

二、coding

Ⅰ 生產(chǎn)者 

1、引入相應(yīng)的pom文件 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 https://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.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xpf</groupId>
    <artifactId>rabbitmq-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rabbitmq-springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

2、配置文件 application.properties

server.port=8080
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/
spring.rabbitmq.host=192.168.199.20
spring.rabbitmq.port=5672

3、寫一個(gè)生產(chǎn)者 DirectOrderService.java

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class DirectOrderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 模擬用戶下單,發(fā)送消息給下游系統(tǒng)
     * @param user
     * @param num
     */
    public void makerOrder(String user,  int num){
        //1、查詢庫(kù)存是否有剩余
        //2、保存訂單
        String orderId = UUID.randomUUID().toString();
        System.out.println("訂單生產(chǎn)成功:" + orderId);
        //3、通過(guò)mq給下游系統(tǒng)發(fā)送消息
        String exchangeName = "direct_order_exchange";
        rabbitTemplate.convertAndSend(exchangeName, "sms", orderId);
        rabbitTemplate.convertAndSend(exchangeName, "email", orderId);
        System.out.println("完成");
    }
}

(從代碼中可以看到,direct_order_exchange交換機(jī)分別給綁定的路由key為sms和email的消息隊(duì)列發(fā)送了消息)

4、寫一個(gè)測(cè)試類,發(fā)送消息

import com.xpf.rabbitmqspringboot.service.DirectOrderService;
import com.xpf.rabbitmqspringboot.service.FanoutOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitmqSpringbootApplicationTests {
    @Autowired
    private FanoutOrderService fanoutOrderService;
    @Autowired
    private DirectOrderService directOrderService;
    /**
     * Direct模式生產(chǎn)者發(fā)送消息
     */
    @Test
    public void setDirectOrderService(){
        directOrderService.makerOrder("用戶2", 10);
    }
}

(先別啟動(dòng)測(cè)試類,因?yàn)榻粨Q機(jī)和隊(duì)列的聲明放在下面的消費(fèi)者中。) 

 Ⅱ 消費(fèi)者

1、新建一個(gè)springboot項(xiàng)目,其中pom.xml 和 application.properties和上述生產(chǎn)者文件相同,但是如果在一個(gè)電腦模擬同啟動(dòng)兩個(gè)項(xiàng)目時(shí),記得把a(bǔ)pplication.properties中的端口換成不同的

2、使用springboot寫一個(gè)配置文件 RabbitMqConfiguration.java

關(guān)于為啥在消費(fèi)者中建配置文件而不是在生產(chǎn)者,請(qǐng)看rabbitmq使用springboot實(shí)現(xiàn)fanout模式

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfiguration {
    //1、聲明注冊(cè)Direct模式交換機(jī)
    @Bean
    public DirectExchange DirectExchange(){
        return new DirectExchange("direct_order_exchange", true, false);
    }
    //2、聲明隊(duì)列 sms.Direct.queue、email.Direct.queue、duanxin.Direct.queue
    @Bean
    public Queue smsQueue(){
        return new Queue("sms.direct.queue", true);
    }
    @Bean
    public Queue emailQueue(){
        return new Queue("email.direct.queue", true);
    }
    @Bean
    public Queue duanxinQueue(){
        return new Queue("duanxin.direct.queue", true);
    }
    //3、完成綁定關(guān)系(隊(duì)列綁定交換機(jī))
    @Bean
    public Binding smsBinding(){
        return BindingBuilder.bind(smsQueue()).to(DirectExchange()).with("sms");
    }
    @Bean
    public Binding emailBinding(){
        return BindingBuilder.bind(emailQueue()).to(DirectExchange()).with("email");
    }
    @Bean
    public Binding duanxinBinding(){
        return BindingBuilder.bind(duanxinQueue()).to(DirectExchange()).with("duanxin");
    }
}

3、寫三個(gè)消費(fèi)者分別監(jiān)聽(tīng)路由key為sms、email、duanxin的消息隊(duì)列

(這里舉例兩個(gè) SmsDirectConsumer.java 和 EmailDirectConsumer.java)

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
 * @Author xpf
 * @Date 2023/7/9 1:27
 * @Version 1.0
 */
@Component
@RabbitListener(queues = "sms.direct.queue")
public class SmsDirectConsumer {
    @RabbitHandler
    public void receiveMessage(String message){
        System.out.println("接收到來(lái)自隊(duì)列sms.direct.queue消息訂單的message是:" + message);
    }
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
 * @Author xpf
 * @Date 2023/7/9 1:27
 * @Version 1.0
 */
@Component
@RabbitListener(queues = "email.direct.queue")
public class EmailDirectConsumer {
    @RabbitHandler
    public void receiveMessage(String message){
        System.out.println("接收到來(lái)自隊(duì)列email.direct.queue消息訂單的message是:" + message);
    }
}

三、測(cè)試

1、先啟動(dòng)消費(fèi)者,因?yàn)楸卷?xiàng)目配置類在消費(fèi)者

2、啟動(dòng)生產(chǎn)者測(cè)試類

結(jié)果發(fā)現(xiàn)路由key為sms、email的消息隊(duì)列接收到了生產(chǎn)者發(fā)送的消息,而duanxin沒(méi)有收到,結(jié)果符合預(yù)期

到此這篇關(guān)于rabbitmq使用springboot實(shí)現(xiàn)direct模式的文章就介紹到這了,更多相關(guān)springboot實(shí)現(xiàn)direct模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot?通過(guò)FastJson實(shí)現(xiàn)bean對(duì)象和Json字符串互轉(zhuǎn)問(wèn)題

    Springboot?通過(guò)FastJson實(shí)現(xiàn)bean對(duì)象和Json字符串互轉(zhuǎn)問(wèn)題

    這篇文章主要介紹了Springboot?通過(guò)FastJson實(shí)現(xiàn)bean對(duì)象和Json字符串互轉(zhuǎn),本文嘗試驗(yàn)證兩種場(chǎng)景給大家詳細(xì)介紹,對(duì)Springboot?FastJson實(shí)現(xiàn)bean和Json互轉(zhuǎn)問(wèn)題,感興趣的朋友一起看看吧
    2022-08-08
  • BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析

    BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析

    這篇文章主要介紹了BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 完美解決在Servlet中出現(xiàn)一個(gè)輸出中文亂碼的問(wèn)題

    完美解決在Servlet中出現(xiàn)一個(gè)輸出中文亂碼的問(wèn)題

    下面小編就為大家?guī)?lái)一篇完美解決在Servlet中出現(xiàn)一個(gè)輸出中文亂碼的問(wèn)題。小編覺(jué)得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Java中使用websocket實(shí)現(xiàn)在線聊天功能

    Java中使用websocket實(shí)現(xiàn)在線聊天功能

    這篇文章主要介紹了Java中使用websocket實(shí)現(xiàn)在線聊天功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 手把手教你用Java實(shí)現(xiàn)一套簡(jiǎn)單的鑒權(quán)服務(wù)

    手把手教你用Java實(shí)現(xiàn)一套簡(jiǎn)單的鑒權(quán)服務(wù)

    現(xiàn)今大部分系統(tǒng)都會(huì)有自己的鑒權(quán)服務(wù),本文介紹了最常用的鑒權(quán)服務(wù),就是日常用戶的登錄登出,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • SpringBoot3安全管理操作方法

    SpringBoot3安全管理操作方法

    這篇文章主要介紹了SpringBoot3安全管理,在實(shí)際開(kāi)發(fā)中,最常用的是登錄驗(yàn)證和權(quán)限體系兩大功能,在登錄時(shí)完成身份的驗(yàn)證,加載相關(guān)信息和角色權(quán)限,在訪問(wèn)其他系統(tǒng)資源時(shí),進(jìn)行權(quán)限的驗(yàn)證,保護(hù)系統(tǒng)的安全,文中有詳細(xì)的操作步驟,需要的朋友可以參考下
    2023-08-08
  • Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題

    Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題

    這篇文章主要介紹了Spring Boot 2.4新特性減少95%內(nèi)存占用問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java探索之string字符串的應(yīng)用代碼示例

    Java探索之string字符串的應(yīng)用代碼示例

    這篇文章主要介紹了Java探索之string字符串的應(yīng)用代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • 詳解spring cloud分布式關(guān)于熔斷器

    詳解spring cloud分布式關(guān)于熔斷器

    這篇文章主要介紹了詳解spring cloud分布式關(guān)于熔斷器,詳細(xì)的介紹了什么是熔斷器和使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 詳解Spring Boot + Mybatis 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源

    詳解Spring Boot + Mybatis 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源

    這篇文章主要介紹了Spring Boot + Mybatis 實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論