分布式事务示例

1、部署Nacos
Nacos部署示例
https://blog.csdn.net/zhaoyinjun7897/article/details/119805622
2、部署seata
seata部署示例
https://blog.csdn.net/zhaoyinjun7897/article/details/119806371
3、示例
nacos-seata.rar
https://download.csdn.net/download/zhaoyinjun7897/21328872
nacos-seata-sql.rar
https://download.csdn.net/download/zhaoyinjun7897/21332374

项目目录结构如下:
在这里插入图片描述
每个模块的配置文件注意点:两根红线处的事务组名称保持一致

seata:
service:
vgroupMapping:
kotlin_group: default 此处的kotlin_group与红线的事务组名称保持一致
grouplist:
default: 127.0.0.1:8091 此处默认端口为8091
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Spring Boot和JPA的分布式事务示例代码: 1. 首先,你需要在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>1.1.0</version> </dependency> ``` 2. 然后,你需要配置Seata的相关属性,包括注册中心地址、事务组名称等。可以在application.yml文件中添加以下配置: ``` spring: application: name: account-service datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10 username: root password: root jpa: hibernate: ddl-auto: update show-sql: true cloud: alibaba: seata: tx-service-group: my_test_tx_group enable-auto-data-source-proxy: true application-id: account-service mybatis: mapper-locations: classpath:mapper/*.xml feign: hystrix: enabled: true eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ seata: enabled: true application-id: account-service registry: type: eureka eureka: service-url: http://localhost:8761/eureka/ config: type: nacos nacos: server-addr: localhost:8848 group: SEATA_GROUP namespace: dev tx-service-group: my_test_tx_group ``` 3. 在代码中使用@GlobalTransactional注解来实现分布式事务。以下是一个简单的转账示例: ``` @Service public class AccountService { @Autowired private AccountRepository accountRepository; @Autowired private UserFeignClient userFeignClient; @GlobalTransactional public void transfer(String fromUserId, String toUserId, BigDecimal amount) { // 扣减账户余额 Account fromAccount = accountRepository.findByUserId(fromUserId); fromAccount.setBalance(fromAccount.getBalance().subtract(amount)); accountRepository.save(fromAccount); // 增加账户余额 Account toAccount = accountRepository.findByUserId(toUserId); toAccount.setBalance(toAccount.getBalance().add(amount)); accountRepository.save(toAccount); // 记录转账日志 userFeignClient.addTransferLog(fromUserId, toUserId, amount); } } ``` 4. 最后,你需要在启动类中添加@EnableFeignClients和@EnableDiscoveryClient注解,以启用Feign和Eureka注册中心的支持。 ``` @SpringBootApplication @EnableFeignClients @EnableDiscoveryClient public class AccountServiceApplication { public static void main(String[] args) { SpringApplication.run(AccountServiceApplication.class, args); } } ``` 以上示例代码实现了一个简单的分布式事务,通过使用Seata来保证ACID事务特性的同时,使用Feign来实现服务之间的调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值