接着看下dubbo模式的源码,它的实现和api其实很相似,只不过从单体服务变成了4个微服务,首先看下三个RM的实现,基本没有变化,比如Account
public void debit(String userId, int money) {
LOGGER.info("Account Service ... xid: " + RootContext.getXID());
LOGGER.info("Deducting balance SQL: update account_tbl set money = money - {} where user_id = {}", money,
userId);
jdbcTemplate.update("update account_tbl set money = money - ? where user_id = ?", new Object[] {money, userId});
LOGGER.info("Account Service End ... ");
}
复制
order和stock也类似就不介绍了。TM抽象出了一个BusinessServiceImpl,它实现的区别是什么呢?去掉了申请全局事务id,然后通过try cache判断是否出错来决定提交事务还是回滚事务,多了一个 @GlobalTransactional注解
@Override
@GlobalTransactional(timeoutMills = 300000, name = "dubbo-demo-tx")
public void purchase(String userId, String commodityCode, int orderCount) {
LOGGER.info("purchase begin ... xid: " + RootContext.getXID());
stockService.deduct(commodityCode, orderCount);
// just test batch update
//stockService.batchDeduct(commodityCode, orderCount);
orderService.create(userId, commodityCode, orderCount);
if (random.nextBoolean()) {
throw new RuntimeException("random exception mock!");
}
}
复制
相应的,我们到,src/main/resources/spring/dubbo-business.xml里面可以看到,它除了使用dubbo协议暴露服务外
<dubbo:application name="dubbo-demo-app">
<dubbo:parameter key="qos.enable" value="false"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false"/>
复制
还加入了一个bean,通过这个bean完成注解的扫描,通过注解实现动态代理,添加事务执行和回滚对应的切片代码,降低了代码的复杂性。
<bean class="io.seata.spring.annotation.GlobalTransactionScanner">
<constructor-arg value="dubbo-demo-app"/>
<constructor-arg value="my_test_tx_group"/>
</bean>
复制
除了上述代码外,还实现了几个对应的starter,开启一个新的线程,等待退出之前来清理资源
public static void main(String[] args) {
/**
* 3. Order service is ready . Waiting for buyers to order
*/
ClassPathXmlApplicationContext orderContext = new ClassPathXmlApplicationContext(
new String[] {"spring/dubbo-order-service.xml"});
orderContext.getBean("service");
new ApplicationKeeper(orderContext).keep();
}
复制
在keeper里面注册了shutDown的hook方法
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
复制
然后在我们的testClient里面触发执行我们的业务。
public static void main(String[] args) {
/**
* 4. The whole e-commerce platform is ready , The buyer(U100001) create an order on the sku(C00321) , the
* count is 2
*/
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"spring/dubbo-business.xml"});
final BusinessService business = (BusinessService)context.getBean("business");
business.purchase("U100001", "C00321", 200);
}
复制
文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
缓存监控治理在游戏业务的实践和探索
vivo互联网技术
35次阅读
2025-03-20 09:51:10
Worker模块源码实战:万字长文解析DolphinScheduler如何实现亿级任务调度
海豚调度
33次阅读
2025-03-04 09:47:24
Java的SPI机制详解
京东云开发者
27次阅读
2025-03-05 11:47:12
数仓建模:基于OTD流程的订单履约分析
会飞的一十六
27次阅读
2025-03-03 09:53:12
C#操作SQLite数据库
淡定
24次阅读
2025-03-25 23:27:25
What's new in dubbo-go v3.3.0
PikiwiDB
17次阅读
2025-03-23 23:10:58
从零构建企业级财务分析数仓 | Hive建模实战
会飞的一十六
15次阅读
2025-03-04 09:47:15
如何使用 Redis完成 PV,UV 统计?
猿java
12次阅读
2025-03-04 08:02:34
ASP.NET Core + WebAPI + EF Core 三层架构快速入门指南【AI编程源码仅售¥9.99】
跟着阿笨一起玩NET
12次阅读
2025-03-06 09:26:55
TDengine+MQTT:实现物联网设备数据采集
老王两点中
9次阅读
2025-03-10 09:00:43