springboot 整合 seata的配置过程

网友投稿 1261 2022-12-15

springboot 整合 seata的配置过程

springboot 整合 seata的配置过程

前言:

引入的图片和文字描述都是来自于尚硅谷的视频讲解,在此感谢尚硅谷的老师,同时也结合 seata文档官方文档进行整合

项目地址(gitee): https://gitee.com/qinenqi/online

springboot整合 seata

整合配置

online-project 这个服务调用 online-coupon这个服务

在 这两个被整合的服务对用的数据库中分别 创建 UNDO_LOG 表

-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log

CREATE TABLE `undo_log` (

`id` bigint(20) NOT NULL AUTO_INCREMENT,

`branch_id` bigint(20) NOT NULL,

`xid` varchar(100) NOT NULL,

`context` varchar(128) NOT NULL,

`rollback_info` longblob NOT NULL,

`log_status` int(11) NOT NULL,

`log_created` datetime NOT NULL,

`log_modified` datetime NOT NULL,

`ext` varchar(100) DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2. 引入依赖

com.alibaba.cloud

spring-cloud-starter-alibaba-seata

这儿已经引入了 阿里的相关组件,请根据自己的实际情况进行处理

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-discovery

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-config

引入依赖后,查看自己的 seata-all-0.7.1,要根据这个版本-相应的seata 服务器

4.-对应的服务器软件包,-:seata-,-是seata-server-0.7.1,-完成之后解压文件

5.修改配置文件,进入 conf文件夹,修改registry.conf

在注册中, 配置的是nacos, 把type = “file” 改成 type = “nacos”,

在配置信息中,用的是默认的文件方式

6.在online-coupon、online-project 新建 MySeataConfig

import com.zaxxer.hikari.HikariDataSource;

import io.seata.rm.datasource.DataSourceProxy;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.util.StringUtils;

import javax.sql.DataSource;

@Configuration

public class MySeataConfig {

@Autowired

DataSourceProperties dataSourceProperties;

@Bean

public DataSource dataSource(DataSourceProperties dataSourceProperties){

HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();

if (StringUtils.hasText(dataSourceProperties.getName())) {

dataSource.setPoolName(dataSourceProperties.getName());

}

return new DataSourceProxy(dataSource);

}

}

7.分别引入配置文件(file.conf、registry.conf)并修改 vgroup_mapping.my_test_tx_group = “default”

把这两个配置文件从conf文件夹下复制到项目的resources目录下,分别修改file.conf,把vgroup_mapping.my_test_tx_group = "default"分别修改成vgroup_mapping.online-coupon-fescar-service-group = "default"和 vgroup_mapping.online-project-fescar-service-group = “default”

8.启动nacos 和 seata 服务(startup.cmd、seata-server.bat)

服务启动以后,访问 http://127.0.0.1:8848/nacos/, 可以看到 seata的服务

9.给分布式大事务的入口标注@GlobalTransactional、每一个远程的小事务用 @Transactional

10.具体业务:

在 online-project服务的ProjectController中

/**

* 根据 id 更新数据

* @param project

* @return

*/

@PostMapping("/updateProjectById")

public R updateProjectById(@RequestBody Project project){

projectService.updateProjectById(project);

return R.ok();

}

在 CouponServiceImpl中

/**

* 从 商品哪儿调用 用来测试 seata

*/

@Transactional

public void testSeata(){

CouponEntity couponEntity = new CouponEntity();

couponEntity.setId(4L);

couponEntity.setCouponName("从 商品哪儿调用 用来测试 seata02");

couponMapper.updateById(couponEntity);

// int number = 2/0;

}

在online-coupon服务CouponController中

/**

* 从 商品哪儿调用 用来测试 seata

* @return

*/

@RequestMapping("/testSeata")

public R testSeata(){

couponService.testSeata();

return R.ok();

}

新建 CouponFeignService

import com.example.onlinecommon.utils.R;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("online-coupon")

public interface CouponFeignService {

@RequestMapping("/coupon/couponController/testSeata")

R testSeata();

}

在 CouponServiceImpl中

/**

* 从 商品哪儿调用 用来测试 seata

*/

@Transactional

public void testSeata(){

CouponEntity couponEntity = new CouponEntity();

couponEntity.setId(4L);

couponEntity.setCouponName("从 商品哪fwyLYXB儿调用 用来测试 seata02");

couponMapper.updateById(couponEntity);

// int number = 2/0;

}

两个服务之间的调用使用的 openforeign,经过的测试,两个微服务实现了分布式事务的一致性

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:你知道Spring中为何不建议使用字段注入吗
下一篇:多线程Thread,Runnable,Callable实现方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~