文章目录由于springboot+openFeign+nacos+seata开发实战内容实在过多,故分成上下两篇来完成。
- 项目管理规范
- seata程序实战
- product服务详解
- order服务详解
- 本文小结
项目管理规范
本示例通过Seata中间件实现分布式事务,通过订单微服务执行下单操作,然后由订单微服务调用商品微服务执行扣除库存的操作,这也是微服务中一个典型的分布式事务场景。
seata程序实战
为了后续方便使用SpringCloud Alibaba进行开发, 首先创建一个pom类型的父项目, 主要用于项目技术栈版本管理, 创建一个maven项目,名称为spring-cloud-alibaba-example, 去除src文件, 修改pom文件
4.0.0 ah.wideth spring-cloud-alibaba-example 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE pom UTF-8 1.8 Hoxton.SR12 2.2.7.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${com-alibaba-cloud.version} pom import
后续创建的项目都放到此目录下, 只需要声明groupId和artifactId, 会自动引用父项目spring-cloud-alibaba-example的版本。与其说是父项目, 不如说是根项目: 因为下面每学习一个新的技术, 就会新建一个真正的父项目, 而在对应的父项目下面又会创建许多的子项目
seata-openFeign-nacos-example子项目详细结构
seata-openFeign-nacos-example父工程的pom.xml文件
4.0.0 spring-cloud-alibaba-example ah.wideth 1.0-SNAPSHOT seata-openFeign-nacos-example seata-openFeign-nacos-example seata分布式事务框架和openFeign整合的父工程 pom 1.8 utf-8 UTF-8 2.7.13 1.4.1 order product com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery nacos-client com.alibaba.nacos com.alibaba.nacos nacos-client ${nacos.version} org.springframework.cloud spring-cloud-starter-openfeign
product服务详解
product项目作为服务提供者,maven文件
4.0.0 seata-openFeign-nacos-example ah.wideth 1.0-SNAPSHOT product product 商品服务模块 1.8 utf-8 UTF-8 org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 com.alibaba druid-spring-boot-starter 1.1.9 io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0 com.github.xiaoymin swagger-bootstrap-ui 1.8.1 org.projectlombok lombok org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version} ${project.build.sourceEncoding} org.springframework.boot spring-boot-maven-plugin public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true false
yaml配置文件
server:
port: 8381
spring:
application:
name: product-service
cloud: #nacos注册中心地址的配置
nacos:
discovery:
server-addr: localhost:8848 # nacos 注册中心的地址
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/product?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
profiles:
include: config
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`pid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品表主键',
`name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
`price` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
`stock` int(16) NULL DEFAULT NULL COMMENT '商品库存',
`del_flag` char(3) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '删除标志(0代表存在 1代表删除)',
`create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime(0) NOT NULL COMMENT '创建时间',
`update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`pid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, '苹果笔记本', 1000.00, 78, '0', 'admin', '2022-04-23 19:50:25', 'admin', '2022-04-23 22:53:44', NULL);
INSERT INTO `product` VALUES (2, '小米手机', 800.00, 200, '0', 'admin', '2022-04-23 19:51:17', 'admin', '2022-04-23 22:53:51', NULL);
INSERT INTO `product` VALUES (3, '蓝牙耳机', 200.00, 300, '0', 'admin', '2022-04-23 19:52:07', 'admin', '2022-04-23 22:53:56', NULL);
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
`ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
System.out.println("商品服务8381启动了!!");
}
}
swagger配置
package ah.wideth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_12)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("ah.wideth.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("seata分布式事务测试API接口文档")
.termsOfServiceUrl("http://127.0.0.1:8381/swagger-ui.html")
.version("1.0")
.build();
}
}
ProductServiceImpl文件
package ah.wideth.service.impl;
import ah.wideth.entity.Product;
import ah.wideth.mapper.ProductMapper;
import ah.wideth.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ProductServiceImpl implements IProductService {
@Autowired
private ProductMapper productMapper;
@Override
public Product findProductById(Integer pid) {
Product product = productMapper.findProductById(pid);
return product;
}
@Override
@Transactional
public void reduceInventory(Integer pid, Integer num) {
Product product = productMapper.findProductById(pid);
product.setStock(product.getStock() - num);//减库存
productMapper.saveProduct(product);
}
}
ProductController文件
import ah.wideth.entity.Product;
import ah.wideth.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@Api(tags = "商品")
@RequestMapping("/api/product")
public class ProductController {
@Autowired
private IProductService iProductService;
@GetMapping("/findProductById")
@ApiOperation(value = "查询商品信息")
public Product findProductById(Integer pid) {
Product product = iProductService.findProductById(pid);
return product;
}
@GetMapping("/reduceInventory")
@ApiOperation(value = "减少库存")
public String reduceInventory(Integer pid,Integer num) {
iProductService.reduceInventory(pid,num);
return "success";
}
}
接口调用测试
order服务详解
order服务作为消费者,去调用product生产者服务,pom.xml文件引入
4.0.0 seata-openFeign-nacos-example ah.wideth 1.0-SNAPSHOT order order 订单服务模块 1.8 utf-8 UTF-8 org.springframework.boot spring-boot-starter-web io.seata seata-spring-boot-starter 1.4.0 org.projectlombok lombok mysql mysql-connector-java runtime org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 com.alibaba druid-spring-boot-starter 1.1.9 io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0 com.github.xiaoymin swagger-bootstrap-ui 1.8.1 com.alibaba fastjson 1.2.70 compile org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version} ${project.build.sourceEncoding} org.springframework.boot spring-boot-maven-plugin public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true false
yaml文件
server:
port: 8380
spring:
application:
name: order-service
cloud: #nacos注册中心地址的配置
nacos:
discovery:
server-addr: localhost:8848 # nacos 注册中心的地址
#seata配置
# alibaba:
# seata:
# #事务分组
# tx-service-group: my_test_tx_group
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/order?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
profiles:
include: config
seata:
tx-service-group: my_test_tx_group #事务分组
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for order_info
-- ----------------------------
DROP TABLE IF EXISTS `order_info`;
CREATE TABLE `order_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单表主键',
`real_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户姓名',
`pid` bigint(20) NULL DEFAULT NULL COMMENT '商品ID',
`pname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
`price` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
`num` int(20) NULL DEFAULT NULL COMMENT '商品数量',
`del_flag` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '删除标志(0代表存在 1代表删除)',
`create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of order_info
-- ----------------------------
INSERT INTO `order_info` VALUES (19, '测试人员', 1, '苹果笔记本', 1000.00, 3, '0', '', '2022-04-29 14:09:10', '', '2022-04-29 14:09:10', NULL);
INSERT INTO `order_info` VALUES (20, '测试人员', 1, '苹果笔记本', 1000.00, 3, '0', '', '2022-04-29 14:36:50', '', '2022-04-29 14:36:50', NULL);
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
`ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
ProductClient文件
import ah.wideth.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "product-service")
public interface ProductClient {
@GetMapping("/api/product/reduceInventory")
String reduceInventory(@RequestParam("pid") Integer pid, @RequestParam("num") Integer num);
@GetMapping("/api/product/findProductById")
Product findProductById(@RequestParam("pid") Integer pid);
}
OrderServiceImpl文件
import ah.wideth.client.ProductClient;
import ah.wideth.entity.Order;
import ah.wideth.entity.Product;
import ah.wideth.mapper.OrderMapper;
import ah.wideth.service.IOrderService;
import com.alibaba.fastjson.JSON;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
private ProductClient productClient;
@Autowired
private OrderMapper orderMapper;
@Override
@GlobalTransactional//全局事务
public Order createOrder(Integer pid) {
// 调用商品微服务,查询商品信息
// 分支事务
Product product = productClient.findProductById(pid);
log.info("查询到{}号商品的信息,内容是:{}",pid, JSON.toJSONString(product));
// 创建订单
Order order = new Order();
order.setRealName("测试人员");
order.setPid(product.getPid());
order.setPName(product.getName());
order.setPrice(product.getPrice());
order.setNum(3);
// 本地事务
orderMapper.saveOrder(order);
log.info("创建订单成功,订单信息为{}",JSON.toJSONString(order));
// 扣库存
// 分支事务
productClient.reduceInventory(pid,order.getNum());
return order;
}
}
接口调用
接口调用成功
本文小结
本文介绍了seata如何使用,其实使用起来也不是很难,seata是代码无侵入式的解决方案,实际使用中只需要一个 @GlobalTransactional注解就可以解决问题。



