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(“work_queue”)
public Queue workQueue() {
return new Queue(MQConst.TEST_WORK_QUEUE, true);
}
}
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;/**
- 工作队列演示
- @return
*/
@ResponseBody
@RequestMapping(value = “/send/work/message”)
public Result sendWorkMessage() {
Result result = new Result();
for (int i = 1; i <= 10; i++) {
rabbitTemplate.convertAndSend(MQConst.TEST_WORK_QUEUE, String.valueOf(i));
}
result.setResultCode(“0000”);
result.setResultMessage(“send success”);
return result;
}
// @RabbitListener(queues = MQConst.TEST_WORK_QUEUE)
// public void recvWorkMessage1(String message) {
// try {
// System.out.println("recv[1] : " + message);
// Thread.sleep(2000);
// } catch (Exception e) {
// LOG.error(“recv[1] 工作抛出异常了…,{}”, e.getMessage());
// } finally {
// System.out.println(“recv[1]:done !”);
// }
// }
// @RabbitListener(queues = MQConst.TEST_WORK_QUEUE)
// public void recvWorkMessage2(String message) {
// try {
// System.out.println("recv[2] : " + message);
// Thread.sleep(1000);
// } catch (Exception e) {
// LOG.error(“recv[2] 工作抛出异常了…,{}”, e.getMessage());
// } finally {
// System.out.println(“recv[2]:done !”);
// }
// }
}
c、创建消息消费者
package com.atzhangwl.sboot.rabbitmq;
import com.atzhangwl.sboot.BaseConst.MQConst;
import com.atzhangwl.sboot.web.RabbitMqControllor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
-
@ClassName WorkQueueRecver
-
@Description
-
@Author zhangwl
-
@Date 2020/4/27 22:27
-
@Version 1.0
**/
@Component
public class WorkQueueRecver {private final Logger LOG = LoggerFactory.getLogger(RabbitMqControllor.class);
@RabbitListener(queuesToDeclare = @Queue(value = MQConst.TEST_WORK_QUEUE))
public void recvWorkMessage2(String message) {
try {
System.out.println("recv[2] : " + message);
Thread.sleep(1000);
} catch (Exception e) {
LOG.error(“recv[2] 工作抛出异常了…,{}”, e.getMessage());
} finally {
System.out.println(“recv[2]:done !”);
}
}@RabbitListener(queuesToDeclare = @Queue(value = MQConst.TEST_WORK_QUEUE))
public void recvWorkMessage1(String message) {
try {
System.out.println("recv[1] : " + message);
Thread.sleep(2000);
} catch (Exception e) {
LOG.error(“recv[1] 工作抛出异常了…,{}”, e.getMessage());
} finally {
System.out.println(“recv[1]:done !”);
}
}
}
运行结果:
recv[1] : 1
recv[2] : 2
recv[2]:done !
recv[2] : 4
recv[1]:done !
recv[1] : 3
recv[2]:done !
recv[2] : 6
recv[2]:done !
recv[2] : 8
recv[1]:done !
recv[1] : 5
recv[2]:done !
recv[2] : 10
recv[2]:done !
recv[1]:done !
recv[1] : 7
recv[1]:done !
recv[1] : 9
recv[1]:done !