Topic交换器-编写生产者
生活随笔
收集整理的這篇文章主要介紹了
Topic交换器-编写生产者
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們已經(jīng)把環(huán)境搭建好了,我們就來編寫具體代碼,我們首先來編寫Provider,消息提供者的代碼,編寫Provider,我們打開我們的項(xiàng)目,配置文件先關(guān)掉,看我們的Provider,Provider這里面我們先看一下Sender,Sender我們之前是按照Direct方式去發(fā)送的,交換器,路由器,然后消息,那么這里我們需要注意的就是,路由器的這個key,我們原來是從配置文件里注入進(jìn)來的,現(xiàn)在我們不要了,我們先暫時把它給寫死一下,我們先來看一下,來看一下這個文件,他的路由key是不是用通配的形式來表示的,星點(diǎn)什么log.info,那么我們現(xiàn)在呢,比如我們現(xiàn)在來編寫用戶服務(wù),那么對于他的路由key,是不是可以用user.log.info,如果是商品呢,我們可以用product.log.info,那么這樣的話,不管你前面是用戶user,product,只要你的后面,后綴,log.info的,這兩個服務(wù)的消息,這兩個服務(wù)info的消息,是不是都會進(jìn)入到log.info的隊(duì)列當(dāng)中,我們現(xiàn)在要描述的是這樣的一個過程,我們首先要去創(chuàng)建一個用戶服務(wù),這里我們還叫Sender呢,我們給他改個名字,這個是UserSender,然后交換器,我們是從配置文件里讀的,我們?nèi)〉拿纸衪opic,然后回到我們的Sender里面,比如我們現(xiàn)在起路由鍵的規(guī)則,用戶服務(wù)就是user.log.info,商品是product.log.info,然后訂單服務(wù)就是order.log.info,我們按照這個規(guī)則去定義他的路由和key,這塊我們來寫吧,user.log.info,比如我們看一下這個圖,它是有info,有error的,然后還有其他的,我們這里就給一個infothis.rabbitAmqpTemplate.convertAndSend(this.exchange,"user.log.info", "user.log.info....."+msg);我們按照日志級別,比如有debug,有人說你這里寫debug也沒用啊,因?yàn)槟氵@里沒有debug的隊(duì)列,debug雖然這里沒有,但是你看最后,全日志處理服務(wù),他是不是包含了*.log.*,那么就是像debug這樣的信息,雖然在這個隊(duì)列的路由key當(dāng)中,但是最后的log隊(duì)列里是可以進(jìn)入到這里面去,就是進(jìn)入到全日志里是沒問題的,所以debug,info,然后還有一個warn,然后還有一個error,我們給他五個級別的日志,這五條像debug,像這個info,error,會進(jìn)到這兩個隊(duì)列當(dāng)中,像debug,info,warn,還有error,其實(shí)他們都會進(jìn)入到這個隊(duì)列里,因?yàn)樗穆酚蒶ey的匹配,更模糊,是全日志,然后后面消息,后面的消息我們也可以給他加一個標(biāo)識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,我們把它定義出來了,用戶服務(wù),然后接下來我們還得有商品服務(wù),我們把它c(diǎn)opy一下,ProductSender,然后回到我們的productSender當(dāng)中,我們看一下這個key是不是得改一下,原來我們拷貝的是user服務(wù)里面的,他的key是user.info里面的,我們現(xiàn)在在商品服務(wù)里面,是不是得改成product.log.info,product.log.error,讓product的服務(wù)的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一個,這個是訂單服務(wù),OrderSender,然后回到order的服務(wù)當(dāng)中,這里是不是要叫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);這樣我們就把服務(wù)端的代碼就寫完了,給他們定義不同的路由key了,然后他會根據(jù)不同的路由key,向我們的RabbitMQ發(fā)送消息,這是他發(fā)送的消息,然后我們把代碼整理到筆記當(dāng)中,這個是UserSender,ProductSender,然后是OrderSender,那么到目前為止呢,消息發(fā)送者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><!-- 這個插件,可以將應(yīng)用打包成一個可執(zhí)行的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;/*** 消息發(fā)送者* @author Administrator**/
@Component
public class UserSender {@Autowiredprivate AmqpTemplate rabbitAmqpTemplate;//exchange 交換器名稱@Value("${mq.config.exchange}")private String exchange;/** 發(fā)送消息的方法*/public void send(String msg){//向消息隊(duì)列發(fā)送消息//參數(shù)一:交換器名稱。//參數(shù)二:路由鍵//參數(shù)三:消息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;/*** 消息發(fā)送者* @author Administrator**/
@Component
public class ProductSender {@Autowiredprivate AmqpTemplate rabbitAmqpTemplate;//exchange 交換器名稱@Value("${mq.config.exchange}")private String exchange;/** 發(fā)送消息的方法*/public void send(String msg){//向消息隊(duì)列發(fā)送消息//參數(shù)一:交換器名稱。//參數(shù)二:路由鍵//參數(shù)三:消息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;/*** 消息發(fā)送者* @author Administrator**/
@Component
public class OrderSender {@Autowiredprivate AmqpTemplate rabbitAmqpTemplate;//exchange 交換器名稱@Value("${mq.config.exchange}")private String exchange;/** 發(fā)送消息的方法*/public void send(String msg){//向消息隊(duì)列發(fā)送消息//參數(shù)一:交換器名稱。//參數(shù)二:路由鍵//參數(shù)三:消息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;/*** 消息隊(duì)列測試類* @author Administrator**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=RabbitTopicProviderApplication.class)
public class QueueTest {@Autowiredprivate UserSender usersender;@Autowiredprivate ProductSender productsender;@Autowiredprivate OrderSender ordersender;/** 測試消息隊(duì)列*/@Testpublic void test1(){this.usersender.send("UserSender.....");this.productsender.send("ProductSender....");this.ordersender.send("OrderSender......");}
}
?
總結(jié)
以上是生活随笔為你收集整理的Topic交换器-编写生产者的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Topic交换器-搭建环境
- 下一篇: Topic交换器-编写消费者