目录
二、在application.properties 配置rabbitmq
一、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二、在application.properties 配置rabbitmq
spring.rabbitmq.host = 192.168.11.84
spring.rabbitmq.port = 5672
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest
#虚拟主机
spring.rabbitmq.virtual-host = /
三、创建一个消费者
package com.by.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Binding;
import java.io.IOException;
@Configuration
public class DirectConsumer {
//注册一个队列
@Bean //启动多次为什么不报错?启动的时候,它会根据这个名称Direct_Q01先去查找有没有这个队列,如果有什么都不做,如果没有创建一个新的
public Queue queue(){
return QueueBuilder.durable("Direct_Q01").maxLength(100).build();
}
//注册交换机
@Bean
public DirectExchange exchange(){
//1.启动的时候,它会根据这个名称Direct_E01先去查找有没有这个交换机,如果有什么都不做,如果没有创建一个新的
return ExchangeBuilder.directExchange("Direct_E01").build();
}
//绑定交换机与队列关系
@Bean
public Binding binding(Queue queue,DirectExchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("RK01");
}
//监听消息
// @RabbitListener(queues = "Direct_Q01")
// public void receiveMessage(String msg){
// System.out.println("收到消息:"+msg);
// }
}
四、创建一个生产者
package com.by.provider;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DirectProvide {
/**
* 生产者
* @param msg
*/
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(Object message) {
rabbitTemplate.convertAndSend("Direct_E01", "RK01", message);
}
}
五、测试代码
package com.by;
import com.by.provider.DirectProvide;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class ApplicationTests {
@Autowired
private DirectProvide directProvide;
@Test
void contextLoads() throws IOException, InterruptedException {
directProvide.send("hello word");
}
}
六、运行
首先启动主类,创建交换机和和队列,接着启动测试类,发送信息。
七、高级使用,发送一个对象
7.1. 新建一个model
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OrderingOk implements Serializable {
private int id;
private String name;
}
7.2 为了防止乱码,写一个rabbitmq的配置类
@Configuration
public class RabbitMQConfig {
/**
* 在上面的 Java 代码片段中,我们定义了一个 Spring Boot 应用整合 RabbitMQ 时用来配置消息转换器(MessageConverter)的类 RabbitMQConfig。
* Jackson2JsonMessageConverter 是 Spring AMQP 提供的一个消息转换器,它可以将消息体转换为 JSON 格式进行序列化和反序列化。
* 这意味着发送到 RabbitMQ 队列中的对象会被转换成 JSON 字符串,而从队列中接收到的消息也会被反序列化成相应的 Java 对象。
* @return
*/
/**
* 将对象转换为json
* @return
*/
@Bean
public MessageConverter messagConverter(){
return new Jackson2JsonMessageConverter();
}
7.3 写一个测试类
@SpringBootTest
class ApplicationTests {
@Autowired
private DirectProvide directProvide;
@Test
void contextLoads() throws IOException, InterruptedException {
OrderingOk orderingOk = OrderingOk.builder().id(1).name("张三").build();
directProvide.send(orderingOk);
}
}
7.4 测试结果
八、总体目录结构
九、源代码参考
https://codeup.aliyun.com/62858d45487c500c27f5aab5/springboot-rabbitmq.git