日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Topic交换器-编写生产者

發布時間:2024/4/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Topic交换器-编写生产者 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們已經把環境搭建好了,我們就來編寫具體代碼,我們首先來編寫Provider,消息提供者的代碼,編寫Provider,我們打開我們的項目,配置文件先關掉,看我們的Provider,Provider這里面我們先看一下Sender,Sender我們之前是按照Direct方式去發送的,交換器,路由器,然后消息,那么這里我們需要注意的就是,路由器的這個key,我們原來是從配置文件里注入進來的,現在我們不要了,我們先暫時把它給寫死一下,我們先來看一下,來看一下這個文件,他的路由key是不是用通配的形式來表示的,星點什么log.info,那么我們現在呢,比如我們現在來編寫用戶服務,那么對于他的路由key,是不是可以用user.log.info,如果是商品呢,我們可以用product.log.info,那么這樣的話,不管你前面是用戶user,product,只要你的后面,后綴,log.info的,這兩個服務的消息,這兩個服務info的消息,是不是都會進入到log.info的隊列當中,我們現在要描述的是這樣的一個過程,我們首先要去創建一個用戶服務,這里我們還叫Sender呢,我們給他改個名字,這個是UserSender,然后交換器,我們是從配置文件里讀的,我們取的名字叫topic,然后回到我們的Sender里面,比如我們現在起路由鍵的規則,用戶服務就是user.log.info,商品是product.log.info,然后訂單服務就是order.log.info,我們按照這個規則去定義他的路由和key,這塊我們來寫吧,user.log.info,比如我們看一下這個圖,它是有info,有error的,然后還有其他的,我們這里就給一個infothis.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.info", "user.log.info....."+msg);我們按照日志級別,比如有debug,有人說你這里寫debug也沒用啊,因為你這里沒有debug的隊列,debug雖然這里沒有,但是你看最后,全日志處理服務,他是不是包含了*.log.*,那么就是像debug這樣的信息,雖然在這個隊列的路由key當中,但是最后的log隊列里是可以進入到這里面去,就是進入到全日志里是沒問題的,所以debug,info,然后還有一個warn,然后還有一個error,我們給他五個級別的日志,這五條像debug,像這個info,error,會進到這兩個隊列當中,像debug,info,warn,還有error,其實他們都會進入到這個隊列里,因為他的路由key的匹配,更模糊,是全日志,然后后面消息,后面的消息我們也可以給他加一個標識this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.debug", "user.log.debug....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.info", "user.log.info....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.warn","user.log.warn....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.error", "user.log.error....."+msg);這樣我們就寫好了一個UserSender,我們把它定義出來了,用戶服務,然后接下來我們還得有商品服務,我們把它copy一下,ProductSender,然后回到我們的productSender當中,我們看一下這個key是不是得改一下,原來我們拷貝的是user服務里面的,他的key是user.info里面的,我們現在在商品服務里面,是不是得改成product.log.info,product.log.error,讓product的服務的key是productthis.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.debug", "product.log.debug....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.info", "product.log.info....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.warn","product.log.warn....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.error", "product.log.error....."+msg);其他的我們就不用動了,product就完事了,然后我們還得去copy一個,這個是訂單服務,OrderSender,然后回到order的服務當中,這里是不是要叫order.log.debug,然后order.log.info,order.log.warn,order.log.error,this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.debug", "order.log.debug....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.info", "order.log.info....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.warn","order.log.warn....."+msg); this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.error", "order.log.error....."+msg);這樣我們就把服務端的代碼就寫完了,給他們定義不同的路由key了,然后他會根據不同的路由key,向我們的RabbitMQ發送消息,這是他發送的消息,然后我們把代碼整理到筆記當中,這個是UserSender,ProductSender,然后是OrderSender,那么到目前為止呢,消息發送者Provider這邊的代碼,就寫完了 <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><groupId>com.learn</groupId><artifactId>rabbitmq-topic-provider</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> </parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><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></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> spring.application.name=rabbitmq-topic-providerspring.rabbitmq.host=59.110.158.145 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guestmq.config.exchange=log.topic package com.learn;import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;/*** 消息發送者* @author Administrator**/ @Component public class UserSender {@Autowiredprivate AmqpTemplate rabbitAmqpTemplate;//exchange 交換器名稱@Value("${mq.config.exchange}")private String exchange;/** 發送消息的方法*/public void send(String msg){//向消息隊列發送消息//參數一:交換器名稱。//參數二:路由鍵//參數三:消息this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.debug", "user.log.debug....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.info", "user.log.info....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.warn","user.log.warn....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.error", "user.log.error....."+msg);} } package com.learn;import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;/*** 消息發送者* @author Administrator**/ @Component public class ProductSender {@Autowiredprivate AmqpTemplate rabbitAmqpTemplate;//exchange 交換器名稱@Value("${mq.config.exchange}")private String exchange;/** 發送消息的方法*/public void send(String msg){//向消息隊列發送消息//參數一:交換器名稱。//參數二:路由鍵//參數三:消息this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.debug", "product.log.debug....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.info", "product.log.info....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.warn","product.log.warn....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"product.log.error", "product.log.error....."+msg);} } package com.learn;import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;/*** 消息發送者* @author Administrator**/ @Component public class OrderSender {@Autowiredprivate AmqpTemplate rabbitAmqpTemplate;//exchange 交換器名稱@Value("${mq.config.exchange}")private String exchange;/** 發送消息的方法*/public void send(String msg){//向消息隊列發送消息//參數一:交換器名稱。//參數二:路由鍵//參數三:消息this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.debug", "order.log.debug....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.info", "order.log.info....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.warn","order.log.warn....."+msg);this.rabbitAmqpTemplate.convertAndSend(this.exchange,"order.log.error", "order.log.error....."+msg);} } package com.learn;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class RabbitTopicProviderApplication {public static void main(String[] args) {SpringApplication.run(RabbitTopicProviderApplication.class, args);} } package com.learn.test;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;import com.learn.OrderSender; import com.learn.ProductSender; import com.learn.RabbitTopicProviderApplication; import com.learn.UserSender;/*** 消息隊列測試類* @author Administrator**/ @RunWith(SpringRunner.class) @SpringBootTest(classes=RabbitTopicProviderApplication.class) public class QueueTest {@Autowiredprivate UserSender usersender;@Autowiredprivate ProductSender productsender;@Autowiredprivate OrderSender ordersender;/** 測試消息隊列*/@Testpublic void test1(){this.usersender.send("UserSender.....");this.productsender.send("ProductSender....");this.ordersender.send("OrderSender......");} }

?

總結

以上是生活随笔為你收集整理的Topic交换器-编写生产者的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。