1.先搭建项目架构,如下
consumer:消费者模块
productor:生产者模块
2.pom文件编写
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath />
</parent>
<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 添加springboot对amqp的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.编写两个模块的application.properties和Application.java
spring.rabbitmq.host=192.168.221.200
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
项目结构如图
4.编写生产者
topic模式
package com.yzy.rabbit;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description 该类用来注入队列、交换机并较队列和交换机绑定
* @create 2019-06-27-14:12
*/
@Component
public class TopicSenderCof {
//通过queue1可以获取名为F的队列
@Bean("queue1")
public Queue queue(){
return new Queue("F");
}
//通过qqueue2可以获取名为G的队列
@Bean("queue2")
public Queue queue2(){
return new Queue("G");
}
//注入一个topic类型的交换机
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topicExchange");
}
//将queue1表示的队列和交换机通过路由键top.message绑定。即通过top.message可以和该对立匹配
@Bean
public Binding binding(@Qualifier("queue1")Queue queue, TopicExchange topicExchange){
return BindingBuilder.bind(queue).to(topicExchange).with("top.message");
}
//将queue2表示的队列和交换机通过路由键top.#绑定。#表示零或多个。
@Bean
public Binding binding2(@Qualifier("queue2")Queue queue, TopicExchange topicExchange){
return BindingBuilder.bind(queue).to(topicExchange).with("top.#"); //*一个 #零个或多个
}
}
利用AmqpTemplate来发送消息
package com.yzy.rabbit;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description
* @create 2019-06-27-14:20
*/
@Component
public class TopicSender {
@Autowired
private AmqpTemplate amqpTemplate;
//发送消息 参数1:交换机名字 参数2:路由键 参数3:要发布的消息
public void send(){
amqpTemplate.convertAndSend("topicExchange", "top.message", "hello-rabbit-topic");
}
}
编写测试类
import com.yzy.Application;
import com.yzy.rabbit.TopicSender;
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.SpringJUnit4ClassRunner;
import java.applet.Applet;
import java.io.PipedReader;
/**
* @author yzy
* @description
* @create 2019-06-27-14:22
*/
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestTopicSend {
@Autowired
private TopicSender topicSender;
@Test
public void send(){
topicSender.send();
}
}
其他模式的代码在最后给出
5.编写消费者
topic模式
package com.yzy.consumer;
import ch.qos.logback.core.net.SyslogOutputStream;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description
* @create 2019-06-27-14:24
*/
@Component
public class TopicRe {
@RabbitListener(queues = "F")
public void getM(String str){
System.out.println("top.message路由的队列F==="+str);
}
@RabbitListener(queues = "G")
public void getMm(String str){
System.out.println("top.#路由的队列G==="+str);
}
}
第一次先启动生产者的测试类,再启动消费者模块的Application.java。
=======================direct模式、fanout模式代码=================================================
direct模式
配置类
package com.yzy.rabbit;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author yzy
* @description
* @create 2019-06-27-10:19
*/
@Configuration
public class MessageProductor {
@Bean
public Queue queue(){
return new Queue("queue");
}
}
发送消息类
package com.yzy.rabbit;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
/**
* @author yzy
* @description
* @create 2019-06-27-10:37
*/
@Configuration
public class MessageSender {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(){
amqpTemplate.convertAndSend("queue", "hello-rabbit");
}
}
测试类
import com.yzy.Application;
import com.yzy.rabbit.MessageSender;
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.SpringJUnit4ClassRunner;
/**
* @author yzy
* @description
* @create 2019-06-27-10:39
*/
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSend {
@Autowired
private MessageSender messageSender;
@Test
public void send(){
messageSender.send();
}
}
消费者模块
package com.yzy.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description
* @create 2019-06-27-10:41
*/
@Component
public class HelloRe {
@RabbitListener(queues = "queue")
public void processM(String str){
System.out.println("messge====="+ str);
}
}
fanout类型
配置类
package com.yzy.rabbit;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description
* @create 2019-06-27-10:51
*/
@Component
public class FanoutExchangeSenderCof {
//创建三个队列
@Bean("queueA")
public Queue queueA(){
return new Queue("A");
}
@Bean("queueB")
public Queue queueB(){
return new Queue("B");
}
@Bean("queueC")
public Queue queueC(){
return new Queue("C");
}
//创建fanout交换机
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchange");
}
//将队列和交换机绑定
@Bean
public Binding BindingQueueA(@Qualifier("queueA") Queue queue, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queue).to(fanoutExchange);
}
@Bean
public Binding BindingQueueB(@Qualifier("queueB") Queue queue, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queue).to(fanoutExchange);
}
@Bean
public Binding BindingQueueC(@Qualifier("queueC") Queue queue, FanoutExchange fanoutExchange){
return BindingBuilder.bind(queue).to(fanoutExchange);
}
}
发送消息类
package com.yzy.rabbit;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description
* @create 2019-06-27-11:00
*/
@Component
public class FanoutExchangeSender {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(){
//参数2被忽略
amqpTemplate.convertAndSend("fanoutExchange", "", "hello-rabbit-fanout");
}
}
测试类
import com.yzy.Application;
import com.yzy.rabbit.FanoutExchangeSender;
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.SpringJUnit4ClassRunner;
/**
* @author 杨郑耀
* @description
* @create 2019-06-27-11:04
*/
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestFanoutSend {
@Autowired
private FanoutExchangeSender fanoutExchangeSender;
@Test
public void send(){
fanoutExchangeSender.send();
}
}
消费者模块
package com.yzy.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @author yzy
* @description
* @create 2019-06-27-11:07
*/
@Component
public class FanoutRe {
@RabbitListener(queues = "A")
public void getMessA(String str){
System.out.println("队列A==="+ str);
}
@RabbitListener(queues = "B")
public void getMessB(String str){
System.out.println("队列B==="+ str);
}
@RabbitListener(queues = "C")
public void getMessC(String str){
System.out.println("队列C==="+ str);
}
}