Skip to content

Commit 9f807d2

Browse files
author
YunaiV
committed
增加 seata 服务
1 parent 8b1a03a commit 9f807d2

File tree

8 files changed

+192
-31
lines changed

8 files changed

+192
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
spring:
2+
application:
3+
name: account-service
4+
5+
datasource:
6+
url: jdbc:mysql://127.0.0.1:3306/seata_account?useSSL=false&useUnicode=true&characterEncoding=UTF-8
7+
driver-class-name: com.mysql.jdbc.Driver
8+
username: root
9+
password:
10+
11+
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
12+
dubbo:
13+
# Dubbo 应用配置
14+
application:
15+
name: ${spring.application.name} # 应用名
16+
# Dubbo 注册中心配
17+
registry:
18+
address: nacos://127.0.0.1:8848 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
19+
# Dubbo 服务提供者协议配置
20+
protocol:
21+
port: -1 # 协议端口。使用 -1 表示随机端口。
22+
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档
23+
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者
24+
scan:
25+
base-packages: cn.iocoder.springboot.lab53.accountservice.service
26+
27+
# Seata 配置项,对应 SeataProperties 类
28+
seata:
29+
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
30+
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
31+
# 服务配置项,对应 ServiceProperties 类
32+
service:
33+
# 虚拟组和分组的映射
34+
vgroup-mapping:
35+
account-service-group: default
36+
# 分组和 Seata 服务的映射
37+
grouplist:
38+
default: 127.0.0.1:8091

lab-53/lab-53-seata-at-dubbo-demo/lab-53-seata-at-dubbo-demo-account-service/src/main/resources/application.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ dubbo:
2828
seata:
2929
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
3030
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
31-
# 服务配置项,对应 ServiceProperties 类
31+
# Seata 服务配置项,对应 ServiceProperties 类
3232
service:
3333
# 虚拟组和分组的映射
3434
vgroup-mapping:
3535
account-service-group: default
36-
# 分组和 Seata 服务的映射
37-
grouplist:
38-
default: 127.0.0.1:8091
36+
# Seata 注册中心配置项,对应 RegistryProperties 类
37+
registry:
38+
type: nacos # 注册中心类型,默认为 file
39+
nacos:
40+
cluster: default # 使用的 Seata 分组
41+
namespace: # Nacos 命名空间
42+
serverAddr: localhost # Nacos 服务地址
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
server:
2+
port: 8081 # 端口
3+
4+
spring:
5+
application:
6+
name: user-service
7+
8+
datasource:
9+
url: jdbc:mysql://127.0.0.1:3306/seata_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8
10+
driver-class-name: com.mysql.jdbc.Driver
11+
username: root
12+
password:
13+
14+
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
15+
dubbo:
16+
# Dubbo 应用配置
17+
application:
18+
name: ${spring.application.name} # 应用名
19+
# Dubbo 注册中心配
20+
registry:
21+
address: nacos://127.0.0.1:8848 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
22+
# Dubbo 服务提供者协议配置
23+
protocol:
24+
port: -1 # 协议端口。使用 -1 表示随机端口。
25+
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档
26+
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者
27+
scan:
28+
base-packages: cn.iocoder.springboot.lab53.orderservice.service
29+
30+
# Seata 配置项,对应 SeataProperties 类
31+
seata:
32+
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
33+
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
34+
# 服务配置项,对应 ServiceProperties 类
35+
service:
36+
# 虚拟组和分组的映射
37+
vgroup-mapping:
38+
user-service-group: default
39+
# 分组和 Seata 服务的映射
40+
grouplist:
41+
default: 127.0.0.1:8091

lab-53/lab-53-seata-at-dubbo-demo/lab-53-seata-at-dubbo-demo-order-service/src/main/resources/application.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ server:
33

44
spring:
55
application:
6-
name: user-service
6+
name: order-service
77

88
datasource:
99
url: jdbc:mysql://127.0.0.1:3306/seata_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8
@@ -31,11 +31,15 @@ dubbo:
3131
seata:
3232
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
3333
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
34-
# 服务配置项,对应 ServiceProperties 类
34+
# Seata 服务配置项,对应 ServiceProperties 类
3535
service:
3636
# 虚拟组和分组的映射
3737
vgroup-mapping:
38-
user-service-group: default
39-
# 分组和 Seata 服务的映射
40-
grouplist:
41-
default: 127.0.0.1:8091
38+
order-service-group: default
39+
# Seata 注册中心配置项,对应 RegistryProperties 类
40+
registry:
41+
type: nacos # 注册中心类型,默认为 file
42+
nacos:
43+
cluster: default # 使用的 Seata 分组
44+
namespace: # Nacos 命名空间
45+
serverAddr: localhost # Nacos 服务地址

lab-53/lab-53-seata-at-dubbo-demo/lab-53-seata-at-dubbo-demo-product-service/src/main/java/cn/iocoder/springboot/lab53/productservice/service/ProductService.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.iocoder.springboot.lab53.productservice.service;
2+
3+
import cn.iocoder.springboot.lab53.productservice.dao.ProductDao;
4+
import cn.iocoder.springboot.lab53.storageservice.api.ProductService;
5+
import io.seata.core.context.RootContext;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.transaction.annotation.Propagation;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@org.apache.dubbo.config.annotation.Service
13+
public class ProductServiceImpl implements ProductService {
14+
15+
private Logger logger = LoggerFactory.getLogger(getClass());
16+
17+
@Autowired
18+
private ProductDao productDao;
19+
20+
@Override
21+
@Transactional(propagation = Propagation.REQUIRES_NEW) // 开启新事物
22+
public void reduceStock(Long productId, Integer amount) throws Exception {
23+
logger.info("[reduceStock] 当前 XID: {}", RootContext.getXID());
24+
25+
// 检查库存
26+
checkStock(productId, amount);
27+
28+
logger.info("[reduceStock] 开始扣减 {} 库存", productId);
29+
// 扣减库存
30+
int updateCount = productDao.reduceStock(productId, amount);
31+
// 扣除成功
32+
if (updateCount == 0) {
33+
logger.warn("[reduceStock] 扣除 {} 库存失败", productId);
34+
throw new Exception("库存不足");
35+
}
36+
// 扣除失败
37+
logger.info("[reduceStock] 扣除 {} 库存成功", productId);
38+
}
39+
40+
private void checkStock(Long productId, Integer requiredAmount) throws Exception {
41+
logger.info("[checkStock] 检查 {} 库存", productId);
42+
Integer stock = productDao.getStock(productId);
43+
if (stock < requiredAmount) {
44+
logger.warn("[checkStock] {} 库存不足,当前库存: {}", productId, stock);
45+
throw new Exception("库存不足");
46+
}
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
spring:
2+
application:
3+
name: product-service
4+
5+
datasource:
6+
url: jdbc:mysql://127.0.0.1:3306/seata_product?useSSL=false&useUnicode=true&characterEncoding=UTF-8
7+
driver-class-name: com.mysql.jdbc.Driver
8+
username: root
9+
password:
10+
11+
# dubbo 配置项,对应 DubboConfigurationProperties 配置类
12+
dubbo:
13+
# Dubbo 应用配置
14+
application:
15+
name: ${spring.application.name} # 应用名
16+
# Dubbo 注册中心配
17+
registry:
18+
address: nacos://127.0.0.1:8848 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。
19+
# Dubbo 服务提供者协议配置
20+
protocol:
21+
port: -1 # 协议端口。使用 -1 表示随机端口。
22+
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档
23+
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者
24+
scan:
25+
base-packages: cn.iocoder.springboot.lab53.productservice.service
26+
27+
# Seata 配置项,对应 SeataProperties 类
28+
seata:
29+
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
30+
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
31+
# 服务配置项,对应 ServiceProperties 类
32+
service:
33+
# 虚拟组和分组的映射
34+
vgroup-mapping:
35+
product-service-group: default
36+
# 分组和 Seata 服务的映射
37+
grouplist:
38+
default: 127.0.0.1:8091

lab-53/lab-53-seata-at-dubbo-demo/lab-53-seata-at-dubbo-demo-product-service/src/main/resources/application.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ dubbo:
2828
seata:
2929
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
3030
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
31-
# 服务配置项,对应 ServiceProperties 类
31+
# Seata 服务配置项,对应 ServiceProperties 类
3232
service:
3333
# 虚拟组和分组的映射
3434
vgroup-mapping:
3535
product-service-group: default
36-
# 分组和 Seata 服务的映射
37-
grouplist:
38-
default: 127.0.0.1:8091
36+
# Seata 注册中心配置项,对应 RegistryProperties 类
37+
registry:
38+
type: nacos # 注册中心类型,默认为 file
39+
nacos:
40+
cluster: default # 使用的 Seata 分组
41+
namespace: # Nacos 命名空间
42+
serverAddr: localhost # Nacos 服务地址

0 commit comments

Comments
 (0)