springcloud+eureka+seata(AT模式)解決分布式事務(wù)

  1. 我這里是Centos7薯定,第一步先安裝java環(huán)境髓棋;
  2. 下載steata包,下載地址:seata1.4.2
  3. 上傳到linux的/software/目錄后解壓
cd /software/
tar -zxvf seata-server-1.4.2.tar.gz
cd /seata/seata-server-1.4.2

4.修改registry.conf文件

registry {
  # file 腰根、nacos 扯夭、eureka、redis梦抢、zk般贼、consul、etcd3奥吩、sofa
  # 指定注冊(cè)中心為eureka哼蛆,以下只需要配置eureka相關(guān)配置參數(shù)即可
  type = "eureka"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = ""
    password = ""
  }
  eureka {
    #修改了宿主機(jī)的hosts,將注冊(cè)中心的ip統(tǒng)一為指定域名霞赫,方便維護(hù)
    serviceUrl = "http://127.0.0.1:8761/eureka"
    #自定義注冊(cè)中心服務(wù)名
    application = "SEATA"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = ""
    cluster = "default"
    timeout = 0
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file腮介、nacos 、apollo端衰、zk叠洗、consul、etcd3
  type = "file"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = ""
    password = ""
    dataId = "seataServer.properties"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
    aclToken = ""
  }
  apollo {
    appId = "seata-server"
    ## apolloConfigService will cover apolloMeta
    apolloMeta = "http://192.168.1.204:8801"
    apolloConfigService = "http://192.168.1.204:8080"
    namespace = "application"
    apolloAccesskeySecret = ""
    cluster = "seata"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
    nodePath = "/seata/seata.properties"
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    # 存儲(chǔ)模式的配置
    name = "file.conf"
  }
}

5.修改file.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file旅东、db灭抑、redis
  # 指定存儲(chǔ)模式為db,集群必須配置為db
  mode = "db"
  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }

  ## database store property
  # 第一步創(chuàng)建數(shù)據(jù)表的數(shù)據(jù)庫(kù)配置參數(shù)
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.cj.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://xxx:3306/test_seata"
    user = "xxx"
    password = "xxx"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

  ## redis store property
  redis {
    ## redis mode: single抵代、sentinel
    mode = "single"
    ## single mode property
    single {
      host = "127.0.0.1"
      port = "6379"
    }
    ## sentinel mode property
    sentinel {
      masterName = ""
      ## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381"
      sentinelHosts = ""
    }
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    maxTotal = 100
    queryLimit = 100
  }
}
  1. 啟動(dòng)
#在test_seata數(shù)據(jù)庫(kù)建以下幾張表腾节,建表語(yǔ)句:
drop table if exists `global_table`;
create table `global_table` (
  `xid` varchar(128)  not null,
  `transaction_id` bigint,
  `status` tinyint not null,
  `application_id` varchar(32),
  `transaction_service_group` varchar(32),
  `transaction_name` varchar(128),
  `timeout` int,
  `begin_time` bigint,
  `application_data` varchar(2000),
  `gmt_create` datetime,
  `gmt_modified` datetime,
  primary key (`xid`),
  key `idx_gmt_modified_status` (`gmt_modified`, `status`),
  key `idx_transaction_id` (`transaction_id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

drop table if exists `branch_table`;
create table `branch_table` (
  `branch_id` bigint not null,
  `xid` varchar(128) not null,
  `transaction_id` bigint ,
  `resource_group_id` varchar(32),
  `resource_id` varchar(256) ,
  `lock_key` varchar(128) ,
  `branch_type` varchar(8) ,
  `status` tinyint,
  `client_id` varchar(64),
  `application_data` varchar(2000),
  `gmt_create` datetime,
  `gmt_modified` datetime,
  primary key (`branch_id`),
  key `idx_xid` (`xid`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

drop table if exists `lock_table`;
create table `lock_table` (
  `row_key` varchar(128) not null,
  `xid` varchar(96),
  `transaction_id` long ,
  `branch_id` long,
  `resource_id` varchar(256) ,
  `table_name` varchar(32) ,
  `pk` varchar(36) ,
  `gmt_create` datetime ,
  `gmt_modified` datetime,
  primary key(`row_key`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

#后臺(tái)運(yùn)行
nohup ./bin/seata-server.sh -p 8091 -h 127.0.0.1 -m db &> ./logs/seata.log &
#查看日志,看到-Server started即成功,再打開(kāi)eureka是否有注冊(cè)上去
tail -f ./logs/seata.log
  • -p 指定啟動(dòng)seata server的端口號(hào)禀倔;
  • -h 指定seata server所綁定的主機(jī)榄融;
  • -m 事務(wù)日志、事務(wù)執(zhí)行信息存儲(chǔ)的方式救湖,目前支持file(文件方式)愧杯、db(數(shù)據(jù)庫(kù)方式,建表語(yǔ)句請(qǐng)查看步驟6)
  1. 在項(xiàng)目中使用鞋既,添加依賴(lài)(seata-spring-boot-starter方式力九,可直接在application.yml/application.properties中進(jìn)行配置,但是需要手動(dòng)傳遞XID邑闺;而使用spring-cloud-alibaba-seata跌前,需要在每個(gè)微服務(wù)中添加registry.conf和file.conf文件,這里結(jié)合):
      <dependency>
          <groupId>com.alibaba.cloud</groupId>
          <artifactId>spring-cloud-alibaba-seata</artifactId>
          <version>2.2.0.RELEASE</version>
      </dependency>
      <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
  1. application.yml中添加:
#seata 配置陡舅, 代替file.conf和registry.conf配置
seata:
  enabled: true
  # 你的服務(wù)名稱(chēng)
  application-id: ${spring.application.name}
  #自定義group
  tx-service-group: test-seata
  # 開(kāi)啟數(shù)據(jù)源自動(dòng)代理
  enable-auto-data-source-proxy: true
  service:
    vgroup-mapping:
      #這里對(duì)應(yīng)端的file.conf中vgroup_mapping.seata_server_group = "seata-server"
     test-seata: SEATA
      enable-degrade: false
      disable-global-transaction: false
  # Eureka配置
  registry:
    type: eureka
    eureka:
      service-url: http://127.0.0.1:8761/eureka

9.seata1.1.0以后抵乓,不需要手動(dòng)代理數(shù)據(jù)源,啟動(dòng)類(lèi)上@SpringBootApplication(exclude = DataSourceAutoConfiguration.class):


自定義數(shù)據(jù)源
  1. 在你需要使用feign進(jìn)行微服務(wù)間調(diào)到的方法上添加注解@GlobalTransactional


    注解
  2. 每個(gè)涉及到分布式事務(wù)的微服務(wù)的數(shù)據(jù)庫(kù)都要有一張undo_log日志表靶衍,sql如下:

DROP TABLE IF EXISTS `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=utf8mb4 COLLATE=utf8mb4_general_ci;
  1. 在需要微服務(wù)調(diào)用的微服務(wù)中進(jìn)行以上相同配置(7.8.9.10.12),除了@GlobalTransactional注解灾炭,之后便可以?huà)伋霎惓?词欠窕貪L成功
  • 注:seata集群只需要再以其他端口啟動(dòng)seata文件目錄即可


    seata集群
  1. docker-compose編排部署:
version: "3"
services:
  seata-server:
    image: seataio/seata-server:1.4.2
    container_name: seata-server
    hostname: seata-server
    networks:
      - seata-server
    ports:
      - "8091:8091"
    environment:
      #宿主機(jī)ip
      - SEATA_PORT=8091
      # 指定seata-server的事務(wù)日志存儲(chǔ)方式, 支持db ,file(默認(rèn)),redis
      - STORE_MODE=db
      #seata的被發(fā)現(xiàn)地址颅眶,該IP用于向注冊(cè)中心注冊(cè)時(shí)使用(也就是自身的ip)
      - SEATA_IP=127.0.0.1
      #給予權(quán)限
      #- privileged=true
    volumes:
      - /software/docker/seata/config/registry.conf:/seata-server/resources/registry.conf
      - /software/docker/seata/config/file.conf:/seata-server/resources/file.conf
      - /software/docker/seata/mysql-connector-java-8.0.16.jar:/seata-server/libs/mysql-connector-java-8.0.16.jar
      - /project/docker/seata/logs:/root/logs/seata
      - /etc/localtime:/etc/localtime
networks:
  seata-server:
    driver: bridge

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蜈出,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子涛酗,更是在濱河造成了極大的恐慌铡原,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,482評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件商叹,死亡現(xiàn)場(chǎng)離奇詭異燕刻,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)剖笙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)酌儒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人枯途,你說(shuō)我怎么就攤上這事忌怎。” “怎么了酪夷?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,762評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵榴啸,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我晚岭,道長(zhǎng)鸥印,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,273評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮库说,結(jié)果婚禮上狂鞋,老公的妹妹穿的比我還像新娘。我一直安慰自己潜的,他們只是感情好骚揍,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,289評(píng)論 5 373
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著啰挪,像睡著了一般信不。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上亡呵,一...
    開(kāi)封第一講書(shū)人閱讀 49,046評(píng)論 1 285
  • 那天抽活,我揣著相機(jī)與錄音,去河邊找鬼锰什。 笑死下硕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的汁胆。 我是一名探鬼主播梭姓,決...
    沈念sama閱讀 38,351評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼沦泌!你這毒婦竟也來(lái)了糊昙?” 一聲冷哼從身側(cè)響起辛掠,我...
    開(kāi)封第一講書(shū)人閱讀 36,988評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤谢谦,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后萝衩,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體回挽,經(jīng)...
    沈念sama閱讀 43,476評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,948評(píng)論 2 324
  • 正文 我和宋清朗相戀三年猩谊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了千劈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,064評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡牌捷,死狀恐怖墙牌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情暗甥,我是刑警寧澤喜滨,帶...
    沈念sama閱讀 33,712評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站撤防,受9級(jí)特大地震影響虽风,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,261評(píng)論 3 307
  • 文/蒙蒙 一辜膝、第九天 我趴在偏房一處隱蔽的房頂上張望无牵。 院中可真熱鬧,春花似錦厂抖、人聲如沸茎毁。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,264評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)充岛。三九已至,卻和暖如春耕蝉,著一層夾襖步出監(jiān)牢的瞬間崔梗,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,486評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工垒在, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蒜魄,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,511評(píng)論 2 354
  • 正文 我出身青樓场躯,卻偏偏與公主長(zhǎng)得像谈为,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子踢关,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,802評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 一.整體搭建準(zhǔn)備 一.1.下載seata-server服務(wù)端(地址:https://seata.io/zh-cn...
    程流兒閱讀 613評(píng)論 1 2
  • 3. 分布式事務(wù)解決方案之2PC(兩階段提交) 針對(duì)不同的分布式場(chǎng)景業(yè)界常見(jiàn)的解決方案有2PC伞鲫、TCC、可靠消息最...
    小小一技術(shù)驛站閱讀 1,737評(píng)論 2 2
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月签舞,有人笑有人哭秕脓,有人歡樂(lè)有人憂(yōu)愁,有人驚喜有人失落儒搭,有的覺(jué)得收獲滿(mǎn)滿(mǎn)有...
    陌忘宇閱讀 8,523評(píng)論 28 53
  • 人工智能是什么吠架?什么是人工智能?人工智能是未來(lái)發(fā)展的必然趨勢(shì)嗎搂鲫?以后人工智能技術(shù)真的能達(dá)到電影里機(jī)器人的智能水平嗎...
    ZLLZ閱讀 3,763評(píng)論 0 5
  • 上周六在壓力下實(shí)在需要釋放傍药,去西塘躲了兩天,真是好地方魂仍,很優(yōu)雅的江南古鎮(zhèn)拐辽,而且與周莊比開(kāi)發(fā)不算過(guò)度。 我們是周五半...
    聚塔閱讀 800評(píng)論 2 2