java分布式事務(wù)seata的使用方式
首先創(chuàng)建一個(gè)seata的springboot模塊
并引入seata的起步依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
模塊的目錄結(jié)構(gòu)
如下:

- seata.yaml中的內(nèi)容為:
seata:
tx-service-group: seata-toutiao
service:
vgroup-mapping: # 事務(wù)組與cluster的映射關(guān)系
seata-toutiao: DEFAULT
grouplist:
DEFAULT: 192.168.211.136:8091
- spring.factories中的內(nèi)容為:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.jjw.core.seata.SeataHeimaAutoConfiguration org.springframework.boot.env.EnvironmentPostProcessor=\ com.jjw.core.seata.MyEnvironmentPostProcessor
- MyEnvironmentPostProcessor中的內(nèi)容為:
package com.jjw.core.seata;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.util.List;
/**
* 自定義環(huán)境處理,在運(yùn)行SpringApplication之前加載任意配置文件到Environment環(huán)境中
*/
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {
//Properties對(duì)象
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
//自定義配置文件
String[] profiles = {
"seata.yaml"
};
//循環(huán)添加
for (String profile : profiles) {
//從classpath路徑下面查找文件
Resource resource = new ClassPathResource(profile);
//加載成PropertySource對(duì)象,并添加到Environment環(huán)境中
environment.getPropertySources().addFirst(loadProfiles(resource));
}
}
//加載單個(gè)配置文件
private PropertySource<?> loadProfiles(Resource resource) {
if (!resource.exists()) {
throw new IllegalArgumentException("資源" + resource + "不存在");
}
try {
YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> resources = yamlPropertySourceLoader.load(resource.getFilename(), resource);
return resources.get(0);
} catch (IOException ex) {
throw new IllegalStateException("加載配置文件失敗" + resource, ex);
}
}
}
- SeataHeimaAutoConfiguration中的內(nèi)容為:
package com.jjw.core.seata;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import io.seata.spring.boot.autoconfigure.properties.client.ServiceProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* --加載文件properties
* https://www.cnblogs.com/huanzi-qch/p/11122107.html
* --加載yaml
* https://blog.csdn.net/baidu_28523317/article/details/108701391
*
* --順序
* https://blog.csdn.net/f641385712/article/details/105596178
*
* --可能存在的問(wèn)題
* https://segmentfault.com/q/1010000040364236
*/
@Configuration
@ConditionalOnClass(DataSourceProxy.class)
public class SeataHeimaAutoConfiguration {
//僅僅用作測(cè)試
@Bean
public Map<String,String> seatax(SeataProperties seataProperties,
ServiceProperties serviceProperties){
String txServiceGroup = seataProperties.getTxServiceGroup();
System.out.println(txServiceGroup);
System.out.println(serviceProperties.getGrouplist());
return new HashMap<>();
}
}
在需要用到分布式事務(wù)的微服務(wù)中添加依賴
(用到幾個(gè)微服務(wù)就給相應(yīng)的微服務(wù)都添加分布式事務(wù)依賴)、且對(duì)這幾個(gè)微服務(wù)所在的庫(kù)都添加一個(gè)undolog表:
-- auto-generated definition
create table undo_log
(
id bigint auto_increment
primary key,
branch_id bigint not null,
xid varchar(100) not null,
context varchar(128) not null,
rollback_info longblob not null,
log_status int not null,
log_created datetime not null,
log_modified datetime not null,
ext varchar(100) null,
constraint ux_undo_log
unique (xid, branch_id)
)
charset = utf8;
在最開(kāi)始使用分布式事務(wù)的那個(gè)微服務(wù)上的方法上添加事務(wù)注解
@GlobalTransactional // 全局事務(wù)
@Transactional // 本地事務(wù)
@Override
public void pass(Integer id) {
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決spring AOP中自身方法調(diào)用無(wú)法應(yīng)用代理的問(wèn)題
這篇文章主要介紹了解決spring AOP中自身方法調(diào)用無(wú)法應(yīng)用代理的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot搭建go-cqhttp機(jī)器人的方法實(shí)現(xiàn)
本文主要介紹了SpringBoot搭建go-cqhttp機(jī)器人的方法實(shí)現(xiàn)2021-12-12
Spring Mvc中傳遞參數(shù)方法之url/requestMapping詳解
在開(kāi)發(fā)中,參數(shù)傳遞是必不可少的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于Spring Mvc中傳遞參數(shù)方法之url/requestMapping的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
關(guān)于@Scheduled參數(shù)及cron表達(dá)式解釋
這篇文章主要介紹了關(guān)于@Scheduled參數(shù)及cron表達(dá)式解釋?zhuān)哂泻芎玫膮⒖純r(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java使用WeakHashMap實(shí)現(xiàn)緩存自動(dòng)清理
在 Java 中,內(nèi)存管理是一個(gè)重要的話題,尤其是在涉及到緩存的實(shí)現(xiàn)時(shí),如果緩存項(xiàng)不再被使用,我們希望它們能被自動(dòng)清理,而不必手動(dòng)刪除,WeakHashMap 就是 Java 提供的一種用于緩存和內(nèi)存管理的工具,本文將深入探討如何利用 WeakHashMap 來(lái)實(shí)現(xiàn)緩存自動(dòng)清理2025-01-01
java虛擬機(jī)深入學(xué)習(xí)之內(nèi)存管理機(jī)制
java虛擬機(jī)在程序運(yùn)行時(shí)將內(nèi)存劃分為多個(gè)區(qū)域,每個(gè)區(qū)域作用,生命周期各不相同,下面這篇文章主要給大家介紹了關(guān)于java虛擬機(jī)深入學(xué)習(xí)之內(nèi)存管理機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-11-11

