本篇文章為大家展示了rabbitmq如何在SpringBoot中應用,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
北湖ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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-amqp</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
AMQP,即Advanced Message Queuing Protocol,一個提供統(tǒng)一消息服務的應用層標準高級消息隊列協(xié)議,是應用層協(xié)議的一個開放標準,為面向消息的中間件設計。基于此協(xié)議的客戶端與消息中間件可傳遞消息,并不受客戶端/中間件不同產(chǎn)品,不同的開發(fā)語言等條件的限制,spring-boot-starter-amqp引入的就是rabbitmq。有個前提,你的機子上要首先先安裝rabbitmq的server,然后執(zhí)行 rabbitmq-server server就啟動了。啟動后,我們就可以配置我們的客戶端程序了。首先看下我們的配置文件
spring.application.name: spirng-boot-rabbitmq spring.rabbitmq.host: 127.0.0.1 spring.rabbitmq.port: 5672 spring.rabbitmq.username: guest spring.rabbitmq.password: guest
配置了服務器的IP,端口,用戶名,密碼等基礎信息,保證我們能連上服務器。
增加一個Rabbitmq的配置類
package com.shuqi; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public Queue Queue() { return new Queue("hello"); } }
創(chuàng)建了一個名稱叫做hello的隊列,然后producer可以往hello的隊列里放數(shù)據(jù),consumer可以從hello的隊列里消費數(shù)據(jù)??聪聀roducer的處理程序
package com.shuqi.controller; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private AmqpTemplate rabbitTemplate; @RequestMapping("/hello") public String hello(@RequestParam String name){ rabbitTemplate.convertAndSend("hello","hello "+name); return "消息發(fā)送成功"; } }
通過controller生產(chǎn)消息,通過AmqpTemplate發(fā)送消息。有了生產(chǎn)者我們看下消費者
package com.shuqi.consumer; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "hello") @Slf4j public class HelloConsumer { @RabbitHandler public void process(String hello) { log.info("接收到的消息:message:{}",hello); } }
@RabbitListener(queues = "hello") 表示是一個Rabbitmq的監(jiān)聽器,監(jiān)聽的隊列名稱是hello,說明數(shù)據(jù)可定會過來,數(shù)據(jù)過來了,通過 @RabbitHandler 修飾的方法來處理過來的數(shù)據(jù)。打印一下。下面我們啟動項目看看效果。
在瀏覽器中輸入 http://localhost:8080/hello?name=shuqi 看到下面的結(jié)果
看下控制臺輸出的日志
2018-03-25 16:24:32.752 INFO 4987 --- [cTaskExecutor-1] com.shuqi.consumer.HelloConsumer : 接收到的消息:message:hello shuqi
上述內(nèi)容就是rabbitmq如何在SpringBoot中應用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標題:rabbitmq如何在SpringBoot中應用
網(wǎng)頁地址:http://www.rwnh.cn/article2/gdddoc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、網(wǎng)站維護、建站公司、面包屑導航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)