快速入门
Phoenix框架提供了一些列 Phoenix-Starter 可以帮助用户快速构建应用。
maven依赖
<dependency>
<groupId>com.iquantex</groupId>
<artifactId>phoenix-server-starter</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.iquantex</groupId>
<artifactId>phoenix-transaction</artifactId>
<version>2.4.0</version>
</dependency>
案例工程
phoenix事务聚合根可以对实体聚合根提供的TCC
和SAGA
接口灵活组装。该案例使用的是TCC+Saga
模式。更多的事务模式请参见:PhoenixIQ/phoenix-samples/shopping
购买商品是很常见的业务场景,一般涉及购买方账户扣减,以及商家库存扣减,和订单生成。该案例为了简化实现,不生成订单。
整个业务逻辑由2个聚合根(微服务)构成:
- 仓储服务: 对给定的商品扣除仓储数量。
- 账户服务: 从用户账户中扣除余额。
架构图
消息定义
聚合根接收Command产生Event,Command和Event类需要实现Serializable接口
command/event定义
Phoenix支持protostuff
和javaBean
协议进行序列化,可以通过以下配置进行指定,设定值分别为:proto_stuff
和java
quantex.phoenix.server.default-serializer: java
这里使用javaBean
序列化协议进行示范.
事务服务
@Getter
@AllArgsConstructor
public class BuyGoodsCmd implements Serializable {
private static final long serialVersionUID = -8667685124103764667L;
private String accountCode;
private String goodsCode;
private long qty;
private double price;
}
账户服务
@Getter
@AllArgsConstructor
public class AccountTryCmd implements Serializable {
private static final long serialVersionUID = 4778468915465985552L;
private String accountCode;
private double frozenAmt;
}
@Getter
@AllArgsConstructor
public class AccountConfirmCmd implements Serializable {
private static final long serialVersionUID = 6915539313674995272L;
private String accountCode;
private double frozenAmt;
}
@Getter
@AllArgsConstructor
public class AccountCancelCmd implements Serializable {
private static final long serialVersionUID = 3086192070311956483L;
private String accountCode;
private double frozenAmt;
}
@Getter
@Setter
@AllArgsConstructor
public class AccountTryOkEvent implements Serializable {
private static final long serialVersionUID = 1525408241428571363L;
private String accountCode;
private double frozenAmt;
}
@Getter
@Setter
@AllArgsConstructor
public class AccountTryFailEvent implements Serializable {
private static final long serialVersionUID = -8352616962272592136L;
private String accountCode;
private double frozenAmt;
private String remark;
}
@Getter
@Setter
@AllArgsConstructor
public class AccountConfirmOkEvent implements Serializable {
private static final long serialVersionUID = -6789245872360028227L;
private String accountCode;
private double frozenAmt;
}
@Getter
@Setter
@AllArgsConstructor
public class AccountCancelOkEvent implements Serializable {
private static final long serialVersionUID = -1017410771260579937L;
private String accountCode;
private double frozenAmt;
}