Skip to content

Commit

Permalink
增加 seata 服务
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Apr 2, 2020
1 parent 8b1a03a commit 9f807d2
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 31 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ dubbo:
seata:
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
# 服务配置项,对应 ServiceProperties 类
# Seata 服务配置项,对应 ServiceProperties 类
service:
# 虚拟组和分组的映射
vgroup-mapping:
account-service-group: default
# 分组和 Seata 服务的映射
grouplist:
default: 127.0.0.1:8091
# Seata 注册中心配置项,对应 RegistryProperties 类
registry:
type: nacos # 注册中心类型,默认为 file
nacos:
cluster: default # 使用的 Seata 分组
namespace: # Nacos 命名空间
serverAddr: localhost # Nacos 服务地址
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ server:

spring:
application:
name: user-service
name: order-service

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

This file was deleted.

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("库存不足");
}
}

}
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ dubbo:
seata:
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name}
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名
# 服务配置项,对应 ServiceProperties 类
# Seata 服务配置项,对应 ServiceProperties 类
service:
# 虚拟组和分组的映射
vgroup-mapping:
product-service-group: default
# 分组和 Seata 服务的映射
grouplist:
default: 127.0.0.1:8091
# Seata 注册中心配置项,对应 RegistryProperties 类
registry:
type: nacos # 注册中心类型,默认为 file
nacos:
cluster: default # 使用的 Seata 分组
namespace: # Nacos 命名空间
serverAddr: localhost # Nacos 服务地址

0 comments on commit 9f807d2

Please sign in to comment.