1、添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2、添加配置
官方配置https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsinglespring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.virtual-host=/
3、guest用户默认是不支持远程连接的
操作:在/usr/local/rabbitmq/etc/rabbitmq下创建rabbitmq.conf 并写入[{rabbit, [{loopback_users, []}]}]. 重启rabbitmq http://www.rabbitmq.com/access-control.html
4、简单例子(direct模式)
4.1、ioc注入
@Configuration public class MQConfig { public static final String QUEUE = "queue"; @Bean public Queue queue() { return new Queue(QUEUE, true); } }
4.2、发送者
@Service public class MQSender { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private AmqpTemplate amqpTemplate; public void send(String message){ logger.info("send message:{}",message); amqpTemplate.convertAndSend(MQConfig.QUEUE,message); } }
4.3、接收者
@Service public class MQReceiver { private Logger logger = LoggerFactory.getLogger(getClass()); @RabbitListener(queues = MQConfig.QUEUE) public void receiver(String message){ logger.info("receiver message:{}",message); } }
标签:集成,logger,SpringBoot,spring,rabbitmq,QUEUE,RabbitMQ,message,public From: https://www.cnblogs.com/anquing/p/17638100.html