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


相关推荐

  • Unrecognized Windows Sockets error: 0: JVM_Bind

    Unrecognized Windows Sockets error: 0: JVM_BindUnrecognized Windows Sockets error: 0: JVM_Bind

    2022年4月25日
    38
  • 学生成绩管理系统(C语言)「建议收藏」

    学生成绩管理系统(C语言)「建议收藏」思路学生成绩管理系统,首先要初始化系统,开始一个新的学生成绩系统初始化记录学生姓名,学号,院系,然后输入学生各科成绩,数学,英语,语文成绩。记录完各课成绩以后,可查看学生平均成绩和是否及格,成绩查询其中有学号查询,姓名查询,院系查询,还有全部输出,可以清晰的看到及格人数,按照分数高低排列,最后还可以添加和删除学生成绩,或者更改学生成绩,避免人为录入成绩错误。基本函数1.结构体structStudent定义一个结构体struct,结构体的关键字,students定义的结构体对象。学…

    2022年6月20日
    33
  • 软件设计之——“高内聚低耦合”

    软件设计之——“高内聚低耦合”

    2021年10月10日
    96
  • 万洲金业平台上炒黄金亏损了怎么办?「建议收藏」

    万洲金业平台上炒黄金亏损了怎么办?「建议收藏」  由于受国际行情变化影响,黄金市场很难长时间维持单边走势,因此金价起伏波动不断才是正确的打开方式。尽管黄金价格不断变化为人们营造了良好的盈利空间,但对于大多数人来说,尽管亏损是难以避免的,但真当风险来临,还是难以接受。所以今天就详细介绍一下当人们在万洲金业平台上发生了炒金亏损之后应该怎么办。万洲金业是一家专业的黄金交易平台,为人们提供了极为周到的黄金投资服务,也借助良好的市场表现成为了不少人的炒金选择。即便如此也不能代表平台客户不会发生黄金投资亏损。  在万洲金业平台上炒黄金,一旦发生了交易亏损,

    2022年6月15日
    83
  • 2021 idea 激活码(JetBrains全家桶)

    (2021 idea 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWN…

    2022年3月21日
    83
  • 砸盘、销号、解散社群,Merlin Lab“跑路三连”暴露了DeFi哪些问题?

    砸盘、销号、解散社群,Merlin Lab“跑路三连”暴露了DeFi哪些问题?抛售代币、注销推特、微信群解散,昨夜BSC机枪池项目MerlinLab上演一出火速“大逃亡”。6月29日15点24分,MerlinLab遭到黑客攻击。据区块链安全公司PeckShield…

    2022年5月19日
    44

发表回复

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

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