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

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

新建会员项目

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

然后用以下的内容替换掉项目中原来的 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-account</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: TxAccountApplication

代码:

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

public static void main(String[] args) {

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

}

新建Model

package: cn.com.hohistar.cloud.account.model
class: OrderHistory

代码:

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

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

@NotNull
private String product;

private Integer allCount;

private Integer orderId;
}

数据访问接口

package: cn.com.hohistar.cloud.account.repository
interface: OrderHistoryRepos

代码:

1
2
3
@Repository
public interface OrderHistoryRepos extends JpaRepository<OrderHistory, Integer> {
}

业务类:

package: cn.com.hohistar.cloud.account.biz
class: OrderHistoryBiz

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

public class OrderHistoryBiz {

@Autowired
private OrderHistoryRepos orderHistoryRepos;

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

OrderHistory order = new OrderHistory();
order.setProduct(prod);
order.setOrderId(orderId);
order.setAllCount(allCount);

orderHistoryRepos.save(order);
}
}

Api接口类

package: cn.com.hohistar.cloud.account.api
class: OrderHistoryApi

代码:

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

@Autowired
private OrderHistoryBiz orderHistoryBiz;


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

orderHistoryBiz.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
server:
port: 8083

logging:
level:
com.example.demo: DEBUG

spring:
application:
name: cloud-store-service
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:./hohistar-account
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课题实验手册(四)

文章作者:晨星

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

最后更新:2020年05月28日 - 16:05

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

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