引入依赖
引入依赖之后,配置文件写完,spring 就帮我们注入了,RabbitTemplate
spring: application: name: rabbitmq_name rabbitmq: host: 主机地址 port: 端口号 username: 账号 password: 密码 virtual-hoat: 虚拟主机地址
RabbitTemplate 用来简化操作,使用时直接注入,在spring跟mq服务器建立配置文件之后会自动实例化,模板对象
队列模型 点对点生产者
@springBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(StringRunner.class) //spirng 工厂
public class Test{
@Autowired
private RabbitTemplate rabbitTemplate;
// 生产者不会自动创建队列
@Test
public void test(){
// rountingKye,Object
rabbitTemplate.convertAndSend("hello","body消息");
}
}
消费者
01:-------------
@Component
// 加上这个注解,告诉spring它是一个消费者
// 参数 queuesToDeclare 表示监听哪个队列,如果没有,就创建/其他参数请看:01
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer{
@RabbitHandler
public void n(String message){
System,out.pring(mess)
}
}
队列模型,1对多
二:
生产者
@springBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(StringRunner.class) //spirng 工厂
public class Test{
@Autowired
private RabbitTemplate rabbitTemplate;
// 生产者不会自动创建队列
@Test
public void test(){
// rountingKye,Object
rabbitTemplate.convertAndSend("worl","worl消息");
}
}
消费者
@Component
public class HelloCustomer{
//不用rabbitHandler 可以直接用RabbitListener 监听
@RabbitListener(queuesToDeclare = @Queue("work"))
public void n(String message){
System,out.pring(mess)
}
// 监听同一个,轮询执行,平均分
// 可以监听多个
@RabbitListener(queuesToDeclare = @Queue("work"))
public void n(String message){
System,out.pring(mess)
}
}
交换机模型 广播
生产者
@springBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(StringRunner.class) //spirng 工厂
public class Test{
@Autowired
private RabbitTemplate rabbitTemplate;
// 生产者不会自动创建队列
@Test
public void test(){
// 交换机 rountingKye,Object
rabbitTemplate.convertAndSend("worl","","worl消息");
}
}
消费者
@Component
public class HelloCustomer{
//不用rabbitHandler 可以直接用RabbitListener 监听
@RabbitListener(bindings={
@QueuBinding(
//声明指定名称,不声明,创建临时队列@Queue("work")
value = @Queue, //临时队列
exchange=@Exchange(value = "work", type = "fanout") //绑定交换机
)
})
public void test (String message){
System,out.pring(mess)
}
}
路由key
生产者
@springBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(StringRunner.class) //spirng 工厂
public class Test{
@Autowired
private RabbitTemplate rabbitTemplate;
// 生产者不会自动创建队列
@Test
public void test(){
// 交换机 rountingKye,Object
rabbitTemplate.convertAndSend("work","work01","worl消息");
}
}
消费者
@Component
public class HelloCustomer{
//不用rabbitHandler 可以直接用RabbitListener 监听
@RabbitListener(bindings={
@QueuBinding(
//声明指定名称,不声明,创建临时队列@Queue("work")
value = @Queue, //临时队列
exchange=@Exchange(value = "work", type = "direct") //自定义交换机名称和类型
key = {"info","work01"}
)
})
public void test (String message){
System,out.pring(mess)
}
}
动态路由。订阅模式
生产者
@springBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(StringRunner.class) //spirng 工厂
public class Test{
@Autowired
private RabbitTemplate rabbitTemplate;
// 生产者不会自动创建队列
@Test
public void test(){
// 交换机 rountingKye,Object
rabbitTemplate.convertAndSend("work","work.work","worl消息");
}
}
消费者
@Component
public class HelloCustomer{
//不用rabbitHandler 可以直接用RabbitListener 监听
@RabbitListener(bindings={
@QueuBinding(
//声明指定名称,不声明,创建临时队列@Queue("work")
value = @Queue, //临时队列
exchange=@Exchange(value = "work", type = "direct") //自定义交换机名称和类型
key = {"info","work01.*,work01.#"}
)
})
public void test (String message){
System,out.pring(mess)
}
}



