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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • hibernate作用_json.parsearray(string,class)

    hibernate作用_json.parsearray(string,class)Hibernate的query.uniqueResult();转换异常

    2022年9月30日
    0
  • PO模式简介「建议收藏」

    PO模式简介「建议收藏」PO模式简介1.什么是PO模式PO模型是:PageObjectModel的简写页面对象模型作用:就是把测试页面和测试脚本进行分离,即把页面封装成类,供测试脚本进行调用;分层机制,让不同层去做不同类型的事情,让代码结构清晰,增加复用性。PO设计模式是Selenium自动化测试中最佳的设计模式之一,主要体现在对界面交互细节的封装2.不使用PO设计会出现以下几种情况:复用性不太好,扩展性不好,易读性差,不好维护,UI界面频繁的项目维护起来比较麻烦。3.PO模式的优缺点优点:提高代

    2022年6月11日
    65
  • ConcurrentHashMap 1.7和1.8区别

    ConcurrentHashMap 1.7和1.8区别ConcurrentHashMap与HashMap和Hashtable最大的不同在于:put和get两次Hash到达指定的HashEntry,第一次hash到达Segment,第二次到达Segment里面的Entry,然后在遍历entry链表(1)从1.7到1.8版本,由于HashEntry从链表变成了红黑树所以concurrentHashMap的时间复杂度从O(n)到O…

    2022年6月24日
    25
  • JS转换HTML转义符,防止javascript注入攻击,亲测可用「建议收藏」

    JS转换HTML转义符,防止javascript注入攻击,亲测可用「建议收藏」//去掉html标签123function removeHtmlTab(tab){ return tab.replace(/]+?>/g,”);//删除所有HTML标签}//普通字符转换成转意符123fu

    2022年7月17日
    13
  • 【ElasticSearch面试】10道不得不会的ElasticSearch面试题[通俗易懂]

    【ElasticSearch面试】10道不得不会的ElasticSearch面试题[通俗易懂]以下是ElasticSearch面试题,相信大家都会有种及眼熟又陌生的感觉、看过可能在短暂的面试后又马上忘记了。JavaPub在这里整理这些容易忘记的重点知识及解答,建议收藏,经常温习查阅。评论区见关于es的面试,建议使用名词用官方语言描述会更准确。文章目录1.说说你们公司es的集群架构,索引数据大小,分片有多少,以及一些调优手段2.elasticsearch的倒排索引是什么3.elasticsearch是如何实现master选举的5.描述一下Elasticsearch索引

    2022年9月12日
    0
  • lookdiv激活码【2021最新】[通俗易懂]

    (lookdiv激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月26日
    226

发表回复

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

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