AmqpTemplate接口没有参数说明,不知道传啥参数,这是很难受,不知道怎么看参数的定义....
1、先来添加引用包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2、添加配置文件信息
spring: rabbitmq: host: 192.168.1.1 port: 5672 username: sa password: 123456 virtual-host: /
3、添加配置类信息
package com.example.springbootredis; import org.springframework.context.annotation.Configuration; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; @Configuration public class RabbitConfig { @Bean public Queue Queue() { return new Queue("hello"); } }
4、搞一个生产者
@Autowired private AmqpTemplate amqpTemplate; @GetMapping("/home/index") public String index() { amqpTemplate.convertAndSend("hello", "发送一些信息看看"); return "一个生产者"; }
5、搞一个消费者
@Autowired private AmqpTemplate amqpTemplate; @GetMapping("/home/home") public String home() throws Exception { Object aa = amqpTemplate.receiveAndConvert("hello"); return "一个消费者" + aa.toString(); }
注:网上教程大部分消费者都是通过添加一个监听注解来实现的,博客太多,我就不写了。