我们已经把环境搭建好了,我们就来编写具体代码,我们首先来编写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的,
然后还有其他的,我们这里就给一个info
this.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是product
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);
其他的我们就不用动了,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-provider
spring.rabbitmq.host=59.110.158.145
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
mq.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 {
@Autowired
private 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 {
@Autowired
private 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 {
@Autowired
private 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 {
@Autowired
private UserSender usersender;
@Autowired
private ProductSender productsender;
@Autowired
private OrderSender ordersender;
/*
* 测试消息队列
*/
@Test
public void test1(){
this.usersender.send("UserSender.....");
this.productsender.send("ProductSender....");
this.ordersender.send("OrderSender......");
}
}