Article directory
Create a SpringBoot project
I use Java8, jdk1.8, maven project, springboot2.6.11 version here
add dependencies
All dependencies needed:
<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>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
configuration file
Change the configuration file to a .yml suffix
and add:
server:
port: 8080
spring:
application:
name: rabbitmq-provider
rabbitmq:
host: localhost
port: 5672
username: admin
password: admin
#确认消息已发送到交换机
#publisher-confirm-type: correlated
#确认消息已发送到队列(Queue)
#publisher-returns: true
We will talk about the last two configurations later, you can not use them here
project structure
Among them: P is the producer,
C is the consumer,
X is the direct switch,
A is the queue,
XA is the RoutingKey, which is the line connecting X and A.
In this structure, switch X may not be used, but for completeness, it is added here.
For some basic concepts, such as producers and consumers, or if you don’t know much about the types of switches, you can go on to learn:
Configuration class code
The main role of the configuration class is to declare switches, queues, and bind them
@Configuration
public class Config {
//换机名称
public static final String X_EXCHANGE="X";
//队列名称
public static final String QUEUE_A="A";
//声明交换机xExchange
@Bean("xExchange")
public DirectExchange xExchange(){
return new DirectExchange(X_EXCHANGE);
}
//声明队列A
@Bean("queueA")
public Queue queueA(){
return QueueBuilder.durable(QUEUE_A).build();
}
//绑定交换机与队列
//A与X通过XA线路绑定
@Bean
public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
@Qualifier("xExchange") DirectExchange xExchange ){
return BindingBuilder.bind(queueA).to(xExchange).with("XA");
}
producer code
@Slf4j
@RestController
@RequestMapping("/myMQ")
public class ProduceController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMessage/{message}")
public void sendMessage(@PathVariable String message) {
log.info("发送消息:{}",message);
rabbitTemplate.convertAndSend("X", "XA",message);
}
}
consumer code
@Slf4j
@Component
public class Consumer {
//接收消息
@RabbitListener(queues="A")
public void receiveA(Message message){
log.info("当前时间:{},收到A队列的消息:{}",new Date().toString(),new String(message.getBody()));
}
}
Startup project
Start the pulled rabbitmq
startup project in docker
test
Test success
Articles in the same series
Principle part
Introduction to MQ (message queue) Introduction
to RabbitMQ Introduction to
RabbitMQ Four core concepts and working principles
Operating part
Docker for Windows installs RabbitMQ
Maven integrates RabbitMQ to realize production and consumption messages
RabbitMQ delay queue and actual combat
RabbitMQ release confirmation-switch confirmation
RabbitMQ-message return (queue confirmation)
RabbitMQ-backup switchRabbitMQ
-priority queue