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(”/”);
写作不易,既然来了,不妨点个关注,点个赞吧!!!

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • UNIX命令

    UNIX命令 一、UNIX命令格式1、UNIX命令提示符   在命令行下,操作系统会显示一提示符,提示用户在此提示符后可以输入一行命令。不同的Shell有不同的缺省提示符:       BShell和KShell的缺省提示符为”$”;       CShell的缺省提示符为”%”;       但当以root用户登录时,系统提示符统一缺省为”#”。用户可以更改自己的缺省Shell和提

  • java uninstall tool_java卸载工具|java卸载工具(JavaUninstallTool)下载v1.1.0.0 – 欧普软件下载…「建议收藏」

    java uninstall tool_java卸载工具|java卸载工具(JavaUninstallTool)下载v1.1.0.0 – 欧普软件下载…「建议收藏」java卸载工具(JavaUninstallTool)是甲骨文官方发布的一款专门用于卸载JAVA软件的工具,能够非常方便快捷以及彻底的卸载掉JAVA,使用很简单,只需要解压缩即可使用,赶快下载使用吧!软件功能1、检测到的Java版本将向用户显示以供删除2、用户可以选择删除全部版本,也可以选择删除特定的Java版本3、适用于Windows操作系统4、检测并允许删除Java版本1.4…

  • 微信公众平台开发(46)在线电影/移动影院

    微信公众平台开发(46)在线电影/移动影院微信公众平台开发微信公众平台开发模式企业微信公众平台在线电影移动影院微信电影移动电影作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/weixi

  • 面试手写代码的经验分享

    面试手写代码的经验分享前言今天分享一下自己面试的时候遇到的写面试题的经验分享。在面试的时候,分为视频面试和现场面试,接下来讲解着两部分。视频面试在我和我周围的同学参加过的公司里面,不在现场的面…

  • 最小设计流量怎么计算_动态限流算法

    最小设计流量怎么计算_动态限流算法给定一个包含 n 个点 m 条边的有向图,并给定每条边的容量和费用,边的容量非负。图中可能存在重边和自环,保证费用不会存在负环。求从 S 到 T 的最大流,以及在流量最大时的最小费用。输入格式第一行包含四个整数 n,m,S,T。接下来 m 行,每行三个整数 u,v,c,w,表示从点 u 到点 v 存在一条有向边,容量为 c,费用为 w。点的编号从 1 到 n。输出格式输出点 S 到点 T 的最大流和流量最大时的最小费用。如果从点 S 无法到达点 T 则输出 0 0。数据范围2≤n≤50

  • web是什么?_怎么进入web界面

    web是什么?_怎么进入web界面我也是从接触Web这个词开始就一直搞不清和Internet之间是什么关系。说说我理解的Web,可能很多地方不准确.Web就是在Http协议基础之上,利用浏览器进行访问的网站.WebPage指

发表回复

您的电子邮箱地址不会被公开。

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