Springboot微服務(wù)整合Seata分布式事務(wù)一:服務(wù)端搭建

Seata Server服務(wù)端搭建

一、官網(wǎng)地址

Seata 文檔

Seata github

Seata releases

二显沈、Seata Server 下載

這里地址為 1.3.0版本

seata-server-1.3.0

  • Linux
wget https://github.com/seata/seata/releases/download/v1.3.0/seata-server-1.3.0.zip

三汇跨、修改配置文件

配置文件主要有:registry.conf file.conf

  • registry.conf : Seata 服務(wù)注冊(cè)中心地址坝辫,配置中心地址

  • file.conf : 當(dāng)配置中心使用 file時(shí),此配置文件生效狸涌。配置store存儲(chǔ)配置

    這里我們使用eureka做為注冊(cè)中心,Apollo做為配置中心

registry.conf

registry {
  # file 、nacos 胀屿、eureka、redis包雀、zk宿崭、consul、etcd3馏艾、sofa
  type = "eureka"
  # 配置eureka地址
  eureka {
    serviceUrl = "http://10.0.17.92:9001/eureka"
    application = "seata-server"
    weight = "1"
  }
  ...
  file {
    name = "file.conf"
  }
}

config {
  # file劳曹、nacos 、apollo琅摩、zk铁孵、consul、etcd3
  #type = "file"
  type = "apollo"
  # 配置中心配置apollo地址和namespace
  apollo {
    appId = "seata-server"
    apolloMeta = "http://10.0.17.92:9083"
    namespace = "application"
  }
  file {
    name = "file.conf"
  }
}

file.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file房资、db蜕劝、redis
  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
  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"
    url = "jdbc:mysql://10.0.17.122:3306/seata"
    user = "root"
    password = "Pdhn^456"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

  ## redis store property
  redis {
    host = "127.0.0.1"
    port = "6379"
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    queryLimit = 100
  }
}

Apollo配置Server配置

主要修改:數(shù)據(jù)庫地址 用戶名 密碼等

  • 注意:mysql 版本問題 1.3.0后,Seata使用mysql 8.0版本
  • 使用 8.0版本后轰异,driverClassName = com.mysql.cj.jdbc.Driver 岖沛。否則會(huì)報(bào)錯(cuò):
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server
transport.type = TCP
transport.server = NIO
transport.heartbeat = true
transport.enableClientBatchSendRequest = false
transport.threadFactory.bossThreadPrefix = NettyBoss
transport.threadFactory.workerThreadPrefix = NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix = NettyServerBizHandler
transport.threadFactory.shareBossWorker = false
transport.threadFactory.clientSelectorThreadPrefix = NettyClientSelector
transport.threadFactory.clientSelectorThreadSize = 1
transport.threadFactory.clientWorkerThreadPrefix = NettyClientWorkerThread
transport.threadFactory.bossThreadSize = 1
transport.threadFactory.workerThreadSize = default
transport.shutdown.wait = 3
transport.serialization = seata
transport.compressor = none

store.mode = db
store.db.datasource = druid
store.db.dbType = mysql
store.db.driverClassName = com.mysql.cj.jdbc.Driver
store.db.url = jdbc:mysql://10.0.17.122:3306/seata?useUnicode=true
store.db.user = root
store.db.password = Pdhn^456
store.db.minConn = 5
store.db.maxConn = 30
store.db.globalTable = global_table
store.db.branchTable = branch_table
store.db.queryLimit = 100
store.db.lockTable = lock_table
store.db.maxWait = 5000

server.recovery.committingRetryPeriod = 1000
server.recovery.asynCommittingRetryPeriod = 1000
server.recovery.rollbackingRetryPeriod = 1000
server.recovery.timeoutRetryPeriod = 1000
server.maxCommitRetryTimeout = -1
server.maxRollbackRetryTimeout = -1
server.rollbackRetryTimeoutUnlockEnable = false

metrics.enabled = false
metrics.registryType = compact

初始化數(shù)據(jù)庫

  • global_table 維護(hù)全局事務(wù)
  • branch_table 維護(hù)分支事務(wù)
  • lock_table 維護(hù)全局鎖
-- the table to store GlobalSession data
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`)
);

-- the table to store BranchSession data
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`)
);

-- the table to store lock data
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`)
);

四、Seata-Server啟動(dòng)

  • 先保證配置eureka apollo配置搭独,啟動(dòng)注冊(cè)中心和配置中心
  • Apollo 配置

[圖片上傳失敗...(image-646e30-1623312838097)]

  • 進(jìn)入到: seata/bin目錄
nohup sh seata-server.sh -h 10.0.17.92 -p 8091 &
  • Eureka

[圖片上傳失敗...(image-c5fe2a-1623312838097)]

五婴削、Docker啟動(dòng) Seata server

1. 拉取 seata-server鏡像

docker pull docker.io/seataio/seata-server:1.3.0

2. 修改配置文件registry.conf

  • 配置 Apollo
registry {
 # file 、nacos 牙肝、eureka唉俗、redis嗤朴、zk、consul虫溜、etcd3雹姊、sofa
 type = "eureka"
 loadBalance = "RandomLoadBalance"
 loadBalanceVirtualNodes = 10

 eureka {
   serviceUrl = "http://10.0.17.92:9001/eureka"
   application = "seata-server"
   weight = "1"
 }

 file {
   name = "file.conf"
 }
}

config {
 # file、nacos 衡楞、apollo吱雏、zk、consul瘾境、etcd3
 type = "apollo"

 apollo {
   appId = "seata-server"
   apolloMeta = "http://10.0.17.92:9083"
   namespace = "application"
 }

 file {
   name = "file.conf"
 }
}

3. 啟動(dòng)腳本

start.sh

SEATA_PORT : 自定義端口

SERVER_NODE: 集群環(huán)境下歧杏,指定節(jié)點(diǎn)

seata日志目錄:/root/logs/seata ,可掛載到服務(wù)器本地目錄

#!/bin/bash
 
docker stop seata-server;
docker container rm seata-server;
 
docker run --name seata-server -it -d  -p 8091:8091 \
-e SEATA_CONFIG_NAME=file:/root/seata/config/registry \
-e SEATA_IP=10.0.17.92 \
-e SEATA_PORT=8091 \
-e SERVER_NODE=1 \
-v /opt/logs/seata:/root/logs/seata \
-v /opt/seata/config:/root/seata/config \
- v /opt/seata/config/libs/mysql-connector-java-8.0.21.jar:/root/seata/config/libs/mysql-connector-java-8.0.21.jar \
--net=bridge --restart=always docker.io/seataio/seata-server:1.3.0

六寄雀、 Docker Compose 啟動(dòng) Seata server

  • 注意數(shù)據(jù)庫版本問題得滤,1.3.0版本后 mysql驅(qū)動(dòng)為 8.0版本

    - 在config目錄中,配置`mysql-connector-java-8.0.21.jar`驅(qū)動(dòng)
    
  • Apollo數(shù)據(jù)庫驅(qū)動(dòng) cj盒犹,連接地址加 characterEncoding=utf-8&serverTimezone=UTC

store.db.driverClassName = com.mysql.cj.jdbc.Driver
store.db.url = jdbc:mysql://10.0.17.122:3306/seata?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
  • 啟動(dòng)容器
docker-compose up -d 

1. docker-compose.yml配置文件

version: "3"
services:
  seata-server:
    image: seataio/seata-server:1.3.0
    container_name: seata-server
    hostname: seata-server
    ports:
      - "8091:8091"
    volumes:
      - "/opt/seata/config:/root/seata/config"
      - "/opt/seata/config/libs/mysql-connector-java-8.0.21.jar:/root/seata/config/libs/mysql-connector-java-8.0.21.jar"
    environment:
      - SEATA_CONFIG_NAME=file:/root/seata/config/registry
      - SEATA_IP=10.0.17.92
      - SEATA_PORT=8091
       - STORE_MODE=db
    restart: always
    deploy:
      resources:
        limits:
          cpus: '0.3'
          memory: 300M  

2. registry.conf

registry {
  # file 懂更、nacos 、eureka急膀、redis沮协、zk、consul卓嫂、etcd3慷暂、sofa
  type = "eureka"
  loadBalance = "RandomLoadBalance"
  loadBalanceVirtualNodes = 10

  eureka {
    serviceUrl = "http://10.0.17.92:9001/eureka"
    application = "seata-server"
    weight = "1"
  }

  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 晨雳、apollo行瑞、zk、consul餐禁、etcd3
  type = "apollo"

  apollo {
    appId = "seata-server"
    apolloMeta = "http://10.0.17.92:9083"
    namespace = "application"
  }
 
  file {
    name = "file:/root/seata/config/file.conf"
  }
}

3. 異常 database

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server

15:09:54.010 ERROR --- [ionPool-Create-1072506992] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://10.0.17.122:3306/seata?useUnicode=true, errorCode 0, state 08001
==>
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
        at com.mysql.jdbc.Util.getInstance(Util.java:372)

4. 進(jìn)入seata server 容器

[root@lipei92 seata]# docker ps|grep seata
55af30b183b7        seataio/seata-server:1.4.1          "java -Djava.secur..."   4 minutes ago       Up 8 seconds          0.0.0.0:8091->8091/tcp                                             seata-server
[root@lipei92 seata]# docker exec -it 55af30b183b7 sh
/seata-server # pwd
/seata-server

九血久、參考文檔

  • Docker-compose主要參考

使用docker安裝seata-server,mysql8引擎帮非,nacos作為注冊(cè)和配置中心

  • Docker 主要參考文檔

docker快速部署一個(gè)seata-server+向nacos推送配置

  • Seata Server 數(shù)據(jù)版本升級(jí) 8.0后報(bào)異常

Mysql驅(qū)動(dòng)類導(dǎo)致拋異常

圖片參考地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末氧吐,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子末盔,更是在濱河造成了極大的恐慌筑舅,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,454評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件陨舱,死亡現(xiàn)場(chǎng)離奇詭異翠拣,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)游盲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門误墓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來邦尊,“玉大人,你說我怎么就攤上這事优烧。” “怎么了链峭?”我有些...
    開封第一講書人閱讀 157,921評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵畦娄,是天一觀的道長。 經(jīng)常有香客問我弊仪,道長熙卡,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,648評(píng)論 1 284
  • 正文 為了忘掉前任励饵,我火速辦了婚禮驳癌,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘役听。我一直安慰自己颓鲜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評(píng)論 6 386
  • 文/花漫 我一把揭開白布典予。 她就那樣靜靜地躺著甜滨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瘤袖。 梳的紋絲不亂的頭發(fā)上衣摩,一...
    開封第一講書人閱讀 49,950評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音捂敌,去河邊找鬼艾扮。 笑死,一個(gè)胖子當(dāng)著我的面吹牛占婉,可吹牛的內(nèi)容都是我干的泡嘴。 我是一名探鬼主播,決...
    沈念sama閱讀 39,090評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼锐涯,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼磕诊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起纹腌,我...
    開封第一講書人閱讀 37,817評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤霎终,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后升薯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體莱褒,經(jīng)...
    沈念sama閱讀 44,275評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評(píng)論 2 327
  • 正文 我和宋清朗相戀三年涎劈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了广凸。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阅茶。...
    茶點(diǎn)故事閱讀 38,724評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖谅海,靈堂內(nèi)的尸體忽然破棺而出脸哀,到底是詐尸還是另有隱情,我是刑警寧澤扭吁,帶...
    沈念sama閱讀 34,409評(píng)論 4 333
  • 正文 年R本政府宣布撞蜂,位于F島的核電站,受9級(jí)特大地震影響侥袜,放射性物質(zhì)發(fā)生泄漏蝌诡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評(píng)論 3 316
  • 文/蒙蒙 一枫吧、第九天 我趴在偏房一處隱蔽的房頂上張望浦旱。 院中可真熱鬧,春花似錦九杂、人聲如沸颁湖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽爷狈。三九已至,卻和暖如春裳擎,著一層夾襖步出監(jiān)牢的瞬間涎永,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評(píng)論 1 266
  • 我被黑心中介騙來泰國打工鹿响, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留羡微,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,503評(píng)論 2 361
  • 正文 我出身青樓惶我,卻偏偏與公主長得像妈倔,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绸贡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評(píng)論 2 350

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