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

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

在注冊中, 小編配置的是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)以后,訪問 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)用 用來測試 seata
*/
@Transactional
public void testSeata(){
CouponEntity couponEntity = new CouponEntity();
couponEntity.setId(4L);
couponEntity.setCouponName("從 商品哪兒調(diào)用 用來測試 seata02");
couponMapper.updateById(couponEntity);
// int number = 2/0;
}在online-coupon服務(wù)CouponController中
/**
* 從 商品哪兒調(diào)用 用來測試 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)用 用來測試 seata
*/
@Transactional
public void testSeata(){
CouponEntity couponEntity = new CouponEntity();
couponEntity.setId(4L);
couponEntity.setCouponName("從 商品哪兒調(diào)用 用來測試 seata02");
couponMapper.updateById(couponEntity);
// int number = 2/0;
}兩個(gè)服務(wù)之間的調(diào)用使用的 openforeign,經(jīng)過小編的測試,兩個(gè)微服務(wù)實(shí)現(xiàn)了分布式事務(wù)的一致性
到此這篇關(guān)于springboot 整合 seata的文章就介紹到這了,更多相關(guān)springboot 整合 seata內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Mapper批量插入Oracle數(shù)據(jù)@InsertProvider注解
今天小編就為大家分享一篇關(guān)于Mapper批量插入Oracle數(shù)據(jù)@InsertProvider注解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
詳解如何讓Spring MVC顯示自定義的404 Not Found頁面
這篇文章主要介紹了詳解如何讓Spring MVC顯示自定義的404 Not Found頁面,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
SpringBoot org.springframework.beans.factory.Unsatisfie
本文主要介紹了SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常,文中通過示例代碼介紹的很詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
IDEA創(chuàng)建Java?Web項(xiàng)目的超詳細(xì)圖文教學(xué)
IDEA是程序員們常用的java集成開發(fā)環(huán)境,也是被公認(rèn)為最好用的java開發(fā)工具,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Java?Web項(xiàng)目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Java中finally關(guān)鍵字對返回值的影響詳解
這篇文章主要介紹了Java中finally關(guān)鍵字對返回值的影響詳解,執(zhí)行完try catch里面內(nèi)容準(zhǔn)備return時(shí),如果還有finally需要執(zhí)行這是編譯器會(huì)為我們增加一個(gè)全局變量去暫存return 的值,等到finally執(zhí)行完成去return這個(gè)全局變量,需要的朋友可以參考下2024-01-01
Java線程池submit阻塞獲取結(jié)果的實(shí)現(xiàn)原理詳解
Java線程池中提交任務(wù)運(yùn)行,通常使用execute()方法就足夠了。那如果想要實(shí)現(xiàn)在主線程中阻塞獲取線程池任務(wù)運(yùn)行的結(jié)果,該怎么辦呢?本文就來和大家一起討論討論2022-10-10
springboot整合quartz實(shí)現(xiàn)定時(shí)任務(wù)示例
spring支持多種定時(shí)任務(wù)的實(shí)現(xiàn)。我們來介紹下使用spring的定時(shí)器和使用quartz定時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Struts1教程之ActionMapping_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Struts1教程之ActionMapping,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

