Spring Cloud Alibaba +Nacos+Seata 分布式事务案例

本文介绍了如何结合Spring Cloud Alibaba、Nacos和Seata实现分布式事务。首先,展示了产品的数据库建表和微服务配置,包括各服务的pom.xml、application.yml设置。接着,讲解了product-service和order-service的接口实现,以及如何使用Feign进行服务间调用。随后,详细说明了Seata的安装和配置过程,包括修改配置文件、创建undo_log表和在Nacos中配置Seata。最后,展示了在order-service中添加@GlobalTransactional注解以启用分布式事务,并配置数据源代理以确保事务正确回滚。
摘要由CSDN通过智能技术生成

product-service-client

1.0-SNAPSHOT

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

src/main/java

**/*.xml

product-service-client pom.xml


<?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”>

product-service

com.xuweichao

1.0-SNAPSHOT

4.0.0

com.xuweichao

product-service-client

jar

com.baomidou

mybatis-plus-annotation

3.3.0

compile

com.baomidou

mybatis-plus-extension

3.3.0

compile

org.springframework.cloud

spring-cloud-starter-openfeign

数据库建表:订单表 产品表

CREATE TABLE order_base (

id bigint(20) NOT NULL AUTO_INCREMENT,

user_name varchar(10) DEFAULT NULL,

p_id bigint(20) DEFAULT NULL,

p_name varchar(50) DEFAULT NULL,

p_price double(10,2) DEFAULT NULL,

number int(11) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8

CREATE TABLE product_base (

id bigint(20) NOT NULL AUTO_INCREMENT,

name varchar(100) DEFAULT NULL,

price double(10,2) DEFAULT NULL,

stock int(11) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4

使用 代码生成工具生成数据库试题对象和 service

order-service 微服务 添加配置 application.yml

spring:

application:

name: order-service

datasource:

driver-class-name: com.mysql.jdbc.Driver

password: xxx

url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/database-name?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

username: root

server:

port: 18080

swagger:

enable: true

mybatis-plus:

configuration:

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

product-service 微服务 添加配置 application.yml

spring:

application:

name: product-service

datasource:

driver-class-name: com.mysql.j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值