天天看點

seata整合spring cloud alibaba并把nacos作為seata的注冊中心和配置中心

一、下載下傳seata服務端seata-server

seata-server下載下傳位址:https://seata.io/zh-cn/blog/download.html

這裡以seata-server-1.3為例:https://github.com/seata/seata/releases/download/v1.3.0/seata-server-1.3.0.zip

二、執行官網提供的sql檔案,修改registry.conf配置檔案

1、執行官網提供的sql檔案建表

https://github.com/seata/seata/tree/1.3.0/script/server/db

2、修改registry.conf配置檔案

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "你的nacos位址"
    group = "SEATA_GROUP"
    namespace = "命名空間"
    cluster = "default"
    username = "nacos使用者名"
    password = "nacos密碼"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    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"
  }
  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 = "nacos"

  nacos {
    serverAddr = "你的nacos位址"
    namespace = "命名空間"
    group = "SEATA_GROUP"
    username = "nacos使用者名"
    password = "nacos密碼"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    appId = "seata-server"
    apolloMeta = "http://192.168.1.204:8801"
    namespace = "application"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

           

三、将配置資訊上傳至nacos配置中心

1、官方已經提供好了config.txt,下載下傳位址:https://github.com/seata/seata/tree/develop/script/config-center

2、修改config.txt配置檔案中的内容(ps:也可以上傳至nacos後在nacos配置中心上進行修改)

需要修改的内容如下:

service.vgroupMapping.nanyiba_global_tx_group=default
store.mode=db
store.db.url=jdbc:mysql://mysqlip:mysql端口号/庫名?useUnicode=true
store.db.user=mysql賬戶
store.db.password=mysql密碼

nanyiba_global_tx_group是要修改的地方,這裡修改的内容,後面整合時需要的
           

3、将config.txt中的配置資訊上傳至nacos配置中心

執行指令如下:

sh nacos-config.sh -h 你的nacosIP位址 -p 你的nacos端口 -g SEATA_GROUP -t 命名空間 -u nacos使用者嗎 -w nacos密碼
           

四、啟動seata-server服務端

啟動指令:
./bin/seata-server.sh -p 8091 -h 你部署seata-server伺服器的ip位址 -p 你想配置seata-server的端口号 -m db

具體參數文檔:https://seata.io/zh-cn/docs/ops/deploy-server.html

ps:這裡如果不指定ip位址,後面整合時,應用啟動時它找的是你seata-server内網ip位址
           

五、seata整合spring cloud alibaba

1、pom引入依賴

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
           

2、application.yml配置檔案

spring:
  cloud:
    alibaba:
      seata:
        tx-service-group: nanyiba-global-tx-group #事務分組名稱
seata:
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: nacosd位址
      group: SEATA_GROUP
      namespace: 命名空間
      cluster: default
      username: nacos賬戶
      password: nacos密碼
  config:
    type: nacos
    nacos:
      server-addr: nacosd位址
      group: SEATA_GROUP
      namespace: 命名空間
      username: nacos賬戶
      password: nacos密碼
           

六、可能會出現的問題

問題:no available service ‘null’ found, please make sure registry config correct

提示:檢查application.yml配置檔案的事務分組名稱是否和nacos配置中心的保持一緻

seata整合spring cloud alibaba并把nacos作為seata的注冊中心和配置中心