This is an automated email from the ASF dual-hosted git repository.
wangxin pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo-website.git
The following commit(s) were added to refs/heads/asf-site by this push:
new 7aab9c0 add blog - How to use Fescar to ensure consistency between
Dubbo Microservices (#250)
7aab9c0 is described below
commit 7aab9c04d7391439a7debcf9ab77c02d61ee7b20
Author: jimin <[email protected]>
AuthorDate: Fri Jan 18 12:18:14 2019 +0800
add blog - How to use Fescar to ensure consistency between Dubbo
Microservices (#250)
* add doc How to use Fescar to ensure consistency between Dubbo
Microservices
* add doc How to use Fescar to ensure consistency between Dubbo
Microservices
* Delete dubbo-fescar.html
* Delete dubbo-fescar.json
* revert blog.json index.html
---
blog/en-us/dubbo-fescar.md | 225 +++++++++++++++++++++++++++++++++++++++++++
img/blog/fescar/fescar-1.png | Bin 0 -> 101828 bytes
img/blog/fescar/fescar-2.png | Bin 0 -> 324922 bytes
site_config/blog.js | 7 ++
4 files changed, 232 insertions(+)
diff --git a/blog/en-us/dubbo-fescar.md b/blog/en-us/dubbo-fescar.md
new file mode 100644
index 0000000..9f78746
--- /dev/null
+++ b/blog/en-us/dubbo-fescar.md
@@ -0,0 +1,225 @@
+---
+title: How to use Fescar to ensure consistency between Dubbo Microservices
+keywords: Dubbo,Fescar,Consistency
+description: This article will introduce you how to use Fescar to ensure
consistency between Dubbo Microservices.
+---
+# How to use Fescar to ensure consistency between Dubbo Microservices
+
+
+
+## Use case
+
+A business logic for user purchasing commodities. The whole business logic is
powered by 3 microservices:
+
+- Storage service: deduct storage count on given commodity.
+- Order service: create order according to purchase request.
+- Account service: debit the balance of user's account.
+
+### Architecture
+
+
+
+
+### StorageService
+
+```java
+public interface StorageService {
+
+ /**
+ * deduct storage count
+ */
+ void deduct(String commodityCode, int count);
+}
+```
+
+### OrderService
+
+```java
+public interface OrderService {
+
+ /**
+ * create order
+ */
+ Order create(String userId, String commodityCode, int orderCount);
+}
+```
+
+### AccountService
+
+```java
+public interface AccountService {
+
+ /**
+ * debit balance of user's account
+ */
+ void debit(String userId, int money);
+}
+```
+
+### Main business logic
+
+```java
+public class BusinessServiceImpl implements BusinessService {
+
+ private StorageService storageService;
+
+ private OrderService orderService;
+
+ /**
+ * purchase
+ */
+ public void purchase(String userId, String commodityCode, int orderCount) {
+
+ storageService.deduct(commodityCode, orderCount);
+
+ orderService.create(userId, commodityCode, orderCount);
+ }
+}
+```
+
+```java
+public class StorageServiceImpl implements StorageService {
+
+ private StorageDAO storageDAO;
+
+ @Override
+ public void deduct(String commodityCode, int count) {
+ Storage storage = new Storage();
+ storage.setCount(count);
+ storage.setCommodityCode(commodityCode);
+ storageDAO.update(storage);
+ }
+}
+```
+
+```java
+public class OrderServiceImpl implements OrderService {
+
+ private OrderDAO orderDAO;
+
+ private AccountService accountService;
+
+ public Order create(String userId, String commodityCode, int orderCount) {
+
+ int orderMoney = calculate(commodityCode, orderCount);
+
+ accountService.debit(userId, orderMoney);
+
+ Order order = new Order();
+ order.userId = userId;
+ order.commodityCode = commodityCode;
+ order.count = orderCount;
+ order.money = orderMoney;
+
+ return orderDAO.insert(order);
+ }
+}
+```
+
+## Distributed Transaction Solution with Fescar
+
+
+
+We just need an annotation `@GlobalTransactional` on business method:
+
+```java
+
+ @GlobalTransactional
+ public void purchase(String userId, String commodityCode, int orderCount) {
+ ......
+ }
+```
+
+## Example powered by Dubbo + Fescar
+
+### Step 1: Setup database
+
+- Requirement: MySQL with InnoDB engine.
+
+**Note:** In fact, there should be 3 database for the 3 services in the
example use case. However, we can just create one database and configure 3 data
sources for simple.
+
+Modify Spring XML with the database URL/username/password you just created.
+
+dubbo-account-service.xml
+dubbo-order-service.xml
+dubbo-storage-service.xml
+
+```xml
+ <property name="url" value="jdbc:mysql://x.x.x.x:3306/xxx" />
+ <property name="username" value="xxx" />
+ <property name="password" value="xxx" />
+```
+### Step 2: Create UNDO_LOG table for Fescar
+
+`UNDO_LOG` table is required by Fescar AT mode.
+
+```sql
+CREATE TABLE `undo_log` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT,
+ `branch_id` bigint(20) NOT NULL,
+ `xid` varchar(100) NOT NULL,
+ `rollback_info` longblob NOT NULL,
+ `log_status` int(11) NOT NULL,
+ `log_created` datetime NOT NULL,
+ `log_modified` datetime NOT NULL,
+ `ext` varchar(100) DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ KEY `idx_unionkey` (`xid`,`branch_id`)
+) ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8
+```
+
+### Step 3: Create tables for example business
+
+```sql
+
+DROP TABLE IF EXISTS `storage_tbl`;
+CREATE TABLE `storage_tbl` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `commodity_code` varchar(255) DEFAULT NULL,
+ `count` int(11) DEFAULT 0,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY (`commodity_code`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+
+DROP TABLE IF EXISTS `order_tbl`;
+CREATE TABLE `order_tbl` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `user_id` varchar(255) DEFAULT NULL,
+ `commodity_code` varchar(255) DEFAULT NULL,
+ `count` int(11) DEFAULT 0,
+ `money` int(11) DEFAULT 0,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+
+DROP TABLE IF EXISTS `account_tbl`;
+CREATE TABLE `account_tbl` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `user_id` varchar(255) DEFAULT NULL,
+ `money` int(11) DEFAULT 0,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+```
+### Step 4: Start Fescar-Server
+
+- Download server [package](https://github.com/alibaba/fescar/releases), unzip
it.
+- Start Fescar-Server
+
+```shell
+sh fescar-server.sh $LISTEN_PORT $PATH_FOR_PERSISTENT_DATA
+
+e.g.
+
+sh fescar-server.sh 8091 /home/admin/fescar/data/
+```
+
+### Step 5: Run example
+
+- Start AccountService
+- Start StorageService
+- Start OrderService
+- Run BusinessService for test
+
+[Related
code](https://github.com/alibaba/fescar/tree/develop/examples/src/main/java/com/alibaba/fescar/tm/dubbo)
+[learn more about Fescar](https://github.com/alibaba/fescar/)
\ No newline at end of file
diff --git a/img/blog/fescar/fescar-1.png b/img/blog/fescar/fescar-1.png
new file mode 100644
index 0000000..fa009f4
Binary files /dev/null and b/img/blog/fescar/fescar-1.png differ
diff --git a/img/blog/fescar/fescar-2.png b/img/blog/fescar/fescar-2.png
new file mode 100644
index 0000000..38b9a6c
Binary files /dev/null and b/img/blog/fescar/fescar-2.png differ
diff --git a/site_config/blog.js b/site_config/blog.js
index 8ef6cfa..b70bdb9 100644
--- a/site_config/blog.js
+++ b/site_config/blog.js
@@ -4,6 +4,13 @@ export default {
postsTitle: 'All posts',
list: [
{
+ title: 'How to use Fescar to ensure consistency between Dubbo
Microservices',
+ author: '@slievrly',
+ dateStr: 'Jan 17th, 2019',
+ desc: 'This blog describes details of using Fescar to ensure
consistency between Dubbo Microservices',
+ link: '/en-us/blog/dubbo-fescar.html',
+ },
+ {
title: 'Prepare an Apache Release',
author: 'Jun Liu',
dateStr: 'Sep 2nd, 2018',