栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RabbitMq 快速与springboot整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RabbitMq 快速与springboot整合

RabbitMq 快速与springboot整合

1.引入依赖

		
 		
            org.springframework.boot
            spring-boot-starter-amqp
        

2.配置 application.yml

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

3.配置RabbitMQ的config配置文件

package com.xxxx.seckill.config;

 import org.springframework.amqp.core.Queue;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;

 
 @Configuration
 public class RabbitMQConfigChushi {

 	@Bean
	 public Queue queue(){
 		return new Queue("jxj",true);
	}
 }

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

package com.xxxx.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class MQSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;
	 public void senderMsg(Object msg){
        log.info("发送的数据:"+msg);
 		rabbitTemplate.convertAndSend("jxj",msg);
 	}

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

package com.xxxx.seckill.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.seckill.entity.SeckillOrder;
import com.xxxx.seckill.entity.User;
import com.xxxx.seckill.service.impl.GoodsServiceImpl;
import com.xxxx.seckill.service.impl.OrderServiceImpl;
import com.xxxx.seckill.util.JsonUtil;
import com.xxxx.seckill.vo.GoodsVo;
import com.xxxx.seckill.vo.SeckillMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class MQReceiver {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RabbitListener(queues = "jxj")
    public void recevie(Object msg){
       log.info("接收数据:"+msg);
    }

6.创建RabbitMQController控制层类

package com.xxxx.seckill.controller;

import com.xxxx.seckill.rabbitmq.MQSender;
import lombok.extern.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;


@Controller
@RequestMapping("/rabbitMq")
@Slf4j
public class RabbitMqController {
    @Autowired
    private MQSender mqSender;

    
    @GetMapping("/mq")
    @ResponseBody
    public void mq(){
    mqSender.senderMsg("hello 李亚文");
    }

结果

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032337.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号