SpringBoot 简易Rabbit 消息处理
Rabbit这里不做介绍,可以百度找找相关内容。
三种常用的交换器:direct、fanout、topic
direct:点对点消息,严格按照路由键绑定的消息队列一对一的单发消息
fanout: 不管路由键是什么,给所有绑定的消息队列全发消息
topic:给符合路由键绑定的多个消息队列多发消息。如 *.news,#.news。(*:任意一个单词,#:任意多个单词)
---提前创建一些交换器和消息队列并且绑定:
(1)pom 加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
(2)配置文件简单配置
host默认是localhost, 是本机的话可以不写。 username和password是rabbit的登录用户名和密码。guest是初始默认的
spring.rabbitmq.host=192.168.0.113
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
(3)bean中写一个Book类用作测试
package com.***.bean;
public class Book {
private Integer id;
private String bookName;
private String author;
public Book(Integer id, String bookName, String author) {
this.id = id;
this.bookName = bookName;
this.author = author;
}
public Book() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookName='" + bookName + '\'' +
", author='" + author + '\'' +
'}';
}
}
(4)在测试类进行测试
package com.***;
import com.ysp.bean.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
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;
import java.util.Arrays;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRabbitApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void contextLoads() {
}
//direct 单播消息
@Test
public void testMsg01(){
System.out.println("发送单播消息~");
rabbitTemplate.convertAndSend("ysp.direct","user.news",
Arrays.asList("好像还不错哦",123,true));
}
@Test
public void testMsg02(){
System.out.println("发送单播消息~ 发对象");
rabbitTemplate.convertAndSend("ysp.direct","emp.news",
new Book(1,"三国演义","罗贯中"));
}
// fanout 广播消息 topic类型也类似
@Test
public void testMsg03(){
System.out.println("发送广播消息~ 发对象");
rabbitTemplate.convertAndSend("ysp.fanout","",
new Book(2,"西游记","吴承恩"));
}
//接收消息
@Test
public void testMsg04(){
Object o = rabbitTemplate.receiveAndConvert("user.news");
System.out.println(o.getClass());
System.out.println(o);
}
}
(5) 可对消息队列进行监听,一有消息就运行方法 取出消息
需要在 APP类上 添加@EnableRabbit
package com.***.service;
import com.ysp.bean.Book;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@RabbitListener(queues = "user.system")
public void getMag(Book book) {
System.out.println(book.toString());
}
}
(6)可在程序内部用amqpAdmin对象操作 交换器 消息对列 binding。
直接注入amqpAdmin对象,然后可跟踪到相应类了解多个方法的的意义和参数的意义。
@Autowired
AmqpAdmin amqpAdmin;
//操作AmqpAdmin
@Test
public void testAdmin(){
//创建交换器
amqpAdmin.declareExchange(new DirectExchange("admin.direct"));
//创建消息队列
amqpAdmin.declareQueue(new Queue("admin.queue"));
//添加绑定
amqpAdmin.declareBinding(new Binding("admin.queue",Binding.DestinationType.QUEUE,
"admin.direct","admin.queue",null));
}
运行测试都可以在RabbitMQ和控制台直观看到。