1. 程式人生 > 實用技巧 >部署 seata 連線 nacos

部署 seata 連線 nacos

1:下載 seata 編譯後的程式碼包, 下載地址:https://github.com/seata/seata/tags。我下載的是zip的包

2:解壓包 gunzip seata-server-1.3.0.zip 。(沒有解壓命令gunzip的請自行安裝),解壓後包名 seata

3:修改config 目錄下的檔案

file.conf : 配置事物日誌的儲存方式(mysql , file ,redis 等) 預設是file。 為了高可用我改成 db

(1)mode = "db" // 儲存模式修改成db

(2)修改db配置中的屬性,資料庫我使用的是mysql,連線池使用的是 druid,所以只需要修改資料的 url, user, password 屬性即可。

注:mysql是我自己搭的,所以需要我們自己新建資料庫seata,並且還需要建表,新建表的指令碼在這裡:https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

4:registry.conf 檔案裡 配置著 seata-server 需要的服務註冊的服務, 和 配置檔案 的服務地址。 對應著springCloud中就是 eureka 和 配置中心

我的註冊中心使用nacos 所以,只需要改nacos的配置,

registry.file.name ="file.config" //file.config 就是上面的配置檔名稱

config.file.name="file.config" //file.config 就是上面的配置檔名稱

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

  nacos {
    application = "seata-server"
    serverAddr = "10.0.0.51:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = ""
    password 
= "" } file { name = "file.conf" } } config { # file、nacos 、apollo、zk、consul、etcd3 type = "file" nacos { serverAddr = "10.0.0.51:8848" namespace = "" group = "SEATA_GROUP" username = "" password = "" } file { name = "file.conf" } }

5:啟動服務

命令啟動: seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1

-h: 註冊到註冊中心的ip
-p: Server rpc 監聽埠
-m: 全域性事務會話資訊儲存模式,file、db、redis,優先讀取啟動引數 (Seata-Server 1.3及以上版本支援redis)
-n: Server node,多個Server時,需區分各自節點,用於生成不同區間的transactionId,以免衝突
-e: 多環境配置參考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html ()

注:

(1)當使用-e 環境變數時,你需要修改registry.conf 檔名稱,比如:registry-test.conf ,否則會丟擲異常

Exception in thread "main" io.seata.common.exception.NotSupportYetException: config type can not be null
    at io.seata.config.ConfigurationFactory.buildConfiguration(ConfigurationFactory.java:106)
    at io.seata.config.ConfigurationFactory.getInstance(ConfigurationFactory.java:90)
    at io.seata.server.metrics.MetricsManager.init(MetricsManager.java:49)
    at io.seata.server.Server.main(Server.java:75)

(2)啟動異常,這個問題是因為我的mysql使用的mysql8.0 版本,在 file.config中 store.db.driverClassName 預設是com.mysql.jdbc.Driver。 mysql8.0已經是com.mysql.cj.jdbc.Driver。所以修改驅動即可。

20-08-10 16:51:24.052  INFO --- [reate-278240974] com.alibaba.druid.pool.DruidDataSource   : put physical connection to pool failed.
2020-08-10 16:51:24.054 ERROR --- [reate-278240974] com.alibaba.druid.pool.DruidDataSource   : create connection holder error
==>
java.sql.SQLException: Could not retrieve transation read-only status server
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:904)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:894)
    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3613)
    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3582)
    at com.alibaba.druid.pool.DruidConnectionHolder.<init>(DruidConnectionHolder.java:135)
    at com.alibaba.druid.pool.DruidConnectionHolder.<init>(DruidConnectionHolder.java:75)
    at com.alibaba.druid.pool.DruidDataSource.put(DruidDataSource.java:2229)
    at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2547)
Caused by: java.sql.SQLException: Unknown system variable 'tx_read_only'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2531)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2489)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446)
    at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3607)
    ... 5 common frames omitted
<==

再次啟動執行正常。

檢視nacos 上seata-server服務是否註冊