分布式事物【Seata实现、下载启动Seata服务、搭建聚合父工程构建】(四)-全面详解(学习总结---从入门到深化)(上):https://developer.aliyun.com/article/1419969
Seata提供XA模式实现分布式事务_搭建聚合父工程构建
创建工程distribute-transaction
字符编码
注解生效激活
Java编译版本选择
<!-- 指定JDK版本--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compilerplugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
File Type过滤
pom配置版本
<properties> <spring-boot.version>2.6.3</spring-boot.version> <spring.cloud.version>2021.0.1</spring.cloud.version> <spring.cloud.alibaba.version>2021.0.1.0</spring.cloud.alibaba.version> <lombok.version>1.18.22</lombok.version> </properties> <dependencyManagement> <dependencies> <!--spring boot 2.6.3--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-bootstarter-parent</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-clouddependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringCloud Aliabab --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloudalibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement>
IDEA开启Dashboard
普通的Run面板
Run Dashboard面板
修改配置文件
在.idea/workspace.xml 文件中找到
添加配置
<component name="RunDashboard"> <option name="ruleStates"> <list> <RuleState> <option name="name" value="ConfigurationTypeDashboardGroupingRule"/> </RuleState> <RuleState> <option name="name" value="StatusDashboardGroupingRule" /> </RuleState> </list> </option> <option name="configurationTypes"> <set> <option value="SpringBootApplicationConfigurationType"/> </set> </option> </component>
Seata提供XA模式实现分布式事务_转账功能实现上
实现如下功能
李四账户增加金额。
创建bank2
pom引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starterweb</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-bootstarter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connectorjava</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starteralibaba-nacos-discovery</artifactId> </dependency>
编写主启动类
//添加对mapper包扫描 Mybatis-plus @MapperScan("com.tong.mapper") @SpringBootApplication @Slf4j //开启发现注册 @EnableDiscoveryClient public class SeataBank2Main6002 { public static void main(String[] args) { SpringApplication.run(SeataBank1Main6002.class,args); log.info("************** SeataBank1Main6002 *************"); } }
编写YML配置文件
server: port: 6002 spring: application: name: seata-bank2 cloud: nacos: discovery: # Nacos server地址 server-addr: 192.168.66.101:8848 datasource: url: jdbc:mysql://localhost:3306/bank2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: root password01: 123456 driver-class-name: com.mysql.jdbc.Driver
代码生成
引入Mybatis Plus代码生成依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plusgenerator</artifactId> <version>3.5.2</version> </dependency> <!-- 模板引擎 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-enginecore</artifactId> <version>2.0</version> </dependency>
生成代码
package com.tong.utils; import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import java.util.Arrays; import java.util.List; public class CodeGenerator { public static void main(String[] args) { FastAutoGenerator.create("jdbc:mysql://192.168.66.100:3306/bank2", "root", "123456") .globalConfig(builder -> { builder.author("itxiaotong")// 设置作者 .commentDate("MM-dd") // 注释日期格式 .outputDir(System.getProperty("user.dir")+"/xa-seata/bank2"+ "/src/main/java/") // 指定输出目录 .fileOverride(); //覆盖文件 }) // 包配置 .packageConfig(builder -> { builder.parent("com.tong") // 包名前缀 .entity("entity")//实体类包名 .mapper("mapper")//mapper接口包名 .service("service"); //service包名 }) .strategyConfig(builder -> { // 设置需要生成的表名 List<String> strings = Arrays.asList("account_info"); builder.addInclude(strings) // 开始实体类配置 .entityBuilder() // 开启lombok模型 .enableLombok() //表名下划线转驼峰 .naming(NamingStrategy.underline_to_camel) //列名下划线转驼峰 .columnNaming(NamingStrategy.underline_to_camel); }) .execute(); } }
编写转账接口
public interface IAccountInfoService { //李四增加金额 void updateAccountBalance(String accountNo, Double amount); }
编写转账接口实现类
@Service @Slf4j public class AccountInfoServiceImpl implements IAccountInfoService { @Autowired AccountMapper accountMapper; @Override public void updateAccountBalance(String accountNo, Double amount) { // 1. 获取用户信息 AccountInfo accountInfo = accountMapper.selectById(accountNo); accountInfo.setAccountBalance(accountInfo.getAccountBalance() + amount); accountMapper.updateById(accountInfo); } }
编写控制层
@RestController @RequestMapping("/bank2") public class Bank2Controller { @Autowired IAccountInfoService accountInfoService; //接收张三的转账 @GetMapping("/transfer") public String transfer(Double amount){ //李四增加金额 accountInfoService.updateAccountBalance("3",amount); return "bank2"+amount; } }