1、配置文件 application.yml
#基本配置 server: port: 8080 spring: #mysql基本配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybase?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: 123456 #rabbitmq配置 rabbitmq: host: 192.168.42.131 port: 5672 username: dev password: dev virtual-host: /vhost_dev
2、代码实践:
a、配置消息队列 package com.atzhangwl.sboot.rabbitMqConf; import com.atzhangwl.sboot.BaseConst.MQConst; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @ClassName RabbitMqConf * @Description RabbitMq配置 * @Author zhangwl * @Date 2020/4/15 23:02 * @Version 1.0 **/ @Configuration public class RabbitMqConf { /** * 简单队列 * * @return */ @Bean(name = "simple_queue") public Queue defQueue() { return new Queue(MQConst.TEST_SIMPLE_QUEUE); } }
b、创建消息生产者
package com.atzhangwl.sboot.web; import com.atzhangwl.sboot.BaseConst.MQConst; import com.atzhangwl.sboot.req.MessageReq; import com.atzhangwl.sboot.res.Result; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * @ClassName RabbitMqControllor * @Description * @Author zhangwl * @Date 2020/4/17 21:52 * @Version 1.0 **/ @Controller public class RabbitMqControllor { private final Logger LOG = LoggerFactory.getLogger(RabbitMqControllor.class); @Autowired private RabbitTemplate rabbitTemplate; /** * 简单队列演示 * * @param messageReq * @return */ @ResponseBody @RequestMapping(value = "/send/simple/message", method = RequestMethod.POST, consumes = "application/json") public Result sendSimpleMessage(@RequestBody MessageReq messageReq) { Result result = new Result(); if (StringUtils.isEmpty(messageReq.getMessage())) { result.setResultCode("0001"); result.setResultMessage("请求参数不能为空"); return result; } rabbitTemplate.convertAndSend(MQConst.TEST_SIMPLE_QUEUE, messageReq.getMessage()); result.setResultCode("0000"); result.setResultMessage("发送消息成功 :" + messageReq.getMessage()); return result; } // @RabbitListener(queues = MQConst.TEST_SIMPLE_QUEUE) // public void recvSimpleMessage(String message) { // System.out.println("recv :" + message); // } }
c、创建消息消费者
package com.atzhangwl.sboot.rabbitmq; import com.atzhangwl.sboot.BaseConst.MQConst; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * @ClassName SimpleQueueRecver * @Description 默认值非持久化,自动删除 * @Author zhangwl * @Date 2020/4/27 21:56 * @Version 1.0 **/ @Component @RabbitListener(queuesToDeclare = @Queue(value = MQConst.TEST_SIMPLE_QUEUE, durable = "true", autoDelete = "true")) public class SimpleQueueRecver { @RabbitHandler public void recv(String message) { System.out.println("Simple Queue recv :" + message); } }
运行结果:
Simple Queue recv :Hi Simple Message...