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

Springboot集成RabbitMQ并驗(yàn)證五種消息模型

 更新時(shí)間:2024年03月08日 09:18:52   作者:道法自然 實(shí)事求是  
RabbitMQ 提供了5種常用消息模型,本文主要介紹了Springboot集成RabbitMQ并驗(yàn)證五種消息模型,具有一定的參考價(jià)值,感興趣的可以了解一下

一、添加依賴

        <!--AMQP依賴,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- 引入knife4j-spring-ui包 /doc.html-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

二、配置文件中添加RabbitMQ訪問(wèn)配置

spring.rabbitmq.host=192.168.0.104
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin123

三、消息生產(chǎn)者代碼

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "生產(chǎn)者服務(wù)")
@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "基本消息模型")
    @PostMapping("/testHello")
    public String testHello() {
        rabbitTemplate.convertAndSend("hello","hello world");
        return "ok";
    }

    @ApiOperation(value = "work消息模型")
    @PostMapping("/testWork")
    public String testWork() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","hello work!");
        }
        return "ok";
    }

    @ApiOperation(value = "訂閱模型-Fanout 廣播模式")
    @PostMapping("/testFanout")
    public String testFanout() {
        rabbitTemplate.convertAndSend("logs","","這是日志廣播");
        return "ok";
    }

    @ApiOperation(value = "訂閱模型-Direct")
    @PostMapping("/testDirect")
    public String testDirect() {
        rabbitTemplate.convertAndSend("directs","error","error 的日志信息");
        return "ok";
    }

    @ApiOperation(value = "訂閱模型-Topic")
    @PostMapping("/testTopic")
    public String testTopic() {
        rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
        return "ok";
    }
}

四、消息消費(fèi)者代碼

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component
public class SpringRabbitListener {

     //基本消息模型
     @RabbitListener(queuesToDeclare = @Queue("hello"))
     public void receive(String message) {
         System.out.println("message = " + message);
     }

     //work消息模型
     @RabbitListener(queuesToDeclare = @Queue("work"))
     public void workReceive1(String message) {
         System.out.println("workReceive1 message = " + message);
     }
     //work消息模型
     @RabbitListener(queuesToDeclare = @Queue("work"))
     public void workReceive2(String message) {
         System.out.println("workReceive2 message = " + message);
     }

    //訂閱模型-Fanout 廣播模式
    @RabbitListener(bindings = @QueueBinding(value = @Queue, exchange = @Exchange(name="logs",type = "fanout")))
    public void fanoutReceive(String message) {
        System.out.println("fanoutReceive message = " + message);
    }

    //訂閱模型-Direct
    @RabbitListener(bindings ={@QueueBinding(value = @Queue(),key={"info","error"},exchange = @Exchange(type = "direct",name="directs"))})
    public void directReceive(String message) {
        System.out.println("directReceive message = " + message);
    }

    //訂閱模型-Topic
    @RabbitListener(bindings = {@QueueBinding( value = @Queue,key = {"user.*"},exchange = @Exchange(type = "topic",name = "topics"))})
    public void topicReceive1(String message) {
        System.out.println("topicReceive1 message = " + message);
    }
    //訂閱模型-Topic
    @RabbitListener(bindings = {@QueueBinding(value = @Queue,key = {"user.#"},exchange = @Exchange(type = "topic",name = "topics"))})
    public void topicReceive2(String message) {
        System.out.println("topicReceive2 message = " + message);
    }

}

五、驗(yàn)證

瀏覽器輸入:ip:8080/doc.html 對(duì)接口逐個(gè)進(jìn)行測(cè)試

參考資料

到此這篇關(guān)于Springboot集成RabbitMQ并驗(yàn)證五種消息模型的文章就介紹到這了,更多相關(guān)Springboot RabbitMQ驗(yàn)證消息模型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • java swing實(shí)現(xiàn)QQ賬號(hào)密碼輸入框

    java swing實(shí)現(xiàn)QQ賬號(hào)密碼輸入框

    這篇文章主要為大家詳細(xì)介紹了Java swing實(shí)現(xiàn)QQ賬號(hào)密碼輸入框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • 最新springboot解決跨域的幾種方式小結(jié)

    最新springboot解決跨域的幾種方式小結(jié)

    跨域指的是瀏覽器不能執(zhí)?其他?站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對(duì)javascript施加的安全限制,這篇文章主要介紹了springboot解決跨域的幾種方式,需要的朋友可以參考下
    2022-05-05
  • Java中equals和hashcode用法

    Java中equals和hashcode用法

    `equals`和`hashCode`方法在Java中密切相關(guān),必須保持一致性,如果兩個(gè)對(duì)象通過(guò)`equals`方法相等,它們的`hashCode`也必須相同,這對(duì)于基于哈希的數(shù)據(jù)結(jié)構(gòu)至關(guān)重要,因?yàn)檫@些結(jié)構(gòu)依賴哈希值進(jìn)行快速查找和存儲(chǔ),為了減少哈希沖突
    2025-01-01
  • GraalVM和Spring Native嘗鮮一步步讓Springboot啟動(dòng)飛起來(lái)66ms完成啟動(dòng)

    GraalVM和Spring Native嘗鮮一步步讓Springboot啟動(dòng)飛起來(lái)66ms完成啟動(dòng)

    GraalVM是高性能的JDK,支持Java/Python/JavaScript等語(yǔ)言,它可以讓Java變成二進(jìn)制文件來(lái)執(zhí)行,讓程序在任何地方運(yùn)行更快,這篇文章主要介紹了GraalVM和Spring Native嘗鮮一步步讓Springboot啟動(dòng)飛起來(lái)66ms完成啟動(dòng),需要的朋友可以參考下
    2023-02-02
  • java實(shí)現(xiàn)貪吃蛇游戲代碼(附完整源碼)

    java實(shí)現(xiàn)貪吃蛇游戲代碼(附完整源碼)

    這篇文章主要介紹了java實(shí)現(xiàn)貪吃蛇游戲代碼(附完整源碼),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Spring?Boot虛擬線程Webflux在JWT驗(yàn)證和MySQL查詢性能比較

    Spring?Boot虛擬線程Webflux在JWT驗(yàn)證和MySQL查詢性能比較

    這篇文章主要為大家介紹了Spring Boot虛擬線程與Webflux在JWT驗(yàn)證和MySQL查詢上的性能比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

    用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn)

    這篇文章主要介紹了用Java編程輸出萬(wàn)年歷的功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • SpringBoot實(shí)現(xiàn)Tomcat集群的會(huì)話管理功能

    SpringBoot實(shí)現(xiàn)Tomcat集群的會(huì)話管理功能

    在使用 Tomcat 集群時(shí),由于每個(gè) Tomcat 實(shí)例的 Session 存儲(chǔ)是獨(dú)立的,導(dǎo)致無(wú)法實(shí)現(xiàn) Session 的共享,這可能影響到用戶跨節(jié)點(diǎn)的訪問(wèn),為了實(shí)現(xiàn)跨 Tomcat 實(shí)例共享 Session,可以使用 Spring Session 配合 Redis 進(jìn)行集中式會(huì)話管理,需要的朋友可以參考下
    2024-12-12
  • java 中繼承和多態(tài)詳細(xì)介紹

    java 中繼承和多態(tài)詳細(xì)介紹

    這篇文章主要介紹了java 中繼承和多態(tài)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 解決idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException的問(wèn)題

    解決idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException的問(wèn)題

    這篇文章主要介紹了idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException,解決打不開(kāi)idea問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論