该笔记大部分搬运B站编程不良人的RabbitMQ,顺便把图文合并记录,便于回顾,仅用于学习!
视频地址:https://www.bilibili.com/video/BV1dE411K7MG
作者真的非常好,别白嫖,记得三连 如有侵权,请联系删除!
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
spring:
application:
name: springboot_rabbitmq
rabbitmq:
host: localhost
port: 5672
username: ems
password: 123
virtual-host: /ems
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 RabbitmqDemo02ApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads() {
rabbitTemplate.convertAndSend("hello","hello world");
}
}
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 HelloCustomer {
@RabbitHandler
public void receive(String message) {
System.out.println("message=="+message);
}
}
@SpringBootTest
class RabbitmqDemo02ApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads2() {
for (int i = 0; i < 20; i++) {
rabbitTemplate.convertAndSend("work",("provider"+i+"hello work"));
}
}
}
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class WorkCustomer {
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String message) {
System.out.println("work message1==" + message);
}
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String message) {
System.out.println("work message2==" + message);
}
}
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 RabbitmqDemo02ApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
//参数1为交换机,参数2为路由key,“”表示为任意路由,参数3为消息内容
@Test
void contextLoads3() {
rabbitTemplate.convertAndSend("logs"," ","fanout provider message");
}
}
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 FanoutCustomer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(name = "logs",type = "fanout")
))
public void receive1(String message) {
System.out.println("work message1==" + message);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(name = "logs",type = "fanout")
))
public void receive(String message){
System.out.println("work message==" + message);
}
}
@SpringBootTest
class RabbitmqDemo02ApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads4() {
rabbitTemplate.convertAndSend("directs","error","direct provider message");
}
}
@Component
public class DirectCustomer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
key = {"info","error"},
exchange = @Exchange(name = "directs",type = "direct")
))
public void receive1(String message) {
System.out.println("work message1==" + message);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
key = {"info"},
exchange = @Exchange(name = "directs",type = "direct")
))
public void receive2(String message) {
System.out.println("work message2==" + message);
}
}
@SpringBootTest
class RabbitmqDemo02ApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads5() {
rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
}
}
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 topicsCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
key = {"user.*"},
exchange = @Exchange(type = "topic",name = "topics")
)
})
public void receive1(String message){
System.out.println("message1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
key = {"user.#"},
exchange = @Exchange(type = "topic",name = "topics")
)
})
public void receive2(String message){
System.out.println("message2 = " + message);
}
}