POM依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.meirisheng</groupId>
<artifactId>ranbbimq02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ranbbimq02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.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</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件
spring:
application:
name: springboot-rabbitmq
rabbitmq:
username: xuhaitao
password: 123456
addresses: 127.0.0.1:5672
virtual-host: /my
生产者:
package com.meirisheng.ranbbimq02;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = Ranbbimq02Application.class)
@RunWith(SpringRunner.class)
public class provider {
@Autowired
private RabbitTemplate rabbitTemplate;
//Topics模式,动态路由模式
@Test
public void testTopics() {
rabbitTemplate.convertAndSend("change03", "user.save.add","user.save发送的消息 ");
}
//路由模式
@Test
public void testRouter() {
rabbitTemplate.convertAndSend("change02", "error","router模型发送的消息 ");
}
//fanout广播模式 ,一次发送,所有都能收到
@Test
public void testPublish() {
rabbitTemplate.convertAndSend("change01", "","fanout模型发送的消息 ");
}
//work队列,发送到一个队列,所有消费者轮询公平消费
@Test
public void testWork() {
for (int i = 0; i < 6; i++) {
rabbitTemplate.convertAndSend("work", "wo shi xuhaitao 1 2 45");
}
}
//简单模型,直接往队列中发
@Test
public void testSimple() {
rabbitTemplate.convertAndSend("hello", "wo shi xuhaitao 1 5 9");
}
}
SimpleConsumer:
package com.meirisheng.ranbbimq02.consummers;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class SimpleConsumer {
@RabbitHandler
public void receive(String message){
System.out.println("message = " + message);
}
}
WorkConsumer:
package com.meirisheng.ranbbimq02.consummers;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class WorkConsumer {
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String msg) {
System.out.println("msg1 = " + msg);
}
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String msg) {
System.out.println("msg2 = " + msg);
}
}
FanoutCustomer:
package com.meirisheng.ranbbimq02.consummers;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
//publish/subscribe 发布订阅模式也是Fanout模式,也称广播模式
@Component
public class FanoutCustomer {
//定义一个消费者
@RabbitListener(bindings = {
@QueueBinding(
value= @Queue,
exchange = @Exchange(value = "change01",type = "fanout"))
})
public void receive1(String message){
System.out.println("message1 = " + message);
}
//再定义一个消费者
@RabbitListener(bindings = {
@QueueBinding(
value= @Queue,
exchange = @Exchange(value = "change01",type = "fanout"))
})
public void receive2(String message){
System.out.println("message2 = " + message);
}
}
RouterCustomer:
package com.meirisheng.ranbbimq02.consummers;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RouterCustomer {
//定义一个消费者
@RabbitListener(bindings = {
@QueueBinding(
value= @Queue,
exchange = @Exchange(value = "change02",type = "direct"),
key={"info","error"}
)
})
public void receive1(String msg){
System.out.println("msg = " + msg);
}
//再定义一个消费者
@RabbitListener(bindings = {
@QueueBinding(
value= @Queue,
exchange = @Exchange(value = "change02",type = "direct"),
key={"info"}
)
})
public void receive2(String msg){
System.out.println("msg2 = " + msg);
}
}
TopicConsumer:
package com.meirisheng.ranbbimq02.consummers;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class TopicConsumer {
//定义一个消费者
@RabbitListener(bindings = {
@QueueBinding(
value= @Queue,
exchange = @Exchange(value = "change03",type = "topic"),
key={"user.*"}
)
})
public void receive1(String msg){
System.out.println("msg1 = " + msg);
}
//定义一个消费者
@RabbitListener(bindings = {
@QueueBinding(
value= @Queue,
exchange = @Exchange(value = "change03",type = "topic"),
key={"user.#"}
)
})
public void receive2(String msg){
System.out.println("msg2 = " + msg);
}
}
FR:徐海涛(hunk Xu)
QQ技术交流群:386476712