Springboot整合RabbitMQ详细讲解

Springboot整合RabbitMQ详细讲解搭建RabbitMQ环境Springboot整合RabbitMQ1、添加整合依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>2、配置application.yml提供者1)创建交换机和队列@Configurati

大家好,又见面了,我是你们的朋友全栈君。

搭建RabbitMQ环境

Springboot整合RabbitMQ

1、添加整合依赖

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

2、配置application.yml
在这里插入图片描述

提供者

1)创建交换机和队列

@Configuration     //一般放在启动类同目录下,由spring容器管理
public class RabbitMQConfiguration { 
   
    public static final String FANOUT_NAME="goods_fanoutexchange";
    private static final String QUEUE1="goods_queue1";
    private static final String QUEUE2="goods_queue2";

    /* * 声明队列 * */
    @Bean
    public Queue getQueue1(){ 
   
        return new Queue(QUEUE1);
    }

    /* * 声明队列 * */
    @Bean
    public Queue getQueue2(){ 
   
        return new Queue(QUEUE2);
    }

    /* * 声明交换机 * */
    @Bean
    public FanoutExchange getFanoutExchange(){ 
   
        return new FanoutExchange(FANOUT_NAME);
    }

    /* * 将队列和交换机绑定 * @Bean注解:将方法的返回值直接注入到spring的容器中,并能够有效的通过将另一个方法名作为参数,获得对应方法的返回值。 * */
    @Bean
    public Binding getBinding1(Queue getQueue1,FanoutExchange getFanoutExchange){ 
   
        return BindingBuilder.bind(getQueue1).to(getFanoutExchange);
    }

    @Bean
    public Binding getBinding2(Queue getQueue2,FanoutExchange getFanoutExchange){ 
   
        return BindingBuilder.bind(getQueue2).to(getFanoutExchange);
    }
}

2)注入rabbitmq模板对象,将消息放入队列中

@Service
public class GoodsServiceImpl implements IGoodsService { 
   

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Reference
    private ISearchService searchService;
    /* * 添加商品时: * 1、没有消息队列,通过调用搜索服务的方式,将商品信息同步到搜索库 * 2、通过消息队列,将消息放入队列中,搜索服务监听消息队列中的消息 * */
    @Override
    public int insertGoods(Goods goods) { 
   
        int result=goodsMapper.insert(goods);
        //通过dubbo调用搜索服务,同步索引库
        /*if (result>0){ searchService.insert(goods); }*/
        //将添加商品的信息放入rabbitmq中,必须保证实体类序列化且存在id值
        rabbitTemplate.convertAndSend(RabbitMQConfiguration.FANOUT_NAME,"",goods);
        return result;
    }
}

消费者1

——将商品信息添加到搜索库中

@Component
public class RabbitMQListener { 
   

    @Autowired
    private SolrClient solrClient;
    @RabbitListener(queues = "goods_queue1")
    public void handlerGoods(Goods goods){ 
   
        System.out.println("搜索工程消费消息");
        //接收mq中的消息
        SolrInputDocument solrInputDocument=new SolrInputDocument();
        solrInputDocument.addField("id",goods.getId());
        solrInputDocument.addField("gname",goods.getGname());
        solrInputDocument.addField("gimage",goods.getGimage());
        solrInputDocument.addField("ginfo",goods.getGinfo());
        solrInputDocument.addField("gsave",goods.getGsave());
        //不能传入BigDecimal类型
        solrInputDocument.addField("gprice",goods.getGprice().doubleValue());
        try { 
   
            solrClient.add(solrInputDocument);
            solrClient.commit();
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
   }
}

消费者2

——生成freemarker静态页面

@Component
public class RabbitMQListener { 
   

    @Autowired
    private Configuration configuration;

    @Autowired
    private HttpServletRequest request;

    @RabbitListener(queues = "goods_queue2")
    public void handlerGoodsItem(Goods goods){ 
   
        String [] images=goods.getGimage().split("\\|");
        //通过模板生成商品静态页面
        try { 
   
            System.out.println("详情工程消费消息");
            //获取商品详情的模板对象
            Template template = configuration.getTemplate("goodsItem.ftl");
            String contextPath="";
            try{ 
   
                //request.getContextPath()可以返回当前页面所在的应用的名字(部署的项目名);
                contextPath=request.getContextPath();
            }catch (Exception e){ 
   //利用catch捕获这个异常
                // No thread-bound request found: Are you referring to request attributes outside of an actual web request,
                // or processing a request outside of the originally receiving thread?
                // If you are actually operating within a web request and still receive this message,
                // your code is probably running outside of DispatcherServlet: In this case,
                // use RequestContextListener or RequestContextFilter to expose the current request.
                e.printStackTrace();
            }
            //准备商品数据
            Map<String,Object> map=new HashMap<>();
            map.put("goods",goods);
            map.put("context",contextPath);
            map.put("images",images);
            //生成静态页
            //获得classpath路径
            String path = this.getClass().getResource("/static/page/").getPath()+goods.getId()+".html";;
            template.process(map,new FileWriter(path));
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
    }
}

注意:通过监听触发的这个方法中,根据注入的request对象获取项目的根路径时异常。

补充

1、获取request对象
1)通过注解获取(推荐):

public class GoodsController{ 
   
    @Autowired 
    HttpServletRequest request; //这里可以获取到request
}

2)spring自带的方式:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

需要配置RequestContextListener监听
普通的web项目(含web.xml)

<listener> 
    <listener-class> 
        org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
</listener>

springboot项目(不含web.xml)

//启动类中
@Bean
public RequestContextListener requestContextListener(){ 
   
    return new RequestContextListener();
}

3)直接在参数中传递

public String getGoods(HttpServletRequest request)

2、对request.getContextPath()的理解:
——是在开发Web项目时,经常用到的方法,是为了解决相对路径的问题,可返回站点的根路径。
比如:要生成一个文件放在服务器上得一个目录下,可以使用request.getContextPath()+/dir,组成一个完整得目录结构!

当使用Tomcat作为Web服务器,项目一般部署在Tomcat下的webapps的目录下。具体来说主要用两种部署的路径:

(1)将web项目中的webRoot下的文件直接拷贝到webapps/ROOT下(删除ROOT下的原有文件);

(2)在Tomcat下的webapps中创建以项目名称命名(当然也可以用其他的名称)的文件夹,并将webRoot下的文件直接拷贝到该文件夹下。

对于第一部署方法,request.getContextPath()的返回值为空(即:””,中间无空格,注意区分null)。

对于第二部署方法,其返回值为:/创建的文件夹的名称。

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、 System.out.println(request.getContextPath());

打印结果:/news

2、System.out.println(request.getServletPath());

打印结果:/main/list.jsp

3、 System.out.println(request.getRequestURI());

打印结果:/news/main/list.jsp

4、 System.out.println(request.getRealPath(“/”));

打印结果:F:\Tomcat 6.0\webapps\news\test

request.getContextPath()可以返回当前页面所在的应用的名字;

request.getSchema()可以返回当前页面使用的协议,http 或是 https;

request.getServerName()可以返回当前页面所在的服务器的名字;

request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80;

实际应用中,一般用来解决jsp测试和生产环境路径不同的问题:
<%
String appContext = request.getContextPath();
String basePath =request.getScheme()+”: //”+request.getServerName()+”:”+request.getServerPort() + appContext;
%>
3、获取项目根路径的方式:
//获取类加载的根路径(/classes)
(1)this.getClass().getResource(“/”).getPath();
// 获取当前类的所在工程路径
this.getClass().getResource(“”).getPath();

//获取类加载的根路径(/classes)
(2)this.getClass().getClassLoader().getResource(“”);

//获取当前项目路径
(3)System.getProperty(“user.dir”);

//获取所有的类路径 包括jar包的路径
(4)System.getProperty(“java.class.path”);

//获取类加载的根路径(/classes)
(5)Thread.currentThread().getContentClassLoader().getResource(“”).getPath();

//表示到项目的根目录下, 要是想到目录下的子文件夹,修改”/“即可
(6)request.getSession().getServletContext().getRealPath(”/”);
写作不易,既然来了,不妨点个关注,点个赞吧!!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146335.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java使用多线程导出excel「建议收藏」

    java使用多线程导出excel「建议收藏」前言在一个业务中,需要将数据库的一张日志表导出到excel中做统计分析归类,由于单表的数据量特别大,发现在最终导出excel的时候,由于数量太大,导出速度特别慢,想了一些办法,不管使用何种API,单线程始终是操作的瓶颈,因此最终考虑使用多线程进行改善总体思路:1、数据总量分段2、每个线程处理不同分段的数据3、提交线程池下面来看具体的代码,为测试方便,这里直接使用一个测试接口进行调用,…

    2022年5月5日
    819
  • JVM、JRE和JDK的区别 及 环境变量的作用 什么是垃圾回收机制

    JVM、JRE和JDK的区别 及 环境变量的作用 什么是垃圾回收机制

    2021年9月29日
    59
  • 10240

    10240一个特殊的日子

    2022年6月9日
    38
  • vue.js和jquery的区别_人和人类的区别是什么

    vue.js和jquery的区别_人和人类的区别是什么jquery:曾经是前端最流行的js库。vue:是一个精简的MVVM,从技术角度讲。vue.js专注于MVVM模型的ViewModel层,它通过双向数据绑定把view和Model层连接起来,通过对数据的操作就可以完成对页面视图的渲染。vue和jQuery区别:①vue和jQuery对比jquery是使用选择器()选取DOM对象,对其进行赋值、取值、事件绑定等操作,其实和原生的HTML的区别只在于可以更方便的选取和操作DOM对象,而数据和界面是在一起的。②比如需要获取label标签的内..

    2022年10月15日
    4
  • 分享一个年化15%以上的无风险套利机会「建议收藏」

    分享一个年化15%以上的无风险套利机会「建议收藏」更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流。数字货币期现套利原理数字货币市场,是近期大家关注度相当高的一个市场。在这个市场中,存在着现货交易(数字

    2022年8月6日
    7
  • c语言编程void什么意思,c语言中void啥意思?

    c语言中,void的意思是“无类型”,相应的“void*”为无类型指针,常用在程序编写中对定义函数的参数类型、返回值、函数中指针类型进行声明,其作用是对函数返回和参数的进行限定。c语言中,void的意思是“无类型”,相应的“void*”为无类型指针,常用在程序编写中对定义函数的参数类型、返回值、函数中指针类型进行声明,其作用是对函数返回和参数的进行限定。voidvoid最常见的用法,就是在函数…

    2022年4月3日
    72

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号