Dubbo微服务影院系列(10):分布式事务

章节概要 事务简介 分布式事务的前世今生 分布式事务解决方案 主流分布式事务框架介绍 事务简介 事务是用来保证一组数据操作的完整性和一致性的 事务必须满足 ACID 的四大特性 事务具有四种隔离级别 事务具有七种传播行为 事务属性 原子性(Atomicity) 一致性(Consistency) 隔离型(Isolation) 持久性(Durability) 分布式事务 概念 分布式事务就是将多个节点的事务看成一个整体处理 分布式事务由事务参与者、资源服务器、事务管理器等组成 常见的分布式事务的例子:支付、下订单等 实现思路 两段式事务和三段式事务 基于 XA 的分布式事务 基于消息的最终一致性方案 TCC 编程式补偿性事务 分布式事务类型 两段式和三段式事务 两段式事务 三段式事务 基于 XA 的分布式事务 基于消息的一致性方案 TCC 补偿性事务 基于消息的一致性方案和 TCC 补偿性事务的区别 基于消息的事务是强一致性事务,会存在浪费 TCC 事务是柔性事务,在 try 阶段要对资源做预留 TCC 事务在确认或取消阶段释放资源 与基于消息的事务对比,TCC 的时效性更好 分布式事务框架 全局事务服务(Global Transaction Service,简称 GTS) 蚂蚁金服分布式事务(Distributed Transaction-eXtended,简称 DTX) 开源 TCC 框架(TCC-Transaction)(https://github.com/changmingxie/tcc-transaction) 开源 TCC 框架(ByteTCC)(https://github.com/liuyangming/ByteTCC) TCC-Transaction 分布式事务框架 Github 仓库:https://github.com/changmingxie/tcc-transaction ...

April 17, 2020 · 7 min

PAT甲级 1108 Finding Average (20分)

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N numbers are given in the next line, separated by one space. ...

April 15, 2020 · 3 min

PAT甲级 1072 Gas Station (30分)

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number. ...

April 13, 2020 · 4 min

PAT甲级 1018 Public Bike Management (30分)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well. ...

April 12, 2020 · 5 min

Dubbo微服务影院系列(9):Dubbo服务开发(支付模块开发)

章节概要 完成支付模块业务开发 Dubbo 特性学习:隐式参数、参数验证等 Dubbo 本地存根 本地存根类似于 Dubbo 的静态代理 Dubbo 会在客户端生成一个代理,处理部分业务 Stub 必须有可传入 Proxy 的构造函数 官方文档:https://dubbo.apache.org/zh-cn/docs/user/demos/local-stub.html 远程服务后,客户端通常只剩下接口,而实现全在服务器端,但提供方有些时候想在客户端也执行部分逻辑,比如:做 ThreadLocal 缓存,提前验证参数,调用失败后伪造容错数据等等,此时就需要在 API 中带上 Stub,客户端生成 Proxy 实例,会把 Proxy 通过构造函数传给 Stub,然后把 Stub 暴露给用户,Stub 可以决定要不要去调 Proxy。 在 spring 配置文件中按以下方式配置: <dubbo:service interface="com.foo.BarService" stub="true" /> 或 <dubbo:service interface="com.foo.BarService" stub="com.foo.BarServiceStub" /> 提供 Stub 的实现: package com.foo; public class BarServiceStub implements BarService { private final BarService barService; // 构造函数传入真正的远程代理对象 public BarServiceStub(BarService barService){ this.barService = barService; } public String sayHello(String name) { // 此代码在客户端执行, 你可以在客户端做ThreadLocal本地缓存,或预先验证参数是否合法,等等 try { return barService.sayHello(name); } catch (Exception e) { // 你可以容错,可以做任何AOP拦截事项 return "容错数据"; } } } Stub 必须有可传入 Proxy 的构造函数。 在 interface 旁边放一个 Stub 实现,它实现 BarService 接口,并有一个传入远程 BarService 实例的构造函数 Dubbo 本地伪装 本地伪装是本地存根的一个子集 通常会使用本地伪装处理服务降级 建议本地伪装在客户端实现 官方文档:https://dubbo.apache.org/zh-cn/docs/user/demos/local-mock.html ...

April 11, 2020 · 2 min

PAT考场 Dev C++ 5.10 配置

根据往年 (2019) 年浙大软件学院的考研复试公告,如果今年 (2020) 是线下复试,且考场没有变化的话,机试应该还是在 浙大紫金港校区(如下图)。 浙大软院招生通知:http://www.cst.zju.edu.cn/2019/0307/c36207a1478348/page.htm 从 PAT 官网可以找到紫金港的机房配置: PAT 考点联盟:https://www.patest.cn/location 我本人选择使用 Dev C++ 5.10 版本,考试那天在考场需要先对其作以下配置(当然也许已经配置好了): 打开产生调试信息 在 “工具(Tools)” -> “编译选项(Compiler Options)" 中: 点击 “代码生成/优化(Settings)” 选项卡中的 "连接器(Linker)" 选项卡,将 "产生调试信息(Generate debugging information)" 选项改为 “Yes”。 这样,就可以愉快的 Debug 了~ 添加 C++ 11 支持 由于 PAT 的 OJ 是支持 C++ 11 的,故最好打开 C++ 11 支持。 方法一(推荐) 在 “工具(Tools)” -> “编译选项(Compiler Options)" 中,“代码生成/优化(Settings)” 选项卡下的 “代码生成(Code Generation)” 选项卡中,将 “语言标准(Language Standard)(-std)” 设置为 ISO C++11 即可。 ...

April 11, 2020 · 1 min

PAT甲级 1021 Deepest Root (25分)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (≤10^4) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes’ numbers. ...

April 10, 2020 · 2 min

PAT甲级 1013 Battle Over Cities (25分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly. ...

April 9, 2020 · 2 min

PAT甲级 1032 Sharing (25分)

To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1. You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1). ...

April 7, 2020 · 2 min

PAT甲级 1056 Mice and Rice (25分)

Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse. First the playing order is randomly decided for N P programmers. Then every N G programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every N G winners are then grouped in the next match until a final winner is determined. ...

April 6, 2020 · 3 min