Skip to content

Commit 5b82373

Browse files
authored
Merge pull request #73 from QNJR-GROUP/1.x
demo: add rpc dubbo demo
2 parents 90f34e0 + 2126921 commit 5b82373

24 files changed

Lines changed: 987 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
server_back.properties
2+
*.class
3+
*.log
4+
*.log.*
5+
.factorypath
6+
/target/*
7+
/.settings/*
8+
/bin/*
9+
/.project
10+
/.classpath
11+
/logs
12+
/target/
13+
.DS_Store

easytrans-demo/rpc-dubbo/pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>1.5.13.RELEASE</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<groupId>com.yiqiniu.easytrans.demos</groupId>
13+
<artifactId>rpcdubbo</artifactId>
14+
<version>${revision}</version>
15+
<packaging>pom</packaging>
16+
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
<revision>1.0.0</revision>
23+
</properties>
24+
25+
26+
<modules>
27+
<module>rpcdubbo-wallet-api</module>
28+
<module>rpcdubbo-wallet-service</module>
29+
<module>rpcdubbo-order-service</module>
30+
</modules>
31+
32+
<dependencyManagement>
33+
<dependencies>
34+
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>Dalston.SR5</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.yiqiniu.easytrans</groupId>
45+
<artifactId>easytrans</artifactId>
46+
<version>${revision}</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>com.yiqiniu.easytrans.demos</groupId>
53+
<artifactId>rpcdubbo-wallet-api</artifactId>
54+
<version>${revision}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.yiqiniu.easytrans.demos</groupId>
58+
<artifactId>rpcdubbo-wallet-service</artifactId>
59+
<version>${revision}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.yiqiniu.easytrans.demos</groupId>
63+
<artifactId>rpcdubbo-order-service</artifactId>
64+
<version>${revision}</version>
65+
</dependency>
66+
</dependencies>
67+
</dependencyManagement>
68+
69+
</project>

easytrans-demo/rpc-dubbo/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# English
2+
## rpc dubbo
3+
this demo show how to replace RPC implement with Alibaba Dubbo
4+
5+
to run this demo, you will need zookeeper and mysql,change the configuration in applicaiton.yml,you can start the services
6+
7+
8+
# 中文
9+
## rpc dubbo
10+
本demo只演示了在本框架中改用DUBBO RPC实现
11+
12+
本demo运行起来需要zk以及关系数据库,修改applicaiton.yml文件里相关zk及数据库配置后,即可启动。
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server_back.properties
2+
*.class
3+
*.log
4+
*.log.*
5+
.factorypath
6+
/target/*
7+
/.settings/*
8+
/bin/*
9+
/.project
10+
/.classpath
11+
/logs
12+
/target/
13+
.DS_Store
14+
.springBeans
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>rpcdubbo-order-service</artifactId>
7+
8+
<parent>
9+
<groupId>com.yiqiniu.easytrans.demos</groupId>
10+
<artifactId>rpcdubbo</artifactId>
11+
<version>${revision}</version>
12+
<relativePath>../pom.xml</relativePath>
13+
</parent>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.yiqiniu.easytrans</groupId>
29+
<artifactId>easytrans-starter</artifactId>
30+
<exclusions>
31+
<!-- did not use queue,so exclusive it,then it won't be initialed -->
32+
<!-- 没有使用队列,因此将其排除,否则将会尝试初始化 -->
33+
<exclusion>
34+
<groupId>com.yiqiniu.easytrans</groupId>
35+
<artifactId>easytrans-queue-kafka-starter</artifactId>
36+
</exclusion>
37+
<!-- did not use netflix-ribbon,so exclusive it,then it won't be initialed -->
38+
<!-- 不适用Netflix ribbon,所以将其移除 -->
39+
<exclusion>
40+
<groupId>com.yiqiniu.easytrans</groupId>
41+
<artifactId>easytrans-rpc-rest-ribbon-starter</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.yiqiniu.easytrans</groupId>
48+
<artifactId>easytrans-rpc-dubbo-starter</artifactId>
49+
<exclusions>
50+
<!-- dubbo import a very old version spring , exclude it -->
51+
<exclusion>
52+
<groupId>org.springframework</groupId>
53+
<artifactId>spring</artifactId>
54+
</exclusion>
55+
</exclusions>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>com.yiqiniu.easytrans.demos</groupId>
60+
<artifactId>rpcdubbo-wallet-api</artifactId>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-jdbc</artifactId>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>mysql</groupId>
70+
<artifactId>mysql-connector-java</artifactId>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.apache.httpcomponents</groupId>
75+
<artifactId>httpclient</artifactId>
76+
</dependency>
77+
78+
</dependencies>
79+
80+
<build>
81+
<plugins>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-compiler-plugin</artifactId>
85+
<configuration>
86+
<source>1.8</source>
87+
<target>1.8</target>
88+
</configuration>
89+
</plugin>
90+
<plugin>
91+
<groupId>org.springframework.boot</groupId>
92+
<artifactId>spring-boot-maven-plugin</artifactId>
93+
</plugin>
94+
</plugins>
95+
</build>
96+
97+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.yiqiniu.easytrans.demos.order.impl;
2+
3+
public class Constant {
4+
public static final String APPID="order-service";
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.yiqiniu.easytrans.demos.order.impl;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.transaction.annotation.EnableTransactionManagement;
6+
7+
import com.yiqiniu.easytrans.EnableEasyTransaction;
8+
import com.yiqiniu.easytrans.rpc.impl.dubbo.EnableRpcDubboImpl;
9+
10+
@SpringBootApplication
11+
@EnableEasyTransaction
12+
@EnableTransactionManagement
13+
@EnableRpcDubboImpl
14+
public class OrderApplication {
15+
public static void main(String[] args) {
16+
SpringApplication.run(OrderApplication.class, args);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.yiqiniu.easytrans.demos.order.impl;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
@Controller
10+
public class OrderController {
11+
12+
@Autowired
13+
private OrderService orderService;
14+
15+
@RequestMapping("/buySth")
16+
@ResponseBody
17+
public Integer buySomething(@RequestParam int userId,@RequestParam int money){
18+
return orderService.buySomething(userId, money);
19+
}
20+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.yiqiniu.easytrans.demos.order.impl;
2+
3+
import java.sql.Connection;
4+
import java.sql.Date;
5+
import java.sql.PreparedStatement;
6+
import java.sql.SQLException;
7+
import java.util.concurrent.Future;
8+
9+
import javax.annotation.Resource;
10+
11+
import org.springframework.jdbc.core.JdbcTemplate;
12+
import org.springframework.jdbc.core.PreparedStatementCreator;
13+
import org.springframework.jdbc.support.GeneratedKeyHolder;
14+
import org.springframework.jdbc.support.KeyHolder;
15+
import org.springframework.stereotype.Component;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
import com.yiqiniu.easytrans.core.EasyTransFacade;
19+
import com.yiqiniu.easytrans.demos.wallet.api.vo.WalletPayVO.WalletPayRequestVO;
20+
import com.yiqiniu.easytrans.demos.wallet.api.vo.WalletPayVO.WalletPayResponseVO;
21+
22+
@Component
23+
public class OrderService {
24+
25+
public static final String BUSINESS_CODE = "buySth";
26+
27+
28+
@Resource
29+
private EasyTransFacade transaction;
30+
@Resource
31+
private JdbcTemplate jdbcTemplate;
32+
33+
34+
@Transactional
35+
public int buySomething(int userId,long money){
36+
37+
/**
38+
* finish the local transaction first, in order for performance and generated of business id
39+
*
40+
* 优先完成本地事务以 1. 提高性能(减少异常时回滚消耗)2. 生成事务内交易ID
41+
*/
42+
Integer id = saveOrderRecord(jdbcTemplate,userId,money);
43+
44+
/**
45+
* annotation the global transactionId, it is combined of appId + bussiness_code + id
46+
* it can be omit,then framework will use "default" as businessCode, and will generate an id
47+
* but it will make us harder to associate an global transaction to an concrete business
48+
*
49+
* 声明全局事务ID,其由appId,业务代码,业务代码内ID构成
50+
* 本代码可以省略,框架会自动生成ID及使用一个默认的业务代码
51+
* 但这样的话,会使得我们难以把全局事务ID与一个具体的事务关联起来
52+
*/
53+
transaction.startEasyTrans(BUSINESS_CODE, id);
54+
55+
/**
56+
* call remote service to deduct money, it's a TCC service,
57+
* framework will maintains the eventually constancy based on the final transaction status of method buySomething
58+
* if you think introducing object transaction(EasyTransFacade) is an unacceptable coupling
59+
* then you can refer to another demo(interfacecall) in the demos directory, it will show you how to execute transaction by user defined interface
60+
*
61+
* 调用远程服务扣除所需的钱,这个远程服务实现了TCC接口,
62+
* 框架会根据buySomething方法的事务结果来维护远程服务的最终一致性
63+
* 如果你认为引入transaction(EasyTransFacde)是一个无法接受的耦合
64+
* 那么你可以参考在demos目录下另外一个样例(interfacecall),它会告诉你如何用用户自定义的接口来执行远程事务
65+
*/
66+
WalletPayRequestVO deductRequest = new WalletPayRequestVO();
67+
deductRequest.setUserId(userId);
68+
deductRequest.setPayAmount(money);
69+
/**
70+
* return future for the benefits of performance enhance(batch write execute log and batch execute RPC)
71+
* 返回future是为了能方便的优化性能(批量写日志及批量调用RPC)
72+
*/
73+
@SuppressWarnings("unused")
74+
Future<WalletPayResponseVO> deductFuture = transaction.execute(deductRequest);
75+
76+
/**
77+
* you can add more types of transaction calls here, e.g. TCC,reliable message, SAGA-TCC and so on
78+
* framework will maintains the eventually consistent
79+
*
80+
* 你可以额外加入其它类型的事务,如TCC,可靠消息,SAGA-TCC等等
81+
* 框架会维护全局事务的最终一致性
82+
*/
83+
84+
85+
/**
86+
* we can get remote service result to determine whether to commit this transaction
87+
*
88+
* 可以获取远程返回的结果用以判断是继续往下走 还是 抛异常结束
89+
*
90+
* deductFuture.get();
91+
*/
92+
93+
return id;
94+
}
95+
96+
97+
private Integer saveOrderRecord(JdbcTemplate jdbcTemplate, final int userId, final long money) {
98+
99+
final String INSERT_SQL = "INSERT INTO `order` (`order_id`, `user_id`, `money`, `create_time`) VALUES (NULL, ?, ?, ?);";
100+
KeyHolder keyHolder = new GeneratedKeyHolder();
101+
jdbcTemplate.update(
102+
new PreparedStatementCreator() {
103+
@Override
104+
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
105+
PreparedStatement ps =
106+
connection.prepareStatement(INSERT_SQL, new String[] {"id"});
107+
ps.setInt(1, userId);
108+
ps.setLong(2, money);
109+
ps.setDate(3, new Date(System.currentTimeMillis()));
110+
return ps;
111+
}
112+
},
113+
keyHolder);
114+
115+
return keyHolder.getKey().intValue();
116+
}
117+
118+
119+
}

0 commit comments

Comments
 (0)