分布式事务TX-LCN课题实验手册(三)

上一篇文章,我们构建了分布式事务管理器,本文将构建仓储业务服务。

新建仓储项目

在 IDEA 或 Eclipse 中构建一个简单的 maven 项目,
groupId: cn.com.hohistar.cloud
artifactId: tx-client-store

然后用以下的内容替换掉项目中原来的 pom.xml 中的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.com.hohistar.cloud</groupId>
<artifactId>tx-client-store</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

新建启动类

package: cn.com.hohistar.cloud.store
class: TxStoreApplication

代码:

1
2
3
4
5
6
7
8
9
@SpringBootApplication
public class TxStoreApplication {

public static void main(String[] args) {

SpringApplication.run(TxStoreApplication.class, args);
}

}

新建Model

package: cn.com.hohistar.cloud.store.model
class: StoreMaster

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class StoreMaster {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@NotNull
private String product;

private Integer allCount;

private Integer orderId;
}

数据访问接口

package: cn.com.hohistar.cloud.store.repository
interface: StoreMasterRepos

代码:

1
2
3
@Repository
public interface StoreMasterRepos extends JpaRepository<StoreMaster, Integer> {
}

业务类:

package: cn.com.hohistar.cloud.store.biz
class: StoreMasterBiz

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Service
public class StoreMasterBiz {

@Autowired
private StoreMasterRepos storeMasterRepos;

@Transactional
public void handlOrder(String prod, Integer orderId, Integer allCount) {

if ("jack".equals(prod)) {
throw new RuntimeException("The Jack is not allowed.");
}

StoreMaster store = new StoreMaster();
store.setProduct(prod);
store.setOrderId(orderId);
store.setAllCount(allCount);

storeMasterRepos.save(store);
}
}

Api接口类

package: cn.com.hohistar.cloud.store.api
class: StoreMasterApi

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
public class StoreMasterApi {

@Autowired
private StoreMasterBiz storeMasterBiz;


@GetMapping("/handleOrder")
public String handleOrder(@RequestParam("prod") String prod, @RequestParam("orderId") Integer orderId, @RequestParam("count") Integer count) {

storeMasterBiz.handlOrder(prod, orderId, count);

return "ok";
}
}

配置文件

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server:
port: 8082

logging:
level:
com.example.demo: DEBUG


spring:
application:
name: cloud-store-service
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:./hohistar-store
username: sa
password:
jpa:
database: H2
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: create-drop
h2:
console:
enabled: true
path: /api/h2
settings:
web-allow-others: true

下一步

下一篇文章

本文标题:分布式事务TX-LCN课题实验手册(三)

文章作者:Morning Star

发布时间:2019年12月03日 - 07:12

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/microservice/spring-cloud/springcloud-tx-lcn-practise-manual-03/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。