pom.xml
<?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.2.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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.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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml
spring:
rabbitmq:
host: 192.168.56.200
port: 5672
username: ems
password: 123
virtual-host: /ems
测试主类:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
//Hello word
@Test
void hello() {
rabbitTemplate.convertAndSend("hello1","Hello world");
}
//work模型
@Test
void work() {
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work1","work模型"+i);
}
}
//Fanout 广播
@Test
void fanout() {
rabbitTemplate.convertAndSend("logs1","","fanout模型");
}
//Route 路由模式
@Test
void route() {
rabbitTemplate.convertAndSend("directs1","info","发送info的key的路由信息");
}
//topic 动态路由模式 订阅模式
@Test
void topic() {
rabbitTemplate.convertAndSend("topics1","user.save","user.save动态路由信息");
}
}
1.helloword
package com.example.demo.Hello;
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;
/**
* java类简单作用描述
*
* @ProjectName: demo
* @Package: com.example.demo.Hello
* @ClassName: consum
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 19:36
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 19:36
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
@Component
@RabbitListener(queuesToDeclare = @Queue("hello1"))
public class Hello {
@RabbitHandler
public void receive(String message){
System.out.println("message="+message);
}
}
2.work模型:
package com.example.demo.Hello;
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;
/**
* java类简单作用描述
*
* @ProjectName: demo
* @Package: com.example.demo.Hello
* @ClassName: consum
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 19:36
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 19:36
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
@Component
public class work {
//1个消费者
@RabbitListener(queuesToDeclare = @Queue("work1"))
public void receive1(String message){
System.out.println("message1="+message);
}
//2个消费者
@RabbitListener(queuesToDeclare = @Queue("work1"))
public void receive2(String message){
System.out.println("message2="+message);
}
}
3.fanout模型:
package com.example.demo.Hello;
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;
/**
* java类简单作用描述
*
* @ProjectName: demo
* @Package: com.example.demo.Hello
* @ClassName: consum
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 19:36
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 19:36
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
@Component
public class Fanout {
//1个消费者
@RabbitListener(bindings = {
@QueueBinding(
value =@Queue,//临时队列
exchange = @Exchange(value = "logs1",type = "fanout")//绑定的交换机
)
})
public void receive1(String message){
System.out.println("message1="+message);
}
//2个消费者
@RabbitListener(bindings = {
@QueueBinding(
value =@Queue,//临时队列
exchange = @Exchange(value = "logs1",type = "fanout")//绑定的交换机
)
})
public void receive2(String message){
System.out.println("message2="+message);
}
}
4.route模型:
package com.example.demo.Hello;
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;
/**
* java类简单作用描述
*
* @ProjectName: demo
* @Package: com.example.demo.Hello
* @ClassName: consum
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 19:36
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 19:36
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
@Component
public class Route {
//1个消费者
@RabbitListener(bindings = {
@QueueBinding(
value =@Queue,//临时队列
exchange = @Exchange(value = "directs1",type = "direct"),//绑定的交换机
key = {"info","error","warning"}
)
})
public void receive1(String message){
System.out.println("message1="+message);
}
//2个消费者
@RabbitListener(bindings = {
@QueueBinding(
value =@Queue,//临时队列
exchange = @Exchange(value = "directs1",type = "direct"),//绑定的交换机
key = {"error"}
)
})
public void receive2(String message){
System.out.println("message2="+message);
}
}
5.topic模型:
package com.example.demo.Hello;
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;
/**
* java类简单作用描述
*
* @ProjectName: demo
* @Package: com.example.demo.Hello
* @ClassName: consum
* @Description: java类作用描述
* @Author: zhangq
* @CreateDate: 2020-12-10 19:36
* @UpdateUser: zhangq
* @UpdateDate: 2020-12-10 19:36
* @UpdateRemark: The modified content
* @Version: 1.0 *
*/
@Component
public class topic {
//1个消费者
@RabbitListener(bindings = {
@QueueBinding(
value =@Queue,//临时队列
exchange = @Exchange(value = "topics1",type = "topic"),//绑定的交换机
key = {"user.save","user.*"}
)
})
public void receive1(String message){
System.out.println("message1="+message);
}
//2个消费者
@RabbitListener(bindings = {
@QueueBinding(
value =@Queue,//临时队列
exchange = @Exchange(value = "topics1",type = "topic"),//绑定的交换机
key = {"order.#","product.#","user.*"}
)
})
public void receive2(String message){
System.out.println("message2="+message);
}
}