seata server端配置信息修改

seata-server中,/conf目錄下,有兩個配置文件,需要結合自己的情況來修改:

1.file.conf
裏面有事務組配置,鎖配置,事務日誌存儲等相關配置信息,由於此demo使用db存儲事務信息,我們這裏要修改store中的配置:

transaction log store

store {

store mode: file、db

mode = “db” 修改這裏,表明事務信息用db存儲

file store 當mode=db時,此部分配置就不生效了,這是mode=file的配置

file {
dir = “sessionStore”

# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
max-branch-session-size = 16384
# globe session size , if exceeded throws exceptions
max-global-session-size = 512
# file buffer size , if exceeded allocate new buffer
file-write-buffer-cache-size = 16384
# when recover batch read size
session.reload.read_size = 100
# async, sync
flush-disk-mode = async

}

database store mode=db時,事務日誌存儲會存儲在這個配置的數據庫裏

db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = “dbcp”
## mysql/oracle/h2/oceanbase etc.
db-type = “mysql”
driver-class-name = “com.mysql.jdbc.Driver”
url = “jdbc:mysql://116.62.62.26/seat-server” 修改這裏
user = “root” 修改這裏
password = “root” 修改這裏
min-conn = 1
max-conn = 3
global.table = “global_table”
branch.table = “branch_table”
lock-table = “lock_table”
query-limit = 100
}
}
由於此demo我們使用db模式存儲事務日誌,所以,我們要創建三張表:global_table,branch_table,lock_table,建表sql在上面下載的seata-server的/conf/db_store.sql中;

由於存儲undo_log是在業務庫中,所以在每個業務庫中,還要創建undo_log表,建表sql在/conf/db_undo_log.sql中。

由於我自定義了事務組名稱,所以這裏也做了修改:

service {
#vgroup->rgroup
vgroup_mapping.fsp_tx_group = “default” 修改這裏,fsp_tx_group這個事務組名稱是我自定義的,一定要與client端的這個配置一致!否則會報錯!
#only support single node
default.grouplist = “127.0.0.1:8091” 此配置作用參考:https://blog.csdn.net/weixin_39800144/article/details/100726116
#degrade current not support
enableDegrade = false
#disable
disable = false
#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
max.commit.retry.timeout = “-1”
max.rollback.retry.timeout = “-1”
}
其他的可以先使用默認值。

2.registry.conf
registry{}中是註冊中心相關配置,config{}中是配置中心相關配置。seata中,註冊中心和配置中心是分開實現的,是兩個東西。

我們這裏用eureka作註冊中心,所以,只用修改registry{}中的:

registry {

file 、nacos 、eureka、redis、zk、consul、etcd3、sofa

type = “eureka” 修改這裏,指明註冊中心使用什麼

nacos {
serverAddr = “localhost”
namespace = “”
cluster = “default”
}
eureka {
serviceUrl = “http://localhost:8761/eureka” 修改這裏
application = “default”
weight = “1”
}
redis {
serverAddr = “localhost:6379”
db = “0”
}
zk {
cluster = “default”
serverAddr = “127.0.0.1:2181”
session.timeout = 6000
connect.timeout = 2000
}
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”
}
}
其他的配置可以暫時使用默認值。

如果是在windows下啓動seata-server,現在已經完成配置修改了,等eureka啓動後,就可以啓動seata-server了:執行/bin/seata-server.bat即可。

5.client端相關配置
1.普通配置
client端的幾個服務,都是普通的springboot整合了springCloud組件的正常服務,所以,你需要配置eureka,數據庫,mapper掃描等,即使不使用seata,你也需要做,這裏不做特殊說明,看代碼就好。

2.特殊配置
1.application.yml
以order服務爲例,除了常規配置外,這裏還要配置下事務組信息:

spring:
application:
name: order-server
cloud:
alibaba:
seata:
tx-service-group: fsp_tx_group 這個fsp_tx_group自定義命名很重要,server,client都要保持一致
2.file.conf
自己新建的項目是沒有這個配置文件的,copy過來,修改下面配置:

service {
#vgroup->rgroup
vgroup_mapping.fsp_tx_group = “default” 這個fsp_tx_group自定義命名很重要,server,client都要保持一致
#only support single node
default.grouplist = “127.0.0.1:8091”
#degrade current not support
enableDegrade = false
#disable
disable = false
disableGlobalTransaction = false
}
3.registry.conf
使用eureka做註冊中心,僅需要修改eureka的配置即可:

registry {

file 、nacos 、eureka、redis、zk

type = “eureka” 修改這裏

nacos {
serverAddr = “localhost”
namespace = “public”
cluster = “default”
}
eureka {
serviceUrl = “http://localhost:8761/eureka” 修改這裏
application = “default”
weight = “1”
}
redis {
serverAddr = “localhost:6381”
db = “0”
}
zk {
cluster = “default”
serverAddr = “127.0.0.1:2181”
session.timeout = 6000
connect.timeout = 2000
}
file {
name = “file.conf”
}
}
其他的使用默認值就好。

3.數據源代理
這個是要特別注意的地方,seata對數據源做了代理和接管,在每個參與分佈式事務的服務中,都要做如下配置:

/**

  • 數據源代理

  • @author wangzhongxiang
    */
    @Configuration
    public class DataSourceConfiguration {

    @Bean
    @ConfigurationProperties(prefix = “spring.datasource”)
    public DataSource druidDataSource(){
    DruidDataSource druidDataSource = new DruidDataSource();
    return druidDataSource;
    }

    @Primary
    @Bean(“dataSource”)
    public DataSourceProxy dataSource(DataSource druidDataSource){
    return new DataSourceProxy(druidDataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy)throws Exception{
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSourceProxy);
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
    .getResources(“classpath*:/mapper/*.xml”));
    sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
    return sqlSessionFactoryBean.getObject();
    }

}
6.啓動測試
1.啓動eureka;
2.啓動seata-server;
3.啓動order,storage,account服務;
4.訪問:http://localhost:8180/order/create?userId=1&productId=1&count=10&money=100
然後可以模擬正常情況,異常情況,超時情況等,觀察數據庫即可。

這個demo,未做各種優化,如果壓測,需要修改和優化一些配置,壓測出錯了,不一定是seata的鍋,自己先排查,再去羣裏問問。

7.日誌
正常情況:

1.order
2019-09-06 15:44:33.536 INFO 53904 — [io-8080-exec-10] i.seata.tm.api.DefaultGlobalTransaction : Begin new global transaction [192.168.158.133:8091:2021468859]
2019-09-06 15:44:33.536 INFO 53904 — [io-8080-exec-10] c.j.order.service.OrderServiceImpl : ------->交易開始
2019-09-06 15:44:34.376 INFO 53904 — [io-8080-exec-10] c.j.order.service.OrderServiceImpl : ------->交易結束
2019-09-06 15:44:34.593 INFO 53904 — [io-8080-exec-10] i.seata.tm.api.DefaultGlobalTransaction : [192.168.158.133:8091:2021468859] commit status:Committed
2019-09-06 15:44:35.296 INFO 53904 — [atch_RMROLE_6_8] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.158.133:8091:2021468859,branchId=2021468861,branchType=AT,resourceId=jdbc:mysql://116.62.62.26/seat-order,applicationData=null
2019-09-06 15:44:35.297 INFO 53904 — [atch_RMROLE_6_8] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.158.133:8091:2021468859 2021468861 jdbc:mysql://116.62.62.26/seat-order null
2019-09-06 15:44:35.297 INFO 53904 — [atch_RMROLE_6_8] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
2.storage
2019-09-06 15:44:33.776 INFO 9704 — [nio-8082-exec-1] c.j.storage.service.StorageServiceImpl : ------->扣減庫存開始
2019-09-06 15:44:34.030 INFO 9704 — [nio-8082-exec-1] c.j.storage.service.StorageServiceImpl : ------->扣減庫存結束
2019-09-06 15:44:35.422 INFO 9704 — [atch_RMROLE_5_8] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.158.133:8091:2021468859,branchId=2021468864,branchType=AT,resourceId=jdbc:mysql://116.62.62.26/seat-storage,applicationData=null
2019-09-06 15:44:35.423 INFO 9704 — [atch_RMROLE_5_8] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.158.133:8091:2021468859 2021468864 jdbc:mysql://116.62.62.26/seat-storage null
2019-09-06 15:44:35.423 INFO 9704 — [atch_RMROLE_5_8] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
3.account
2019-09-06 15:44:34.039 INFO 36556 — [nio-8081-exec-5] c.j.account.service.AccountServiceImpl : ------->扣減賬戶開始
2019-09-06 15:44:34.039 INFO 36556 — [nio-8081-exec-5] c.j.account.service.AccountServiceImpl : ------->扣減賬戶結束
2019-09-06 15:44:35.545 INFO 36556 — [atch_RMROLE_3_8] i.s.core.rpc.netty.RmMessageListener : onMessage:xid=192.168.158.133:8091:2021468859,branchId=2021468867,branchType=AT,resourceId=jdbc:mysql://116.62.62.26/seat-account,applicationData=null
2019-09-06 15:44:35.545 INFO 36556 — [atch_RMROLE_3_8] io.seata.rm.AbstractRMHandler : Branch committing: 192.168.158.133:8091:2021468859 2021468867 jdbc:mysql://116.62.62.26/seat-account null
2019-09-06 15:44:35.545 INFO 36556 — [atch_RMROLE_3_8] io.seata.rm.AbstractRMHandler : Branch commit result: PhaseTwo_Committed
8.模擬異常
在AccountServiceImpl中模擬異常情況,然後可以查看日誌

/**
 * 扣減賬戶餘額
 * @param userId 用戶id
 * @param money 金額
 */
@Override
public void decrease(Long userId, BigDecimal money) {
    LOGGER.info("------->扣減賬戶開始");

// try {
// Thread.sleep(30*1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
LOGGER.info("------->扣減賬戶結束");
accountDao.decrease(userId,money);
}
9.調用成環
前面的調用鏈爲order->storage->account; 這裏測試的成環是指order->storage->account->order, 這裏的account服務又會回頭去修改order在前面添加的數據。 經過測試,是支持此種場景的。

/**
 * 扣減賬戶餘額
 * @param userId 用戶id
 * @param money 金額
 */
@Override
public void decrease(Long userId, BigDecimal money) {
    LOGGER.info("------->扣減賬戶開始account中");
    //模擬超時異常,全局事務回滾

// try {
// Thread.sleep(30*1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
accountDao.decrease(userId,money);
LOGGER.info("------->扣減賬戶結束account中");

    //修改訂單狀態,此調用會導致調用成環
    LOGGER.info("修改訂單狀態開始");
    String mes = orderApi.update(userId, money.multiply(new BigDecimal("0.09")),0);
    LOGGER.info("修改訂單狀態結束:{}",mes);
}

在最初的order會創建一個訂單,然後扣減庫存,然後扣減賬戶,賬戶扣減完,會回頭修改訂單的金額和狀態,這樣調用就成環了。

10.seata-server HA
下載seata server包,地址:https://github.com/seata/seata/releases;

部署集羣,第一臺和第二臺配置相同,在server端的registry.conf中,注意:

registry {

file 、nacos 、eureka、redis、zk、consul、etcd3、sofa

type = “eureka”

eureka {
serviceUrl = “http://192.168.xx.xx:8761/eureka” //兩臺tcc相同,註冊中心的地址
application = “default” //兩臺tc相同
weight = “1” //權重,截至0.9版本,暫時不支持此參數
}

注意上述配置和client的配置要一致,2臺和多臺情況相同。

0.9及之前版本,多tc時,tc會誤報異常,此問題0.9之後已經修復,之後的版本應該不會出現此問題。

發佈了18 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章