• SpringBoot简单配置RebbitMq(含源码)


    安装RebbitMq可以参考:https://blog.csdn.net/qq_47588845/article/details/107986373

    1.添加依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
                <version>1.5.14.RELEASE</version>
            </dependency>

    2.添加配置

    spring:
      #配置rabbitMq 服务器
      rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest

    3.添加配置类DirectRabbitConfig

    package com.llltony.springboot.utils;
    
    import org.apache.log4j.Logger;
    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.DirectExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * DirectRabbitConfig
     *
     * @author: lll
     * @date: 2021年04月11日 15:04:38
     */
    @Configuration
    public class DirectRabbitConfig {
    
    
    
        //队列 起名:EmployeeDirectQueue
        @Bean
        public Queue EmployeeDirectQueue() {
            // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
            // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
            // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
            //   return new Queue("EmployeeDirectQueue",true,true,false);
    
            //一般设置一下队列的持久化就好,其余两个就是默认false
            return new Queue("EmployeeDirectQueue",true);
        }
    
        //Direct交换机 起名:EmployeeDirectExchange
        @Bean
        DirectExchange EmployeeDirectExchange() {
            //  return new DirectExchange("EmployeeDirectExchange",true,true);
            return new DirectExchange("EmployeeDirectExchange",true,false);
        }
    
        //绑定  将队列和交换机绑定, 并设置用于匹配键:EmployeeDirectRouting
        @Bean
        Binding bindingDirect() {
            return BindingBuilder.bind(EmployeeDirectQueue()).to(EmployeeDirectExchange()).with("EmployeeDirectRouting");
        }
    
    
    
        @Bean
        DirectExchange lonelyDirectExchange() {
            return new DirectExchange("lonelyDirectExchange");
        }
    
    
    
    }

    4.添加监听类DirectReceiver

    package com.llltony.springboot.listener;
    
    import org.apache.log4j.Logger;
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    import java.util.Map;
    
    /**
     * 消费者队列监听
     *
     * @author: lll
     * @date: 2021年04月11日 16:04:14
     */
    @Component
    @RabbitListener(queues = "EmployeeDirectQueue")//监听的队列名称 EmployeeDirectQueue
    public class DirectReceiver {
    
        private static final Logger logger = Logger.getLogger(com.llltony.springboot.service.Impl.EmployeeServiceImpl.class);
    
        @RabbitHandler
        public void process(Map testMessage) {
            logger.info("DirectReceiver消费者收到消息  : " + testMessage.toString());
        }
    
    }

    5.添加调用

    package com.llltony.springboot.controller;
    
    
    import com.llltony.springboot.bean.Employee;
    import com.llltony.springboot.bean.EmployeeVo;
    import com.llltony.springboot.bean.ResultMap;
    import com.llltony.springboot.service.EmployeeService;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import javax.annotation.Resource;
    import java.io.IOException;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    @RestController
    @RequestMapping("/employee")
    public class EmployeeController {
    
        @Resource
        EmployeeService employeeService;
    
        @Resource
        RabbitTemplate rabbitTemplate;//通过消息队列异步增加学生
        @RequestMapping(value = "/addRebbitMq", method = RequestMethod.POST)
        public ResultMap addRebbitMq(@RequestBody EmployeeVo employeeVo) {
            ResultMap resultMap = new ResultMap();
            try {
                String messageId = String.valueOf(UUID.randomUUID());
                String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                Map<String, Object> map = new HashMap<>();
                map.put("messageId", messageId);
                map.put("messageData", employeeVo);
                map.put("createTime", createTime);
                //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
                rabbitTemplate.convertAndSend("EmployeeDirectExchange", "EmployeeDirectRouting", map);
    //            employeeService.saveEmp(employeeVo);
                resultMap.setStatus("200");
                resultMap.setMessage("保存成功");
            } catch (Exception e) {
                resultMap.setStatus("500");
                resultMap.setMessage("保存失败");
            }
            return resultMap;
        }
    }

    6.实际效果

     

     7.资源

    源码:https://github.com/CodingPandaLLL/tsl.git

    源码压缩包地址:https://codeload.github.com/CodingPandaLLL/tsl/zip/refs/tags/1.0.3

    作者:CodingPanda
    座中铭:润物细无声,功到自然成
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    centos从安装到环境配置
    PHP获取上个月、下个月、本月的日期
    js判断是哪种浏览器和阻止页面加载
    jquery中attr和prop的区别
    php curl 提交 总结
    合并二维数组,并计算重复字段的平均值
    检测端口和URL状态码判断启动服务
    curl-URL语法传输工具
    HAproxy 2.1.3源码 编译问题
    HAproxy 负载均衡器
  • 原文地址:https://www.cnblogs.com/LiLiliang/p/14644587.html
Copyright © 2020-2023  润新知