2021-07-28

Spring Boot整合RabbitMQ

Spring Boot整合RabbitMQ

目录
  • Spring Boot整合RabbitMQ
    • 写在开头
    • 整合流程
      • 配置文件
      • RabbitMQ配置
      • RabbitMQ生产者
      • RabbitMQ消费者
        • 手动应答简单工具类
    • 参考文档

写在开头

最近在搭一套基于SpringBoot的项目,用到了ssm+mysql+rabbitmq+redis。除了rabbitmq之外,其他几个都很快整合好了,唯独rabbitmq找了不少资料,才最终整合好,达到了预期。特此将过程记录下来,供参考。

整合流程

整合流程中的代码都为整合的关键配置及其使用。至于SpringBoot的基本配置,请参考Spring Boot Quick Start。

配置文件

  • pom.
<!-- rabbit-mq --><dependency>	<groupId>org.springframework.boot</groupId>	<artifactId>spring-boot-starter-amqp</artifactId></dependency>
  • application.yml
spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / listener:  simple:  acknowledge-mode: manual # 手动应答  concurrency: 5 # 消费端最小并发数  max-concurrency: 10 # 消费端最大并发数  prefetch: 5 # 一次请求中预处理的消息数量 cache:  channel:  size: 50 # 缓存的channel数量### 自定义配置mq: defaultExchange: amqpExchange # 默认交换器 queue: queue # 队列名 routeKey: queue_key # 路由key
  • MQProperties
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties(prefix = "mq")public class MQProperties { private String defaultExchange; private String routeKey; private String queue; public String getDefaultExchange() {  return defaultExchange; } public void setDefaultExchange(String defaultExchange) {  this.defaultExchange = defaultExchange; } public String getRouteKey() {  return routeKey; } public void setRouteKey(String routeKey) {  this.routeKey = routeKey; } public String getQueue() {  return queue; } public void setQueue(String queue) {  this.queue = queue; }}

RabbitMQ配置

import com.switchvov.rabbitmq.constant.MQProperties;import org.springframework.amqp.core.*;import org.springframework.amqp.rabbit.annotation.EnableRabbit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@EnableRabbitpublic class RabbitMQConfig { @Autowired private MQProperties mqProperties; @Bean public Queue queue() {  boolean durable = true;  boolean exclusive = false;  boolean autoDelete = false;  return new Queue(mqProperties.getQueue(), durable, exclusive, autoDelete); } @Bean public DirectExchange defaultExchange() {  boolean durable = true;  boolean autoDelete = false;  return new DirectExchange(mqProperties.getDefaultExchange(), durable, autoDelete); } @Bean public Binding binding() {  return BindingBuilder.bind(queue())    .to(defaultExchange())    .with(mqProperties.getRouteKey()); }}

RabbitMQ生产者

import com.switchvov.rabbitmq.constant.MQProperties;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class RabbitMQTest { @Autowired private RabbitTemplate rabbitTemplate; @Autowired private MQProperties mqProperties; @Test public void testSendMessage() {  rabbitTemplate.convertAndSend(mqProperties.getDefaultExchange(), 	  mqProperties.getRouteKey(), "发送了一条信息"); }}

RabbitMQ消费者

import com.switchvov.rabbitmq.common.RabbitMQUtils;import com.rabbitmq.client.Channel;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.amqp.support.AmqpHeaders;import org.springframework.messaging.handler.annotation.Header;import org.springframework.stereotype.Service;@Servicepublic class RabbitMQService { private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQService.class); @RabbitListener(queues = "${mq.queue}") public void receive(String payload, Channel channel,      @Header(AmqpHeaders.DELIVERY_TAG) long tag) {  LOGGER.info("消费内容为:{}", payload);  RabbitMQUtils.askMessage(channel, tag, LOGGER); }}

手动应答简单工具类

import com.rabbitmq.client.Channel;import org.slf4j.Logger;import java.io.IOException;public final class RabbitMQUtils {......

原文转载:http://www.shaoqun.com/a/901555.html

跨境电商:https://www.ikjzd.com/

zozotown:https://www.ikjzd.com/w/2180

贝恩投资公司:https://www.ikjzd.com/w/1336

代购公司:https://www.ikjzd.com/w/1982


SpringBoot整合RabbitMQ目录SpringBoot整合RabbitMQ写在开头整合流程配置文件RabbitMQ配置RabbitMQ生产者RabbitMQ消费者手动应答简单工具类参考文档写在开头最近在搭一套基于SpringBoot的项目,用到了ssm+mysql+rabbitmq+redis。除了rabbitmq之外,其他几个都很快整合好了,唯独rabbitmq找了不少资料,才最终整合
铭宣:https://www.ikjzd.com/w/1551.html
万色:https://www.ikjzd.com/w/2382
云游四方|乘坐"绝版"蒸汽小火车,驶向春天的花田:http://www.30bags.com/a/245698.html
云游四方|打卡荷兰"皇家"之城,乘坐海滩摩天轮看夕阳西下:http://www.30bags.com/a/246341.html
云游四方|古丝绸之路上的薰衣草田,比普罗旺斯更美:http://www.30bags.com/a/273353.html
云游四方|三月桃花与雪山、麦田、峡谷交织,只能在这里看到:http://www.30bags.com/a/245604.html
有好多人在下面㖭我 口述公么的粗大满足了我:http://lady.shaoqun.com/a/248395.html
我在做饭他在下添私密 我配合的张开双腿好深:http://www.30bags.com/m/a/249872.html
崩了!「解压玩具」侵权已"压垮"133家店铺,卖家快点撤离下架!:https://www.ikjzd.com/articles/146971
女人一旦出轨,通常会有这些变化。你遇到过他们吗?:http://lady.shaoqun.com/a/438500.html
男人晚上反应正常吗?一晚上几次比较好?看文章怎么说:http://lady.shaoqun.com/a/438501.html
你知道已婚女性出轨后有什么特点吗?:http://lady.shaoqun.com/a/438502.html

No comments:

Post a Comment