springboot 整合 seata的配置過(guò)程
前言:
小編引入的圖片和文字描述都是來(lái)自于尚硅谷的視頻講解,在此感謝尚硅谷的老師,同時(shí)也結(jié)合 seata文檔官方文檔進(jìn)行整合
項(xiàng)目地址(gitee): https://gitee.com/qinenqi/online
springboot整合 seata
1.整合配置
online-project 這個(gè)服務(wù)調(diào)用 online-coupon這個(gè)服務(wù)
在 這兩個(gè)被整合的服務(wù)對(duì)用的數(shù)據(jù)庫(kù)中分別 創(chuàng)建 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. 引入依賴(lài)
<!-- seata -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>小編這兒已經(jīng)引入了 阿里的相關(guān)組件,請(qǐng)根據(jù)自己的實(shí)際情況進(jìn)行處理
<!-- 服務(wù)注冊(cè)/發(fā)現(xiàn)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心來(lái)做配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>引入依賴(lài)后,查看自己的 seata-all-0.7.1,要根據(jù)這個(gè)版本下載相應(yīng)的seata 服務(wù)器

4.下載對(duì)應(yīng)的服務(wù)器軟件包
下載地址:seata下載地址,小編下載是seata-server-0.7.1,下載完成之后解壓文件
5.修改配置文件
進(jìn)入 conf文件夾,修改registry.conf

在注冊(cè)中, 小編配置的是nacos, 把type = “file” 改成 type = “nacos”,

在配置信息中,小編用的是默認(rèn)的文件方式
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”
把這兩個(gè)配置文件從conf文件夾下復(fù)制到項(xiàng)目的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.啟動(dòng)nacos 和 seata 服務(wù)(startup.cmd、seata-server.bat)

服務(wù)啟動(dòng)以后,訪(fǎng)問(wèn) http://127.0.0.1:8848/nacos/, 可以看到 seata的服務(wù)
9.給分布式大事務(wù)的入口標(biāo)注@GlobalTransactional、每一個(gè)遠(yuǎn)程的小事務(wù)用 @Transactional


10.具體業(yè)務(wù):
在 online-project服務(wù)的ProjectController中
/**
* 根據(jù) id 更新數(shù)據(jù)
* @param project
* @return
*/
@PostMapping("/updateProjectById")
public R updateProjectById(@RequestBody Project project){
projectService.updateProjectById(project);
return R.ok();
}在 CouponServiceImpl中
/**
* 從 商品哪兒調(diào)用 用來(lái)測(cè)試 seata
*/
@Transactional
public void testSeata(){
CouponEntity couponEntity = new CouponEntity();
couponEntity.setId(4L);
couponEntity.setCouponName("從 商品哪兒調(diào)用 用來(lái)測(cè)試 seata02");
couponMapper.updateById(couponEntity);
// int number = 2/0;
}在online-coupon服務(wù)CouponController中
/**
* 從 商品哪兒調(diào)用 用來(lái)測(cè)試 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中
/**
* 從 商品哪兒調(diào)用 用來(lái)測(cè)試 seata
*/
@Transactional
public void testSeata(){
CouponEntity couponEntity = new CouponEntity();
couponEntity.setId(4L);
couponEntity.setCouponName("從 商品哪兒調(diào)用 用來(lái)測(cè)試 seata02");
couponMapper.updateById(couponEntity);
// int number = 2/0;
}兩個(gè)服務(wù)之間的調(diào)用使用的 openforeign,經(jīng)過(guò)小編的測(cè)試,兩個(gè)微服務(wù)實(shí)現(xiàn)了分布式事務(wù)的一致性
到此這篇關(guān)于springboot 整合 seata的文章就介紹到這了,更多相關(guān)springboot 整合 seata內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus 集成動(dòng)態(tài)多數(shù)據(jù)源的實(shí)現(xiàn)示例
本文主要介紹了MyBatis-Plus 集成動(dòng)態(tài)多數(shù)據(jù)源的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
IDEA 打開(kāi)java文件對(duì)應(yīng)的class路徑的操作步驟
這篇文章主要介紹了IDEA 打開(kāi)java文件對(duì)應(yīng)的class路徑的操作步驟,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
MyBatis XML方式的基本用法之多表查詢(xún)功能的示例代碼
這篇文章主要介紹了MyBatis XML方式的基本用法之多表查詢(xún)功能的示例代碼,本文通過(guò)示例文字相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Java對(duì)接ansible自動(dòng)運(yùn)維化平臺(tái)方式
這篇文章主要介紹了Java對(duì)接ansible自動(dòng)運(yùn)維化平臺(tái)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
java實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
Springboot整合SpringSecurity實(shí)現(xiàn)登錄認(rèn)證和鑒權(quán)全過(guò)程
這篇文章主要介紹了Springboot整合SpringSecurity實(shí)現(xiàn)登錄認(rèn)證和鑒權(quán)全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
java中Hibernate面試知識(shí)點(diǎn)整理
在本篇文章里小編給大家整理的是一篇關(guān)于java中Hibernate面試知識(shí)點(diǎn)整理內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01
SpringBoot自定義監(jiān)聽(tīng)器的項(xiàng)目實(shí)踐
Spring Boot提供了強(qiáng)大的事件模型,其中包括多種內(nèi)置監(jiān)聽(tīng)器,同時(shí)也支持開(kāi)發(fā)者自定義監(jiān)聽(tīng)器,下面就來(lái)介紹下SpringBoot自定義監(jiān)聽(tīng)器,感興趣的可以了解一下2024-03-03
基于jstree使用JSON數(shù)據(jù)組裝成樹(shù)
這篇文章主要為大家詳細(xì)介紹了基于jstree使用JSON數(shù)據(jù)組裝成樹(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

