forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Apr 2, 2020
1 parent
8b1a03a
commit 9f807d2
Showing
8 changed files
with
192 additions
and
31 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...-demo/lab-53-seata-at-dubbo-demo-account-service/src/main/resources/application-file.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
spring: | ||
application: | ||
name: account-service | ||
|
||
datasource: | ||
url: jdbc:mysql://127.0.0.1:3306/seata_account?useSSL=false&useUnicode=true&characterEncoding=UTF-8 | ||
driver-class-name: com.mysql.jdbc.Driver | ||
username: root | ||
password: | ||
|
||
# dubbo 配置项,对应 DubboConfigurationProperties 配置类 | ||
dubbo: | ||
# Dubbo 应用配置 | ||
application: | ||
name: ${spring.application.name} # 应用名 | ||
# Dubbo 注册中心配 | ||
registry: | ||
address: nacos://127.0.0.1:8848 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。 | ||
# Dubbo 服务提供者协议配置 | ||
protocol: | ||
port: -1 # 协议端口。使用 -1 表示随机端口。 | ||
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档 | ||
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者 | ||
scan: | ||
base-packages: cn.iocoder.springboot.lab53.accountservice.service | ||
|
||
# Seata 配置项,对应 SeataProperties 类 | ||
seata: | ||
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name} | ||
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名 | ||
# 服务配置项,对应 ServiceProperties 类 | ||
service: | ||
# 虚拟组和分组的映射 | ||
vgroup-mapping: | ||
account-service-group: default | ||
# 分组和 Seata 服务的映射 | ||
grouplist: | ||
default: 127.0.0.1:8091 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...bo-demo/lab-53-seata-at-dubbo-demo-order-service/src/main/resources/application-file.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
server: | ||
port: 8081 # 端口 | ||
|
||
spring: | ||
application: | ||
name: user-service | ||
|
||
datasource: | ||
url: jdbc:mysql://127.0.0.1:3306/seata_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8 | ||
driver-class-name: com.mysql.jdbc.Driver | ||
username: root | ||
password: | ||
|
||
# dubbo 配置项,对应 DubboConfigurationProperties 配置类 | ||
dubbo: | ||
# Dubbo 应用配置 | ||
application: | ||
name: ${spring.application.name} # 应用名 | ||
# Dubbo 注册中心配 | ||
registry: | ||
address: nacos://127.0.0.1:8848 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。 | ||
# Dubbo 服务提供者协议配置 | ||
protocol: | ||
port: -1 # 协议端口。使用 -1 表示随机端口。 | ||
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档 | ||
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者 | ||
scan: | ||
base-packages: cn.iocoder.springboot.lab53.orderservice.service | ||
|
||
# Seata 配置项,对应 SeataProperties 类 | ||
seata: | ||
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name} | ||
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名 | ||
# 服务配置项,对应 ServiceProperties 类 | ||
service: | ||
# 虚拟组和分组的映射 | ||
vgroup-mapping: | ||
user-service-group: default | ||
# 分组和 Seata 服务的映射 | ||
grouplist: | ||
default: 127.0.0.1:8091 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
...vice/src/main/java/cn/iocoder/springboot/lab53/productservice/service/ProductService.java
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
.../src/main/java/cn/iocoder/springboot/lab53/productservice/service/ProductServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cn.iocoder.springboot.lab53.productservice.service; | ||
|
||
import cn.iocoder.springboot.lab53.productservice.dao.ProductDao; | ||
import cn.iocoder.springboot.lab53.storageservice.api.ProductService; | ||
import io.seata.core.context.RootContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@org.apache.dubbo.config.annotation.Service | ||
public class ProductServiceImpl implements ProductService { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private ProductDao productDao; | ||
|
||
@Override | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) // 开启新事物 | ||
public void reduceStock(Long productId, Integer amount) throws Exception { | ||
logger.info("[reduceStock] 当前 XID: {}", RootContext.getXID()); | ||
|
||
// 检查库存 | ||
checkStock(productId, amount); | ||
|
||
logger.info("[reduceStock] 开始扣减 {} 库存", productId); | ||
// 扣减库存 | ||
int updateCount = productDao.reduceStock(productId, amount); | ||
// 扣除成功 | ||
if (updateCount == 0) { | ||
logger.warn("[reduceStock] 扣除 {} 库存失败", productId); | ||
throw new Exception("库存不足"); | ||
} | ||
// 扣除失败 | ||
logger.info("[reduceStock] 扣除 {} 库存成功", productId); | ||
} | ||
|
||
private void checkStock(Long productId, Integer requiredAmount) throws Exception { | ||
logger.info("[checkStock] 检查 {} 库存", productId); | ||
Integer stock = productDao.getStock(productId); | ||
if (stock < requiredAmount) { | ||
logger.warn("[checkStock] {} 库存不足,当前库存: {}", productId, stock); | ||
throw new Exception("库存不足"); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...-demo/lab-53-seata-at-dubbo-demo-product-service/src/main/resources/application-file.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
spring: | ||
application: | ||
name: product-service | ||
|
||
datasource: | ||
url: jdbc:mysql://127.0.0.1:3306/seata_product?useSSL=false&useUnicode=true&characterEncoding=UTF-8 | ||
driver-class-name: com.mysql.jdbc.Driver | ||
username: root | ||
password: | ||
|
||
# dubbo 配置项,对应 DubboConfigurationProperties 配置类 | ||
dubbo: | ||
# Dubbo 应用配置 | ||
application: | ||
name: ${spring.application.name} # 应用名 | ||
# Dubbo 注册中心配 | ||
registry: | ||
address: nacos://127.0.0.1:8848 # 注册中心地址。个鞥多注册中心,可见 http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html 文档。 | ||
# Dubbo 服务提供者协议配置 | ||
protocol: | ||
port: -1 # 协议端口。使用 -1 表示随机端口。 | ||
name: dubbo # 使用 `dubbo://` 协议。更多协议,可见 http://dubbo.apache.org/zh-cn/docs/user/references/protocol/introduction.html 文档 | ||
# 配置扫描 Dubbo 自定义的 @Service 注解,暴露成 Dubbo 服务提供者 | ||
scan: | ||
base-packages: cn.iocoder.springboot.lab53.productservice.service | ||
|
||
# Seata 配置项,对应 SeataProperties 类 | ||
seata: | ||
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name} | ||
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名 | ||
# 服务配置项,对应 ServiceProperties 类 | ||
service: | ||
# 虚拟组和分组的映射 | ||
vgroup-mapping: | ||
product-service-group: default | ||
# 分组和 Seata 服务的映射 | ||
grouplist: | ||
default: 127.0.0.1:8091 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters