溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

分布式事務(wù)seata1.3.0如何整合nacos

發(fā)布時(shí)間:2021-12-22 16:57:17 來(lái)源:億速云 閱讀:143 作者:小新 欄目:云計(jì)算

這篇文章給大家分享的是有關(guān)分布式事務(wù)seata1.3.0如何整合nacos的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

分布式事務(wù) seata-demo

版本介紹

JDK:1.8
spring-cloud.version:Hoxton.SR8
alibaba.version: 2.2.1.RELEASE
seata.version:1.3.0
nacos: 1.3.1

整合步驟
  1. 下載nacos我下載的nacos-server-1.3.2.zip

  2. seata我下載的是 seata-server-1.3.0.zip

  3. 創(chuàng)建數(shù)據(jù)庫(kù) server 數(shù)據(jù)庫(kù)腳本 同時(shí)每一個(gè)參與事務(wù)的數(shù)據(jù)庫(kù)需要添加一張表 下載地址

  4. 上傳seata配置到nacos

    1. 下載nacos-config.sh文件,放到解壓文件conf文件夾下

    2. 修改conf目錄下file.conf配置文件,主要是修改自定義事務(wù)組名稱,事務(wù)日志存儲(chǔ)模式為db,并修改數(shù)據(jù)庫(kù)連接信息

      service {
        #vgroup->rgroup
        vgroup_mapping.tx-service-group = "default" #修改事務(wù)組名稱為:tx-service-group,和客戶端自定義的名稱對(duì)應(yīng)
        #only support single node
        default.grouplist = "127.0.0.1:8091"
        #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"
      }
      ## transaction log store, only used in seata-server
      store {
        ## store mode: file、db、redis
        mode = "db"
        ## database store property
        db {
      	## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
      	datasource = "druid"
      	## mysql/oracle/postgresql/h3/oceanbase etc.
      	dbType = "mysql"
      	driverClassName = "com.mysql.jdbc.Driver"
      	url = "jdbc:mysql://192.168.240.129:3306/seat_server"
      	user = "root"
      	password = "Root_123456"
      	minConn = 5
      	maxConn = 30
      	globalTable = "global_table"
      	branchTable = "branch_table"
      	lockTable = "lock_table"
      	queryLimit = 100
      	maxWait = 5000
        }
      }


    3. 修改conf目錄下 registry.conf配置,指明注冊(cè)中心為nacos,及修改nacos連接信息。

      registry {
        # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
        type = "nacos"
        nacos {
      	application = "seata-server"
      	serverAddr = "127.0.0.1:8848"
      	group = "default"
      	namespace = ""
      	cluster = "default"
      	username = "nacos"
      	password = "nacos"
        }
      }
      
      config {
        # file、nacos 、apollo、zk、consul、etcd3  SEATA_GROUP
        type = "nacos"
      
        nacos {
      	serverAddr = "127.0.0.1:8848"
      	namespace = "default"
      	group = ""
      	username = "nacos"
      	password = "nacos"
        }
      }


    4. 上傳配置到nacos: 在conf目錄下打開(kāi)git bash here 執(zhí)行nacos-config.sh 輸入命令 sh nacos-config.sh -h 127.0.0.1 分布式事務(wù)seata1.3.0如何整合nacos

  5. 創(chuàng)建client服務(wù)

    1. 創(chuàng)建seat_order庫(kù),用來(lái)存儲(chǔ)訂單信息

    2. 創(chuàng)建seat_storage庫(kù),用來(lái)存儲(chǔ)庫(kù)存信息

    3. 創(chuàng)建seat_account,用來(lái)存儲(chǔ)賬戶信息

    4. 三個(gè)庫(kù)添加日志回滾表

完整數(shù)據(jù)庫(kù)示意圖

分布式事務(wù)seata1.3.0如何整合nacos

5. 	創(chuàng)建seata-order-service(訂單服務(wù)),seata-storage-service(庫(kù)存服務(wù)),seata-account-service(賬戶服務(wù))

配置內(nèi)容大同小異以order服務(wù)的配置為例,修改bootstrap.yml文件

	server:
	  port: 8180
	spring:
	  application:
		name: seata-order-service
	  cloud:
		nacos:
		  discovery:
			server-addr: 127.0.0.1:8848
			username: "nacos"
			password: "nacos"
		  config:
			server-addr: 127.0.0.1:8848
			username: "nacos"
			password: "nacos"
	mybatis:
	  mapperLocations: classpath:mapper/*.xml

修改application.yml文件

seata:
  enabled: true
  application-id: seata-order-service
  tx-service-group: my_test_tx_group #tx-service-group需要與conf目錄下file.conf文件下名稱一致
  config:
    type: nacos
    nacos:
      namespace:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      username: "nacos"
      password: "nacos"
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace:
      username: "nacos"
      password: "nacos"
spring:
  datasource:
    #driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 'Root_123456'
    url: jdbc:mysql://192.168.240.129:3306/seat_order?characterEncoding=utf-8&useSSL=false

添加主方法

@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 創(chuàng)建訂單
     */
    @GetMapping("/create")
    @GlobalTransactional
    public CommonResult create(Order order) {
        orderService.create(order);
        return new CommonResult("訂單創(chuàng)建成功!", 200);
    }
}
  1. 啟動(dòng)nacos,seata,seata-order-service,seata-storage-service,seata-account-service

2020-09-21 16:17:08.307  INFO 29768 --- [           main] i.s.s.a.GlobalTransactionScanner         : Initializing Global Transaction Clients ... 
2020-09-21 16:17:08.394  INFO 29768 --- [           main] i.s.core.rpc.netty.NettyClientBootstrap  : NettyClientBootstrap has started
2020-09-21 16:17:08.394  INFO 29768 --- [           main] i.s.s.a.GlobalTransactionScanner         : Transaction Manager Client is initialized. applicationId[seata-order-service] txServiceGroup[my_test_tx_group]
2020-09-21 16:17:08.403  INFO 29768 --- [           main] io.seata.rm.datasource.AsyncWorker       : Async Commit Buffer Limit: 10000
2020-09-21 16:17:08.403  INFO 29768 --- [           main] i.s.rm.datasource.xa.ResourceManagerXA   : ResourceManagerXA init ...
2020-09-21 16:17:08.408  INFO 29768 --- [           main] i.s.core.rpc.netty.NettyClientBootstrap  : NettyClientBootstrap has started
2020-09-21 16:17:08.408  INFO 29768 --- [           main] i.s.s.a.GlobalTransactionScanner         : Resource Manager is initialized. applicationId[seata-order-service] txServiceGroup[my_test_tx_group]
2020-09-21 16:17:08.408  INFO 29768 --- [           main] i.s.s.a.GlobalTransactionScanner         : Global Transaction Clients are initialized.

分布式事務(wù)seata1.3.0如何整合nacos 3. 測(cè)試分布式事務(wù)


庫(kù)的初始狀態(tài)
分布式事務(wù)seata1.3.0如何整合nacos
訪問(wèn) http://localhost:8180/order/create?userId=1&productId=1&count=10&money=100

2020-09-21 16:27:22.497  INFO 29768 --- [nio-8180-exec-9] i.seata.tm.api.DefaultGlobalTransaction  : Begin new global transaction [192.168.240.1:8091:51345170083352576]
2020-09-21 16:27:22.497  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->下單開(kāi)始
2020-09-21 16:27:22.508  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減庫(kù)存開(kāi)始
2020-09-21 16:27:22.530  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減庫(kù)存結(jié)束
2020-09-21 16:27:22.530  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減余額開(kāi)始
2020-09-21 16:27:22.543  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減余額結(jié)束
2020-09-21 16:27:22.543  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中修改訂單狀態(tài)開(kāi)始
2020-09-21 16:27:22.550  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中修改訂單狀態(tài)結(jié)束
2020-09-21 16:27:22.550  INFO 29768 --- [nio-8180-exec-9] c.m.cloud.service.impl.OrderServiceImpl  : ------->下單結(jié)束
2020-09-21 16:27:22.552  INFO 29768 --- [nio-8180-exec-9] i.seata.tm.api.DefaultGlobalTransaction  : [192.168.240.1:8091:51345170083352576] commit status: Committed
2020-09-21 16:27:22.560  INFO 29768 --- [h_RMROLE_1_6_16] i.s.c.r.p.c.RmBranchCommitProcessor      : rm client handle branch commit process:xid=192.168.240.1:8091:51345170083352576,branchId=51345170112712704,branchType=AT,resourceId=jdbc:mysql://192.168.240.129:3306/seat_order,applicationData=null
2020-09-21 16:27:22.560  INFO 29768 --- [h_RMROLE_1_6_16] io.seata.rm.AbstractRMHandler            : Branch committing: 192.168.240.1:8091:51345170083352576 51345170112712704 jdbc:mysql://192.168.240.129:3306/seat_order null
2020-09-21 16:27:22.560  INFO 29768 --- [h_RMROLE_1_6_16] io.seata.rm.AbstractRMHandler            : Branch commit result: PhaseTwo_Committed
2020-09-21 16:27:22.622  INFO 29768 --- [h_RMROLE_1_7_16] i.s.c.r.p.c.RmBranchCommitProcessor      : rm client handle branch commit process:xid=192.168.240.1:8091:51345170083352576,branchId=51345170297262080,branchType=AT,resourceId=jdbc:mysql://192.168.240.129:3306/seat_order,applicationData=null
2020-09-21 16:27:22.622  INFO 29768 --- [h_RMROLE_1_7_16] io.seata.rm.AbstractRMHandler            : Branch committing: 192.168.240.1:8091:51345170083352576 51345170297262080 jdbc:mysql://192.168.240.129:3306/seat_order null
2020-09-21 16:27:22.622  INFO 29768 --- [h_RMROLE_1_7_16] io.seata.rm.AbstractRMHandler            : Branch commit result: PhaseTwo_Committed

數(shù)據(jù)庫(kù)狀態(tài)
分布式事務(wù)seata1.3.0如何整合nacos
模擬異常

    /**
     * 扣減賬戶余額
     */
    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("------->account-service中扣減賬戶余額開(kāi)始");
        //模擬超時(shí)異常,全局事務(wù)回滾
        try {
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        accountDao.decrease(userId,money);
        LOGGER.info("------->account-service中扣減賬戶余額結(jié)束");
    }

會(huì)發(fā)現(xiàn)數(shù)據(jù)庫(kù)沒(méi)有變化,并且控制臺(tái)打印日志如下

2020-09-21 16:29:53.921  INFO 29768 --- [nio-8180-exec-3] i.seata.tm.api.DefaultGlobalTransaction  : Begin new global transaction [192.168.240.1:8091:51345805197447168]
2020-09-21 16:29:53.921  INFO 29768 --- [nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl  : ------->下單開(kāi)始
2020-09-21 16:29:53.931  INFO 29768 --- [nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減庫(kù)存開(kāi)始
2020-09-21 16:29:53.957  INFO 29768 --- [nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減庫(kù)存結(jié)束
2020-09-21 16:29:53.957  INFO 29768 --- [nio-8180-exec-3] c.m.cloud.service.impl.OrderServiceImpl  : ------->order-service中扣減余額開(kāi)始
2020-09-21 16:29:56.099  INFO 29768 --- [h_RMROLE_1_8_16] i.s.c.r.p.c.RmBranchRollbackProcessor    : rm handle branch rollback process:xid=192.168.240.1:8091:51345805197447168,branchId=51345805231001600,branchType=AT,resourceId=jdbc:mysql://192.168.240.129:3306/seat_order,applicationData=null
2020-09-21 16:29:56.100  INFO 29768 --- [h_RMROLE_1_8_16] io.seata.rm.AbstractRMHandler            : Branch Rollbacking: 192.168.240.1:8091:51345805197447168 51345805231001600 jdbc:mysql://192.168.240.129:3306/seat_order
2020-09-21 16:29:56.142  INFO 29768 --- [h_RMROLE_1_8_16] i.s.r.d.undo.AbstractUndoLogManager      : xid 192.168.240.1:8091:51345805197447168 branch 51345805231001600, undo_log deleted with GlobalFinished
2020-09-21 16:29:56.143  INFO 29768 --- [h_RMROLE_1_8_16] io.seata.rm.AbstractRMHandler            : Branch Rollbacked result: PhaseTwo_Rollbacked
2020-09-21 16:29:56.147  INFO 29768 --- [nio-8180-exec-3] i.seata.tm.api.DefaultGlobalTransaction  : [192.168.240.1:8091:51345805197447168] rollback status: Rollbacked
2020-09-21 16:29:56.167 ERROR 29768 --- [nio-8180-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.RetryableException: Read timed out executing GET http://seata-account-service/account/decrease?userId=1&money=100] with root cause

java.net.SocketTimeoutException: Read timed out
...

感謝各位的閱讀!關(guān)于“分布式事務(wù)seata1.3.0如何整合nacos”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI