Spring Cloud整合Thrift RPC(二) - 应用案例

本文提供了一套Spring Cloud整合Thrift RPC的银行存款、取款业务应用案例。介绍了Thrift IDL编写,服务端与客户端的实现,以及如何在现有技术框架中集成Thrift以提升性能和吞吐量。文章强调了技术选型的权衡和复杂性的增加。
摘要由CSDN通过智能技术生成

前言

上一篇简单的阐述了 spring-cloud-thrift-starter 这个插件的配置和使用,并引入了一个calculator的项目。本文将基于一个银行存款、取款的业务场景,给出一套thrift在生产环境的应用案例。

首先设计如下几张简单的数据库表:银行(bank)、分支(branch)、银行卡(deposit_card)、客户(customer)、存款历史纪录(deposit_history)、取款历史纪录(withdraw_history)。

12738336-44cb794b2969a9fb.png
image

正文

项目结构如下,依然是由三个模块组成:

12738336-eae8f03415b2ccf7.png
image
  • deposit
    • deposit-client
    • deposit-iface
    • deposit-server

Thrift IDL编写

关于 thrift更复杂的用法可以参考Apache Thrift基础学习系列,根据数据库表的设计编写 deposit.thrift

deposit.thrift定义了以下四个部分:命名空间 (namespace)、枚举类型 (enum)、结构类型 (struct)和服务类型 (service)。

(a). 命名空间 (namespace)

// 指定编译生成的源代码的包路径名称
namespace java com.icekredit.rpc.thrift.examples.thrift

(b). 枚举类型 (enum)

// 通过枚举定义银行分支所属区域
enum ThriftRegion {
   NORTH = 1,
   CENTRAL = 2,
   SOUTH = 3,
   EAST = 4,
   SOUTHWEST = 5,
   NORTHWEST = 6,
   NORTHEAST = 7
}

// 存款完成状态
enum ThriftDepositStatus {
   FINISHED = 1,
   PROCCEDING = 2,
   FAILED = 3
}

// 取款完成状态
enum ThriftWithdrawStatus {
   FINISHED = 1,
   PROCCEDING = 2,
   FAILED = 3
}

(c). 结构类型 (struct)

// 银行
struct ThriftBank {
   1: required i64 id,
   2: required string code,
   3: required string name,
   4: optional string description,
   5: optional map<ThriftRegion, list<ThriftBranch>> branches
}

// 银行分支
struct ThriftBranch {
   1: required i64 id,
   2: required string code,
   3: required string name,
   4: required string address,
   5: optional i32 staffs,
   6: optional ThriftBank bank,
   7: optional ThriftRegion region
}

// 客户
struct ThriftCustomer {
   1: required string IDNumber,
   2: required string name,
   3: required string birthday,
   4: required i32 sex = 0,
   5: required i32 age,
   6: optional list<string> address,
   7: optional set<ThriftDepositCard> depositCards
}

// 银行卡
struct ThriftDepositCard {
   1: required string id,
   2: required bool isVip,
   3: required string openingTime,
   4: required double accountBalance,
   5: optional double accountFlow,
   6: optional ThriftBranch branch,
   7: optional ThriftCustomer customer,
   8: optional list<ThriftDeposit> depositHistory,
   9: optional list<ThriftWithdraw> WithdrawHistory
}

// 存款历史纪录
struct ThriftDeposit {
   1: required string serialNumber,
   2: required double transactionAmount,
   3: required string submittedTime,
   4: optional string finishedTime,
   5: optional ThriftDepositStatus status,
   6: optional ThriftDepositCard depositCard
}

// 取款历史纪录
struct ThriftWithdraw {
   1: required string serialNumber,
   2: required double transactionAmount,
   3: required string submittedTime,
   4: optional string finishedTime,
   5: optional ThriftWithdrawStatus status,
   6: optional ThriftDepositCard depositCard
}

(d). 服务类型 (service)

// 银行 - 业务服务定义
service ThriftBankService {
   void registerNewBank(ThriftBank bank);
   list<ThriftBank> queryAllBanks();
   ThriftBank getBankById(i64 bankId);
   map<ThriftRegion, list<ThriftBranch>> queryAllBranchesByRegion(i64 bankId);
}

// 银行分支 - 业务服务定义
service ThriftBranchService {
   void addNewBranch(i64 bankId, ThriftBranch branch);
   list<ThriftBranch> queryAllBranches(i64 bankId);
   ThriftBranch getBranchById(i64 branchId);
}

// 客户 - 业务服务定义
service ThriftCustomerService {
   ThriftCustomer getCustomerById(string customerId);
   list<ThriftCustomer> queryAllCustomers();
   void addNewUser(ThriftCustomer customer);
   void modifyUserById(string customerId, ThriftCustomer customer);
   i32 getTotalDepositCard(string customerId);

}

// 银行卡 - 业务服务定义
service ThriftDepositCardService {
   set<ThriftDepositCard> queryAllDepositCards(string customerId);
   void addNewDepositCard(string customerId, ThriftDepositCard depositCard);
   ThriftDepositStatus depositMoney(string depositCardId, double money);
   ThriftWithdrawStatus withdrawMoney(string depositCardId, double money);
   list<ThriftDeposit> queryDepositHistorys(string depositCardId);
   list<ThriftWithdraw> queryWithdrawHistorys(string depositCardId);
}

进入src/main/thrift目录,编译生成所需的枚举类结构类业务服务类的源文件。

thrift -gen java ./deposit.thrift

所有生成的源文件都位于同一个命名空间(包)下面:com.icekredit.rpc.thrift.examples.thrift

12738336-17b4d1a8c20211b6.png
image

中间契约(deposit-iface)

上述源文件拷贝到 deposit-iface 模块中。

12738336-81a8046c3635afa9.png
image
<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值