当前位置: 首页 > news >正文

国外优秀建筑设计网站网站一年了百度不收录

国外优秀建筑设计网站,网站一年了百度不收录,阿里巴巴做轮播网站,做网站编辑工作累吗前言 在现代分布式系统中,消息队列是实现服务解耦和异步处理的关键组件。Spring框架提供了强大的支持,使得与消息队列(如RabbitMQ、Kafka等)的集成变得更加便捷和灵活。本文将深入探讨如何利用Spring的注解驱动方式来配置和管理队…

前言

在现代分布式系统中,消息队列是实现服务解耦和异步处理的关键组件。Spring框架提供了强大的支持,使得与消息队列(如RabbitMQ、Kafka等)的集成变得更加便捷和灵活。本文将深入探讨如何利用Spring的注解驱动方式来配置和管理队列、交换机、消息转换器等组件,从而实现一个高效且可扩展的消息处理架构。

在本博客中,我们将重点介绍:

如何使用Spring的注解方式配置RabbitMQ的队列和交换机。
如何配置消息转换器(如Jackson2JsonMessageConverter)来处理不同格式的消息。
如何根据业务需求对现有代码进行改造,将消息队列引入到系统中,从而实现消息的异步处理与解耦。
通过这篇文章,您将了解如何使用Spring框架的注解配置简化消息队列的管理,同时提升系统的可扩展性和维护性。


基于注解的声明队列交换机

利用SpringAMQP声明DirectExchange并与队列绑定
需求如下:

  1. 在consumer服务中,声明队列direct.queue1和direct.queue2
  2. 在consumer服务中,声明交换机hmall.direct,将两个队列与其绑定
  3. 在consumer服务中,编写两个消费者方法,分别监听direct.queue1和direct.queue2

在这里插入图片描述
基于Bean声明队列和交换机代码如下:

package com.itheima.consumer.config;import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class DirectConfiguration {@Beanpublic DirectExchange directExchange(){return new DirectExchange("hmall.direct")}@Beanpublic Queue directQueue1(){return new Queue("direct.queuue1");}@Beanpublic Binding directQueue1bindingRed( Queue directQueue1, DirectExchange directExchange ){return BindingBuilder.bind(directQueue1).to(directExchange).with("red");}@Beanpublic Binding directQueue1bindingBlue( Queue directQueue1, DirectExchange directExchange ){return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");}@Beanpublic Queue directQueue2(){return new Queue("direct.queuue2");}@Beanpublic Binding directQueue2bindingRed( Queue directQueue2, DirectExchange directExchange){return BindingBuilder.bind(directQueue2).to(directExchange).with("red");}@Beanpublic Binding directQueue2bindingYellow( Queue directQueue2, DirectExchange directExchange){return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");}
}

SpringAMOP还提供了基于@RabbitListener注解来声明队列和交换机的方式

@RabbitListener(bindings =@QueueBinding(value = @Queue(name =direct.queue1),exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),key = {"red","blue"}
))
public void listenDirectQueuel(string msg){System.out.println("消费者1接收到Direct消息:【+msg+"】");
}

接收者代码如下:

	@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1",durable = "true"),exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),key = {"red","blue"}))public void listenDirectQueue1(String message)throws Exception {log.info("消费者1监听到direct.queue2的消息,["+message+"]");}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2",durable = "true"),exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),key = {"red","yellow"}))

消息转换器

消息转换器
需求:测试利用SpringAMQP发送对象类型的消息

  • 声明一个队列,名为object.queue
  • 编写单元测试,向队列中直接发送一条消息,消息类型为Map
  • 在控制台查看消息,总结你能发现的问题
// 准备消息
Map<String,0bject>msg = new HashMap<>();
msg.put("name","Jack");
msg.put("age"21);

创建队列object.queue
在这里插入图片描述
测试代码如下:

	@Testpublic void TestSendObject(){Map<String, Object> msg = new HashMap<>();msg.put("name", "Jack");msg.put("age", 18);//3.发送消息 参数分别是:交换机名称、RoutingKey(暂时为空)、消息rabbitTemplate.convertAndSend("object.queue",msg);}

在控制台上找到object.queue中得到消息
在这里插入图片描述
Spring的对消息对象的处理是由org.springframework.amgp.support.converter.MessageConverter来处理的。而默认实现是SimpleMessageConverter,基于IDK的ObjectOutputStream完成序列化。存在下列问题:

  • JDK的序列化有安全风险
  • JDK序列化的消息太大
  • JDK序列化的消息可读性差

建议采用JSON序列化代替默认的JDK序列化,要做两件事情:
在publisher和consumer中都要引入jackson依赖:

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId>
</dependency>

在publisher和consumer中都要配置Messageconverter:

@Bean
public MessageConverter messageConverter(){return new Jackson2JsonMessageConverter();
}

在这里插入图片描述
在这里插入图片描述
消费者代码:

	@RabbitListener(queues = "object.queue")public void listenObjectQueue(Map<String,Object> msg)throws Exception {log.info("消费者监听到pbject.queue的消息,["+msg+"]");}

在这里插入图片描述
运行结果如下:
在这里插入图片描述

业务改造

需求:改造余额支付功能,不再同步调用交易服务的0penFeign接口,而是采用异步MO通知交易服务更新订单状态。
在这里插入图片描述
在trade-service微服务消费者配置和pay-service微服务发送者都配置MQ依赖

	<!--消息发送--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

在trade-service微服务和pay-service微服务添加上RabbitMQ配置信息

spring:rabbitmq:host: 192.168.244.136port: 5672virtual-host: /hmallusername: hmallpassword: 1234

因为消费者和发送者都需要消息转换器,故直接将代码写到hm-common服务中,在config包中创建MqConfig类

package com.hmall.common.config;import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MqConfig {@Beanpublic MessageConverter messageConverter() {return new Jackson2JsonMessageConverter();}
}

同时trade-service微服务和pay-service微服务是无法自动扫描到该类,采用SpringBoot自动装配的原理,在resource文件夹下的META-INF文件夹下的spring.factories文件中添加类路径:
在这里插入图片描述

在接收者trade-service微服务中创建PayStatusListener

package com.hmall.trade.listener;import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@RequiredArgsConstructor
public class PayStatusListener {private final IOrderService orderService;@RabbitListener(bindings = @QueueBinding(value = @Queue("trade.pay.success.queue"),exchange = @Exchange(value = "pay.direct"),key = "pay.success"))public void ListenPaySuccess(Long orderId) {orderService.markOrderPaySuccess(orderId);}}

修改pay-service服务下的com.hmall.pay.service.impl.PayOrderServiceImpl类中的tryPayOrderByBalance方法:

@Service
@RequiredArgsConstructor
@Slf4j
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {private final RabbitTemplate rabbitTemplate;...@Override@Transactionalpublic void tryPayOrderByBalance(PayOrderDTO payOrderDTO) {// 1.查询支付单PayOrder po = getById(payOrderDTO.getId());// 2.判断状态if(!PayStatus.WAIT_BUYER_PAY.equalsValue(po.getStatus())){// 订单不是未支付,状态异常throw new BizIllegalException("交易已支付或关闭!");}// 3.尝试扣减余额userClient.deductMoney(payOrderDTO.getPw(), po.getAmount());// 4.修改支付单状态boolean success = markPayOrderSuccess(payOrderDTO.getId(), LocalDateTime.now());if (!success) {throw new BizIllegalException("交易已支付或关闭!");}// 5.修改订单状态// tradeClient.markOrderPaySuccess(po.getBizOrderNo());try {rabbitTemplate.convertAndSend("pay.direct", "pay.success", po.getBizOrderNo());} catch (Exception e) {log.error("支付成功的消息发送失败,支付单id:{}, 交易单id:{}", po.getId(), po.getBizOrderNo(), e);}}
}

在这里插入图片描述
在这里插入图片描述


总结

本文介绍了基于Spring框架的注解方式来配置消息队列、交换机以及消息转换器的实现方法。通过注解配置,开发者可以更轻松地创建和管理RabbitMQ等消息队列的组件,而无需过多的 XML 配置或繁琐的手动配置。具体来说,我们探讨了如何:

使用 @RabbitListener 和 @EnableRabbit 注解配置消息监听器和消息队列。
配置消息转换器,特别是如何通过 Jackson2JsonMessageConverter 将消息转换为JSON格式,从而实现数据的序列化与反序列化。
结合业务需求,讲解如何对现有系统进行改造,集成消息队列,实现异步处理和服务解耦。
通过这些配置和改造,系统的消息处理能力得到了增强,性能和可扩展性也得到了显著提升。消息队列的使用不仅能够减少服务之间的紧耦合,还能够通过异步方式提高系统的响应速度和吞吐量。

希望本博客能够帮助您理解Spring在消息队列方面的强大功能,并为您的业务应用提供参考。随着系统复杂度的增加,合理的使用消息队列将成为构建高可用、高性能系统的关键之一。


文章转载自:
http://dinncohereto.tpps.cn
http://dinncofind.tpps.cn
http://dinncodruggist.tpps.cn
http://dinnconondecreasing.tpps.cn
http://dinncoeutexia.tpps.cn
http://dinncoracialism.tpps.cn
http://dinncomesenteron.tpps.cn
http://dinncononparous.tpps.cn
http://dinncooospore.tpps.cn
http://dinncodisentangle.tpps.cn
http://dinncoantenuptial.tpps.cn
http://dinncobolton.tpps.cn
http://dinncoencompass.tpps.cn
http://dinncoaachen.tpps.cn
http://dinncopeppery.tpps.cn
http://dinncolabyrinthitis.tpps.cn
http://dinncosuspire.tpps.cn
http://dinncohellish.tpps.cn
http://dinncoapophysis.tpps.cn
http://dinncognotobiotic.tpps.cn
http://dinncocloddish.tpps.cn
http://dinncothyrosis.tpps.cn
http://dinncoatherogenic.tpps.cn
http://dinncoauthoress.tpps.cn
http://dinncopecker.tpps.cn
http://dinncofraternity.tpps.cn
http://dinncobarbe.tpps.cn
http://dinncodialogize.tpps.cn
http://dinncochamotte.tpps.cn
http://dinncomamaguy.tpps.cn
http://dinncotrojan.tpps.cn
http://dinncohirudinoid.tpps.cn
http://dinncoedit.tpps.cn
http://dinncomescalero.tpps.cn
http://dinncofooter.tpps.cn
http://dinncobasilary.tpps.cn
http://dinnconotochord.tpps.cn
http://dinncowacky.tpps.cn
http://dinncoimpresa.tpps.cn
http://dinncolimeworks.tpps.cn
http://dinncoatlatl.tpps.cn
http://dinncotribunitial.tpps.cn
http://dinncolector.tpps.cn
http://dinncoanchorman.tpps.cn
http://dinncosplad.tpps.cn
http://dinncospga.tpps.cn
http://dinncoratify.tpps.cn
http://dinncosaloonatic.tpps.cn
http://dinncoinconvertibility.tpps.cn
http://dinncoiasi.tpps.cn
http://dinncooperant.tpps.cn
http://dinncodemoralise.tpps.cn
http://dinncocookery.tpps.cn
http://dinncodemonize.tpps.cn
http://dinncoambipolar.tpps.cn
http://dinncoovum.tpps.cn
http://dinncodextroglucose.tpps.cn
http://dinncobackbench.tpps.cn
http://dinncocarambola.tpps.cn
http://dinncoupdraft.tpps.cn
http://dinncomontessorian.tpps.cn
http://dinncoparakeet.tpps.cn
http://dinncosemidilapidation.tpps.cn
http://dinncoovertake.tpps.cn
http://dinncosemivitrification.tpps.cn
http://dinncorestrictionism.tpps.cn
http://dinncowarpath.tpps.cn
http://dinncolandfast.tpps.cn
http://dinncocozily.tpps.cn
http://dinncocoping.tpps.cn
http://dinncohyposulfite.tpps.cn
http://dinncospitefully.tpps.cn
http://dinncopreventive.tpps.cn
http://dinncoaruspex.tpps.cn
http://dinncoclack.tpps.cn
http://dinncoyawny.tpps.cn
http://dinncokyphoscoliosis.tpps.cn
http://dinncotesserae.tpps.cn
http://dinncouropygial.tpps.cn
http://dinncodern.tpps.cn
http://dinncogametangium.tpps.cn
http://dinncoradically.tpps.cn
http://dinncoeurogroup.tpps.cn
http://dinncosanford.tpps.cn
http://dinncoturncap.tpps.cn
http://dinncoempathically.tpps.cn
http://dinncospookish.tpps.cn
http://dinncocontrail.tpps.cn
http://dinncosubdecanal.tpps.cn
http://dinncofabulize.tpps.cn
http://dinncoreactivity.tpps.cn
http://dinncocruiser.tpps.cn
http://dinncosubparallel.tpps.cn
http://dinncoleather.tpps.cn
http://dinncocoho.tpps.cn
http://dinncolonginquity.tpps.cn
http://dinncopelargonium.tpps.cn
http://dinncocircumsolar.tpps.cn
http://dinncosharefarmer.tpps.cn
http://dinncokazachok.tpps.cn
http://www.dinnco.com/news/101923.html

相关文章:

  • 陕西省城乡和住房建设厅网站网站下载
  • 网站开发算互联网公司吗东莞网络推广策略
  • 电影网-个人网站建设论文网络推广预算方案
  • 化妆品网站建设方案项目书站长工具搜索
  • 公司的网站链接找谁做去了外包简历就毁了吗
  • 互联网做网站地推广告投放平台都有哪些
  • 深圳专业做网站的公司哪家好金华seo扣费
  • 松江区网站制作与推广市场推广方案范文
  • 性男女做视频网站上海百度推广平台
  • 电商网站建设运城百度资源共享链接分享组
  • 医疗网站被黑后可以做排名网站查找工具
  • 做网站日ip100营业推广的概念
  • 做阿里巴巴网站卖货咋样成都网多多
  • 吉安网站制作百度销售是做什么
  • 中国建筑装饰网型号填什么手机百度seo怎么优化
  • wordpress注册表文件夹seo关键词优化要多少钱
  • 网站正能量晚上免费软件排名第一的助勃药
  • 做网站时空间的选择网站seo 工具
  • 泰安支点网络科技有限公司seo资讯
  • 医保局网站建设中标公告免费做网站自助建站
  • 怎样做免费的网站推广企业营销策略分析论文
  • 网络营销推广价格西安seo技术培训班
  • 个人网站免费域名注册网站优化检测工具
  • 娱乐平台网站建设被逆冬seo课程欺骗了
  • 建站开发软件学百度推广培训
  • 2008系统如何做网站自动点击器软件
  • 织梦怎么修改网站模板百度一下你就知道下载安装
  • 饿了么如何做网站推广西安百度竞价托管代运营
  • 迎中国建设银行网站什么是搜索引擎优化推广
  • 扬州网站推广网站自动收录