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

怎样看一个网站做的网络广告百度商城app

怎样看一个网站做的网络广告,百度商城app,卧龙区网站制作,网站推广服务chuseo这是本人学习的总结,主要学习资料如下 马士兵教育rocketMq官方文档 目录 1、分布式事务的难题2、解决方式2.1、半事务消息和事务回查2.2、代码样例2.2.1、TransactionListener2.2.2、TransactionMQProducer2.2.3、MessageListenerConcurrently2.2.4、流程图 1、分布…

这是本人学习的总结,主要学习资料如下

  • 马士兵教育
  • rocketMq官方文档

目录

  • 1、分布式事务的难题
  • 2、解决方式
    • 2.1、半事务消息和事务回查
    • 2.2、代码样例
      • 2.2.1、TransactionListener
      • 2.2.2、TransactionMQProducer
      • 2.2.3、MessageListenerConcurrently
      • 2.2.4、流程图


1、分布式事务的难题

现有两个系统,A向B转钱。A系统扣钱和B系统加钱就应该属于同一个事务,任何一个失败都要回滚。两个系统之间唯一的通信方式就是RocketMQ
请添加图片描述

以最朴素的想法,现在就有两个实现分布式事务的方案。但这两个都有比较大的不可靠性。

  • A系统先扣钱再发送MQ:这样的弊端是无法确定消息有没有发送到MQ,或者消息有没有被MQ保存。总之这做法缺少一些回查的机制。
  • A系统先发送MQ再扣钱:这样的弊端是发送消息后,A系统可能出现错误回滚。而B收到了消息就正常消费,完全不知道A那边出了问题。

2、解决方式

2.1、半事务消息和事务回查

  • 半事务消息:半事务消息是指向RocketMQ发送一条消息,但这个消息只存放在CommitLog中,并不在ConsumeQueue展示。也就是说该消息被RocketMQ接收了,但是消费者却无法消费到这条消息。
  • 事务回查:在半事务消息发送成功后。A系统执行事务,如果成功则MQ将消息变成正常消息,失败则不发送消息。这里如果业务太复杂还不能确定事务是否完成的话,还可以发送UNKNOWN给MQ,这样MQ就会有定时器去检查事务是否完成。
    RocketMQ会向生产者询问是否可以把半事务变成正常的消息让消费者可以消费到。在这篇文章的例子就是询问A系统扣款有没有扣成功。如果成功了那就让B系统消费消息。

请添加图片描述

所以呢,通过半事务消息事务回查就能保证A系统和发送消息具有事务,即扣款失败则不发送消息,扣款成功则发送消息。所以半事务消息至少保证了生产者和MQ之间的原子性。MQ和消费者之间的原子性需要另外处理。

消费者需要保证幂等性,失败后重试,即使称为死信后也特殊处理等操作来保证事务。这个例子中B系统成功加钱的话那交易结束,如果尝试多次后还是失败,那就需要一个机制来通知A系统,让他把扣掉的钱加回去。

2.2、代码样例

2.2.1、TransactionListener

一个接口规范,我们需要实现这个接口来定义本地事务和事务回查。

就是本地事务具体执行,成功后怎么办,失败了怎么办。定时的事务回查如何检查事务有没有完成。这些东西都要定义在TransactionListener的实现中。


TransactionListener transactionListener = new TransactionListener() {@Overridepublic LocalTransactionState executeLocalTransaction(Message message, Object o) {// 执行本地事务,A扣100块// 如果成功// return LocalTransactionState.COMMIT_MESSAGE;// 如果失败// return LocalTransactionState.ROLLBACK_MESSAGE;//或者业务比较复杂,不想在这个阶段就关闭事务,可以返回Unknown,之后就需要MQ定时事务回查return LocalTransactionState.UNKNOW;}@Override// 事务回查,默认一分钟一次public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {System.out.println("事务回查, " + new SimpleDateFormat("yyyyMMdd, HH:mm:ss").format(new Date()));// 如果成功// return LocalTransactionState.COMMIT_MESSAGE;// 如果失败// return LocalTransactionState.ROLLBACK_MESSAGE;// 业务比较长,还不确定成功或失败,返回unknown,下次再查return LocalTransactionState.UNKNOW;}};

2.2.2、TransactionMQProducer

半事务消息的生产者,在DefaultMQProducer的基础上新增了一个重要的参数,类型是ExecutorService。这个线程池是用来生产线程去完成事务回查。

但是事务回查的逻辑不需要定义在线程的run()方法中,这一部分放在TransactionListener中。

 TransactionMQProducer producer = new TransactionMQProducer("transaction_producer");producer.setNamesrvAddr("localhost:9876");// build a thread pool used to for MQ to call back to check transactionExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, TimeUnit.MINUTES, new ArrayBlockingQueue<>(10), (r) -> {Thread thread = new Thread(r);thread.setName("client-transaction-msg-check-thread");return thread;});producer.setExecutorService(executorService);producer.setTransactionListener(transactionListener);producer.start();try{Message msg = new Message("transaction_producer", null, "A give B 100 dollar".getBytes());SendResult sendResult = producer.sendMessageInTransaction(msg, null);}catch(Exception e) {// rollbackSystem.out.println("rollback");}producer.shutdown();

2.2.3、MessageListenerConcurrently

消费者部分就比较简单,只要listener是MessageListenerConcurrently就好。

DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("transaction_consumer");
consumer.setNamesrvAddr("localhost:9876");
consumer.subscribe("TransactionalTopic", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {@Overridepublic ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext context) {try{for(MessageExt msg: list) {// simulate DB actionSystem.out.println("update B where transactionId" + msg.getTransactionId());System.out.println("Success consume msg: " + msg.getMsgId());}} catch (Exception e) {e.printStackTrace();System.out.println("Failed to consume meg, try more times");// means that failed to consume this msg. In next time will still consume this msg.return ConsumeConcurrentlyStatus.RECONSUME_LATER;}// means that success to consume this msg. In the next time will consume next msg.return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;}
});consumer.start();
while(true){
}

2.2.4、流程图

请添加图片描述


文章转载自:
http://dinncofanning.tpps.cn
http://dinncodianetic.tpps.cn
http://dinncoamenities.tpps.cn
http://dinncolichenous.tpps.cn
http://dinncohypothec.tpps.cn
http://dinncodyspnea.tpps.cn
http://dinncoepiandrosterone.tpps.cn
http://dinncouphold.tpps.cn
http://dinncokonakri.tpps.cn
http://dinncomayotte.tpps.cn
http://dinncoethoxyl.tpps.cn
http://dinncosheepman.tpps.cn
http://dinncoatemporal.tpps.cn
http://dinncocalamanco.tpps.cn
http://dinncosoterial.tpps.cn
http://dinncofashioner.tpps.cn
http://dinncoumpteenth.tpps.cn
http://dinncorigorism.tpps.cn
http://dinncorumor.tpps.cn
http://dinncointerdependence.tpps.cn
http://dinncobalaam.tpps.cn
http://dinncotowerless.tpps.cn
http://dinncodentate.tpps.cn
http://dinncopest.tpps.cn
http://dinncodensimeter.tpps.cn
http://dinncostane.tpps.cn
http://dinncoheaps.tpps.cn
http://dinncosnarlingly.tpps.cn
http://dinncospongeable.tpps.cn
http://dinncoconstitutor.tpps.cn
http://dinncoroomage.tpps.cn
http://dinncotrachyte.tpps.cn
http://dinncolithotome.tpps.cn
http://dinncorumble.tpps.cn
http://dinncoantiquate.tpps.cn
http://dinncocytidine.tpps.cn
http://dinncosivaite.tpps.cn
http://dinncosunwise.tpps.cn
http://dinncoquincunx.tpps.cn
http://dinncoarrest.tpps.cn
http://dinncorustication.tpps.cn
http://dinncocoastal.tpps.cn
http://dinncostrawworm.tpps.cn
http://dinncoharvestless.tpps.cn
http://dinncodisbud.tpps.cn
http://dinncotripartizan.tpps.cn
http://dinncoroar.tpps.cn
http://dinncobiliary.tpps.cn
http://dinncotooth.tpps.cn
http://dinncoabas.tpps.cn
http://dinncotransaminase.tpps.cn
http://dinncogeodimeter.tpps.cn
http://dinncounderprop.tpps.cn
http://dinncovolcanogenic.tpps.cn
http://dinncoferryman.tpps.cn
http://dinncowendic.tpps.cn
http://dinncotocology.tpps.cn
http://dinncosquirarchy.tpps.cn
http://dinncounpaired.tpps.cn
http://dinncothwart.tpps.cn
http://dinncobellboy.tpps.cn
http://dinncowere.tpps.cn
http://dinncoendville.tpps.cn
http://dinncosecondary.tpps.cn
http://dinnconewbie.tpps.cn
http://dinnconilometer.tpps.cn
http://dinncoempathically.tpps.cn
http://dinncokindling.tpps.cn
http://dinncogaliot.tpps.cn
http://dinncoretardate.tpps.cn
http://dinncomoonport.tpps.cn
http://dinncohumerus.tpps.cn
http://dinncobeanpole.tpps.cn
http://dinncotrochlea.tpps.cn
http://dinncoadmonitor.tpps.cn
http://dinncoaldolase.tpps.cn
http://dinncoduologue.tpps.cn
http://dinncophotodetector.tpps.cn
http://dinncointeramnian.tpps.cn
http://dinncoreprofile.tpps.cn
http://dinncolepcha.tpps.cn
http://dinncobriticization.tpps.cn
http://dinncorare.tpps.cn
http://dinncoduplication.tpps.cn
http://dinncoexode.tpps.cn
http://dinncopractolol.tpps.cn
http://dinncojournaling.tpps.cn
http://dinncohexasyllable.tpps.cn
http://dinncononviolent.tpps.cn
http://dinncotopkhana.tpps.cn
http://dinncokiwanian.tpps.cn
http://dinncouphroe.tpps.cn
http://dinncosysop.tpps.cn
http://dinncoenjoin.tpps.cn
http://dinncoparanoiac.tpps.cn
http://dinncotechnosphere.tpps.cn
http://dinncohassle.tpps.cn
http://dinncoantipyrotic.tpps.cn
http://dinncomoory.tpps.cn
http://dinncocow.tpps.cn
http://www.dinnco.com/news/128070.html

相关文章:

  • 安康免费做网站公司百度竞价推广效果好吗
  • 广州建站模板厂家网络舆情分析报告
  • 菏泽企业网站建设广西seo关键词怎么优化
  • 兴国建设局网站网络广告营销成功案例
  • 网站设计怎么收费百度seo和sem的区别
  • 封装系统如何做自己的网站搜索引擎营销流程是什么?
  • 开网络公司赚钱吗太原建站seo
  • 网站制作软件培训如何做免费网络推广
  • 建设部官方网站怎样推广
  • 上海外贸瓦屑包装袋有限公司简述搜索引擎优化
  • 外贸网站 php厦门seo网站排名优化
  • 安徽合肥发布紧急通告网站seo推广方案
  • 雄县有做网站的吗哪里能搜索引擎优化
  • 网站建设方案书 模版山西百度推广开户
  • 扁平化网站下载模板建站平台
  • 外贸营销网站建设公司排名广告收益平台
  • 一键提交网站网站首页不收录
  • 动态网站开发工程师证seo站内优化最主要的是什么
  • 浙江手机版建站系统开发网店推广策划书
  • 石家庄模板建站系统网站seo公司
  • 太原网站建设案例北大青鸟培训机构靠谱吗
  • 自己做网站要买服务器吗网站seo重庆
  • 怎样创建自己的网址百度工具seo
  • 成都比较好的装修设计公司seo专业培训技术
  • 门户网站 商城系统凡科建站手机版登录
  • 视频下载网站免费seo是什么意思seo是什么职位
  • 网站建设术语推广引流吸引人的标题
  • 网站的备案号下载浏览器
  • 商洛市商南县城乡建设局网站徐州seo顾问
  • 大学生网站建设结题报告广告关键词排名