RabbitMq 快速与springboot整合

阅读: 评论:0

RabbitMq 快速与springboot整合

RabbitMq 快速与springboot整合

RabbitMq 快速与springboot整合

1.引入依赖

		<!--RabbitMQ的依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

2.配置 l

#rabbitMqrabbitmq:#服务器host: 116.62.245.209#用户名username: guest#密码password: guest#虚拟主机virtual-host: /#端口port: 5672listener:simple:#消费者最小数量concurrency: 10#消费者最大数量max-concurrency: 10#限制消费者每次只处理一条消息,处理完再继续下一条消息prefetch: 1#启动时是否默认启动容器,默认trueauto-startup: true#被拒绝时重新进入队列default-requeue-rejected: truetemplate:retry:#发布重试,默认falseenabled: true#重试时间,默认1000msinitial-interval: 1000ms#重试最大次数,默认3次max-attempts: 3#重试最大间隔时间,默认10000msmax-interval: 10000ms#重试的间隔乘数。比如配2.0,第一次就等10s,第二次就等20s,第三次就等40smultiplier: 1

3.配置RabbitMQ的config配置文件

fig;import org.Queue;import t.annotation.Bean;import t.annotation.Configuration;/***@Description:  RabbitMQ配置类*@Parameter: *@Return: *@Author: jxj*@Date: 2022/7/29**/@Configurationpublic class RabbitMQConfigChushi {@Beanpublic Queue queue(){return new Queue("jxj",true);}}

4.创建一个层级目录 发送消息

seckill.rabbitmq;slf4j.Slf4j;
import org.springframework.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @ClassName -> 消息发送者* @Description* @Author JXJ* @Date 2022/7/29 13:39 星期五* @Version 1.0*/
@Service
@Slf4j
public class MQSender {@Autowiredprivate RabbitTemplate rabbitTemplate;public void senderMsg(Object msg){log.info("发送的数据:"+msg);vertAndSend("jxj",msg);}

5.在4建的目录下创建一个消息消费类

seckill.rabbitmq;import ditions.query.QueryWrapper;
ity.SeckillOrder;
ity.User;
seckill.service.impl.GoodsServiceImpl;
seckill.service.impl.OrderServiceImpl;
seckill.util.JsonUtil;
seckill.vo.GoodsVo;
seckill.vo.SeckillMessage;
slf4j.Slf4j;
import org.apachemons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.RedisTemplate;
import org.springframework.stereotype.Service;/*** @ClassName -> 消费者* @Description* @Author JXJ* @Date 2022/7/29 13:39 星期五* @Version 1.0*/
@Service
@Slf4j
public class MQReceiver {@Autowiredprivate RabbitTemplate rabbitTemplate;@RabbitListener(queues = "jxj")public void recevie(Object msg){log.info("接收数据:"+msg);}

6.创建RabbitMQController控制层类

ller;seckill.rabbitmq.MQSender;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** @ClassName -> RabbitMqController* @Description* @Author JXJ* @Date 2022/7/29 13:46 星期五* @Version 1.0*/
@Controller
@RequestMapping("/rabbitMq")
@Slf4j
public class RabbitMqController {@Autowiredprivate MQSender mqSender;/*** rabiitmq 与springboot整合demo*/@GetMapping("/mq")@ResponseBodypublic void mq(){mqSender.senderMsg("hello 李亚文");}

结果

这个是就简单的整合,下次我们一起学习 fanout、topic、direct、heade几种消费模式,其实我们工作中最常用的是topic

本文发布于:2024-02-01 21:56:53,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170679581539671.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:快速   RabbitMq   springboot
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23