分布式事务第3篇--seata本地源码环境搭建

183 阅读16分钟

一,简介

为便于对源码进行调试分析,本篇将以seata官方案例AT模式为基础,逐步展示如何搭建seata本地源码环境。

本地环境版本信息:

SpringBoot2.2.2
Dubbo 2.7.5
Mybatis 3.4.2
Nacos 2.1.0
Seata 1.0.0
MySQL 8.0.29 

二,环境配置与服务启动

本地环境主要涉及:注册中心Nacos,Seata-Server,Buiness-Server

1,Nacos本地源码启动

官网下载源码:

github.com/alibaba/nac…

将下载的Nacos源码作为maven工程用IDEA打开,并进行编译,如图所示图片

编译完成之后,修改配置文件中的MySQL信息为本地信息:

#文件路径\console\src\main\resources\application.properties
server.servlet.contextPath=/nacos
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456
nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.max-days=30
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
server.tomcat.basedir=file:.
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-ui/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=false
nacos.core.auth.caching.enabled=true
nacos.core.auth.enable.userAgentAuthWhite=false
nacos.core.auth.server.identity.key=serverIdentity
nacos.core.auth.server.identity.value=security
nacos.core.auth.plugin.nacos.token.expire.seconds=18000
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.istio.mcp.server.enabled=false

创建MySQL表:

CREATE SCHEMA nacos AUTHORIZATION nacos;
CREATE TABLE config_info (  id bigint NOT NULL generated by default as identity,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  tenant_id varchar(128) default '',  app_name varchar(128),  content CLOB,  md5 varchar(32) DEFAULT NULL,  gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  src_user varchar(128) DEFAULT NULL,  src_ip varchar(20) DEFAULT NULL,  c_desc varchar(256) DEFAULT NULL,  c_use varchar(64) DEFAULT NULL,  effect varchar(64) DEFAULT NULL,  type varchar(64) DEFAULT NULL,  c_schema LONG VARCHAR DEFAULT NULL,  encrypted_data_key LONG VARCHAR DEFAULT NULL,  constraint configinfo_id_key PRIMARY KEY (id),  constraint uk_configinfo_datagrouptenant UNIQUE (data_id,group_id,tenant_id));
CREATE INDEX configinfo_dataid_key_idx ON config_info(data_id);CREATE INDEX configinfo_groupid_key_idx ON config_info(group_id);CREATE INDEX configinfo_dataid_group_key_idx ON config_info(data_id, group_id);
CREATE TABLE his_config_info (  id bigint NOT NULL,  nid bigint NOT NULL generated by default as identity,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  tenant_id varchar(128) default '',  app_name varchar(128),  content CLOB,  md5 varchar(32) DEFAULT NULL,  gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00.000',  gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00.000',  src_user varchar(128),  src_ip varchar(20) DEFAULT NULL,  op_type char(10) DEFAULT NULL,  encrypted_data_key LONG VARCHAR DEFAULT NULL,  constraint hisconfiginfo_nid_key PRIMARY KEY (nid));
CREATE INDEX hisconfiginfo_dataid_key_idx ON his_config_info(data_id);CREATE INDEX hisconfiginfo_gmt_create_idx ON his_config_info(gmt_create);CREATE INDEX hisconfiginfo_gmt_modified_idx ON his_config_info(gmt_modified);

CREATE TABLE config_info_beta (  id bigint NOT NULL generated by default as identity,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  tenant_id varchar(128) default '',  app_name varchar(128),  content CLOB,  beta_ips varchar(1024),  md5 varchar(32) DEFAULT NULL,  gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  src_user varchar(128),  src_ip varchar(20) DEFAULT NULL,  encrypted_data_key LONG VARCHAR DEFAULT NULL,  constraint configinfobeta_id_key PRIMARY KEY (id),  constraint uk_configinfobeta_datagrouptenant UNIQUE (data_id,group_id,tenant_id));
CREATE TABLE config_info_tag (  id bigint NOT NULL generated by default as identity,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  tenant_id varchar(128) default '',  tag_id varchar(128) NOT NULL,  app_name varchar(128),  content CLOB,  md5 varchar(32) DEFAULT NULL,  gmt_create timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  src_user varchar(128),  src_ip varchar(20) DEFAULT NULL,  constraint configinfotag_id_key PRIMARY KEY (id),  constraint uk_configinfotag_datagrouptenanttag UNIQUE (data_id,group_id,tenant_id,tag_id));
CREATE TABLE config_info_aggr (  id bigint NOT NULL generated by default as identity,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  tenant_id varchar(128) default '',  datum_id varchar(255) NOT NULL,  app_name varchar(128),  content CLOB,  gmt_modified timestamp NOT NULL DEFAULT '2010-05-05 00:00:00',  constraint configinfoaggr_id_key PRIMARY KEY (id),  constraint uk_configinfoaggr_datagrouptenantdatum UNIQUE (data_id,group_id,tenant_id,datum_id));
CREATE TABLE app_list ( id bigint NOT NULL generated by default as identity, app_name varchar(128) NOT NULL, is_dynamic_collect_disabled smallint DEFAULT 0, last_sub_info_collected_time timestamp DEFAULT '1970-01-01 08:00:00.0', sub_info_lock_owner varchar(128), sub_info_lock_time timestamp DEFAULT '1970-01-01 08:00:00.0', constraint applist_id_key PRIMARY KEY (id), constraint uk_appname UNIQUE (app_name));
CREATE TABLE app_configdata_relation_subs (  id bigint NOT NULL generated by default as identity,  app_name varchar(128) NOT NULL,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',  constraint configdatarelationsubs_id_key PRIMARY KEY (id),  constraint uk_app_sub_config_datagroup UNIQUE (app_name, data_id, group_id));

CREATE TABLE app_configdata_relation_pubs (  id bigint NOT NULL generated by default as identity,  app_name varchar(128) NOT NULL,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',  constraint configdatarelationpubs_id_key PRIMARY KEY (id),  constraint uk_app_pub_config_datagroup UNIQUE (app_name, data_id, group_id));
CREATE TABLE config_tags_relation (  id bigint NOT NULL,  tag_name varchar(128) NOT NULL,  tag_type varchar(64) DEFAULT NULL,  data_id varchar(255) NOT NULL,  group_id varchar(128) NOT NULL,  tenant_id varchar(128) DEFAULT '',  nid bigint NOT NULL generated by default as identity,  constraint config_tags_id_key PRIMARY KEY (nid),  constraint uk_configtagrelation_configidtag UNIQUE (id, tag_name, tag_type));
CREATE INDEX config_tags_tenant_id_idx ON config_tags_relation(tenant_id);
CREATE TABLE group_capacity (  id bigint NOT NULL generated by default as identity,  group_id varchar(128) DEFAULT '',  quota int DEFAULT 0,  usage int DEFAULT 0,  max_size int DEFAULT 0,  max_aggr_count int DEFAULT 0,  max_aggr_size int DEFAULT 0,  max_history_count int DEFAULT 0,  gmt_create timestamp DEFAULT '2010-05-05 00:00:00',  gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',  constraint group_capacity_id_key PRIMARY KEY (id),  constraint uk_group_id UNIQUE (group_id));
CREATE TABLE tenant_capacity (  id bigint NOT NULL generated by default as identity,  tenant_id varchar(128) DEFAULT '',  quota int DEFAULT 0,  usage int DEFAULT 0,  max_size int DEFAULT 0,  max_aggr_count int DEFAULT 0,  max_aggr_size int DEFAULT 0,  max_history_count int DEFAULT 0,  gmt_create timestamp DEFAULT '2010-05-05 00:00:00',  gmt_modified timestamp DEFAULT '2010-05-05 00:00:00',  constraint tenant_capacity_id_key PRIMARY KEY (id),  constraint uk_tenant_id UNIQUE (tenant_id));
CREATE TABLE tenant_info (  id bigint NOT NULL generated by default as identity,  kp varchar(128) NOT NULL,  tenant_id varchar(128)  DEFAULT '',  tenant_name varchar(128)  DEFAULT '',  tenant_desc varchar(256)  DEFAULT NULL,  create_source varchar(32) DEFAULT NULL,  gmt_create bigint NOT NULL,  gmt_modified bigint NOT NULL,  constraint tenant_info_id_key PRIMARY KEY (id),  constraint uk_tenant_info_kptenantid UNIQUE (kp,tenant_id));CREATE INDEX tenant_info_tenant_id_idx ON tenant_info(tenant_id);
CREATE TABLE users (  username varchar(50) NOT NULL PRIMARY KEY,  password varchar(500) NOT NULL,  enabled boolean NOT NULL DEFAULT true);
CREATE TABLE roles (  username varchar(50) NOT NULL,  role varchar(50) NOT NULL,  constraint uk_username_role UNIQUE (username,role));
CREATE TABLE permissions (    role varchar(50) NOT NULL,    resource varchar(512) NOT NULL,    action varchar(8) NOT NULL,    constraint uk_role_permission UNIQUE (role,resource,action));
INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');

找到Nacos启动类,启动Nacos:

路径:console/src/main/java/com/alibaba/nacos/Nacos.java

图片

启动成功之后,访问如下地址:

 http://192.168.138.1:8848/nacos/index.html

2,Seata Server本地源码启动

官网下载源码:github.com/apache/incu…

导入本地IDEA,编译之后修改相关配置:

配置文件路径:server/src/main/resources/application.yml

server:
  port: 7091

spring:
  application:
    name: seata-server
service:
  vgroup_mapping:
    my_test_tx_group: default
logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
  extend:
    logstash-appender:
      destination: 127.0.0.1:4560
    kafka-appender:
      bootstrap-servers: 127.0.0.1:9092
      topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata

seata:
  config:
    # 修改配置中心为Nacos
    type: nacos
    nacos:
      #本地Nacos地址
      server-addr: 192.168.138.1:8848
      group: SEATA_GROUP
      namespace:
      username:
      password:
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
  registry:
    # 修改注册中心为Nacos
    type: nacos
    nacos:
      application: seata-server
      server-addr: 192.168.138.1:8848
      group: SEATA_GROUP
      namespace:
      cluster: default
      username:
      password:
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
  store:
    # 存储模式为DB
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.80.141:3306/seata?rewriteBatchedStatements=true
      user: root
      password: 123456
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

按照源码中的数据库脚本添加数据库表相关信息:

全局事务会话信息由3块内容构成,全局事务-->分支事务-->全局锁,对应表global_table, branch_table, lock_table
配置文件路径:script\server\db\mysql.sql

Seata-Server启动类:

server\src\main\java\io\seata\server\ServerApplication.java

图片

启动成功之后,在Nacos注册中心可以看到Seata-Server已经注册:

图片

3,业务系统启动

整个业务逻辑由 3 个微服务提供支持:仓储服务:对给定的商品扣除仓储数量。订单服务:根据采购需求创建订单。帐户服务:从用户帐户中扣除余额。

官方业务案例架构图:

图片

Seata官方案例下载:github.com/apache/incu…

创建数据库表:

SEATA AT 模式需要 UNDO_LOG 表:

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';

创建业务数据表:

CREATE TABLE `t_account` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `amount` double(14,2) DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- seata_pay.t_order definition
CREATE TABLE `t_order` (
  `id` int NOT NULL AUTO_INCREMENT,
  `order_no` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `user_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `commodity_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `count` int DEFAULT '0',
  `amount` double(14,2) DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- seata_pay.t_stock definition
CREATE TABLE `t_stock` (
  `id` int NOT NULL AUTO_INCREMENT,
  `commodity_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `count` int DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `commodity_code` (`commodity_code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

account-server配置:


server:
  port: 8102
#====================================stream =============================================
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.80.141:3306/seata?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: 123456
  application:
    name: dubbo-account-example

#====================================Dubbo config===============================================
dubbo:
  application:
    id: dubbo-account-example
    name: dubbo-account-example
    qosEnable: false
  protocol:
    id: dubbo
    name: dubbo
    port: 20883
  registry:
    id: dubbo-account-example-registry
    address: nacos://127.0.0.1:8848
  config-center:
    address: nacos://127.0.0.1:8848
  metadata-report:
    address: nacos://127.0.0.1:8848
#====================================mybatis-plus config===============================================
mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml
  typeAliasesPackage: io.seata.samples.integration.*.entity
  configuration:
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      id-type: auto

#====================================Seata Config===============================================

seata:
  enabled: true
  application-id: account-seata-example
  tx-service-group: my_test_tx_group # 事务群组(可以每个应用独立取名,也可以使用相同的名字)
  client:
    rm-report-success-enable: true
    rm-table-meta-check-enable: false # 自动刷新缓存中的表结构(默认false)
    rm-report-retry-count: 5 # 一阶段结果上报TC重试次数(默认5)
    rm-async-commit-buffer-limit: 10000 # 异步提交缓存队列长度(默认10000)
    rm:
      lock:
        lock-retry-internal: 10 # 校验或占用全局锁重试间隔(默认10ms)
        lock-retry-times: 30 # 校验或占用全局锁重试次数(默认30)
        lock-retry-policy-branch-rollback-on-conflict: true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
    tm-commit-retry-count: 3 # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    tm-rollback-retry-count: 3 # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
    undo:
      undo-data-validation: true # 二阶段回滚镜像校验(默认true开启)
      undo-log-serialization: jackson # undo序列化方式(默认jackson)
      undo-log-table: undo_log  # 自定义undo表名(默认undo_log)
    log:
      exceptionRate: 100 # 日志异常输出概率(默认100)
    support:
      spring:
        datasource-autoproxy: true
  service:
    vgroup-mapping:
      my_test_tx_group: default # TC 集群(必须与seata-server保持一致)
    enable-degrade: false # 降级开关
    disable-global-transaction: false # 禁用全局事务(默认false)
    grouplist:
      default: 127.0.0.1:8091
  transport:
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true # 客户端事务消息请求是否批量合并发送(默认true)
  registry:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      server-addr: localhost:8848
      namespace:
      cluster: default
      group: SEATA_GROUP
      username: nacos
      password: nacos
  config:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      namespace:
      server-addr: localhost:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      dataId: nacos-server.properties

business-server配置:


server:
  port: 8104

spring:
  application:
    name: dubbo-business-example
#====================================Dubbo config===============================================
dubbo:
  application:
    id: dubbo-business-example
    name: dubbo-business-example
    qosEnable: false
  protocol:
    id: dubbo
    name: dubbo
    port: 10001
  registry:
    id: dubbo-business-example-registry
    address: nacos://127.0.0.1:8848
  config-center:
    address: nacos://127.0.0.1:8848
  metadata-report:
    address: nacos://127.0.0.1:8848
#====================================Seata Config===============================================
seata:
  enabled: true
  application-id: business-seata-example
  tx-service-group: my_test_tx_group # 事务群组(可以每个应用独立取名,也可以使用相同的名字)
  client:
    rm-report-success-enable: true
    rm-table-meta-check-enable: false # 自动刷新缓存中的表结构(默认false)
    rm-report-retry-count: 5 # 一阶段结果上报TC重试次数(默认5)
    rm-async-commit-buffer-limit: 10000 # 异步提交缓存队列长度(默认10000)
    rm:
      lock:
        lock-retry-internal: 10 # 校验或占用全局锁重试间隔(默认10ms)
        lock-retry-times: 30 # 校验或占用全局锁重试次数(默认30)
        lock-retry-policy-branch-rollback-on-conflict: true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
    tm-commit-retry-count: 3 # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    tm-rollback-retry-count: 3 # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
    undo:
      undo-data-validation: true # 二阶段回滚镜像校验(默认true开启)
      undo-log-serialization: jackson # undo序列化方式(默认jackson)
      undo-log-table: undo_log  # 自定义undo表名(默认undo_log)
    log:
      exceptionRate: 100 # 日志异常输出概率(默认100)
    support:
      spring:
        datasource-autoproxy: true
  service:
    vgroup-mapping:
      my_test_tx_group: default # TC 集群(必须与seata-server保持一致)
    enable-degrade: false # 降级开关
    disable-global-transaction: false # 禁用全局事务(默认false)
    grouplist:
      default: 127.0.0.1:8091
  transport:
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true # 客户端事务消息请求是否批量合并发送(默认true)
  registry:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      server-addr: localhost:8848
      namespace:
      cluster: default
      group: SEATA_GROUP
      username: nacos
      password: nacos
  config:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      namespace:
      server-addr: localhost:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      dataId: nacos-server.properties

order-server配置:

server:
  port: 8101
#====================================stream =============================================
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.80.141:3306/seata?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: 123456
  application:
    name: dubbo-order-example

#====================================Dubbo config===============================================
dubbo:
  application:
    id: dubbo-order-example
    name: dubbo-order-example
    qosEnable: false
  protocol:
    id: dubbo
    name: dubbo
    port: 20880
  registry:
    id: dubbo-order-example-registry
    address: nacos://127.0.0.1:8848
  config-center:
    address: nacos://127.0.0.1:8848
  metadata-report:
    address: nacos://127.0.0.1:8848
#====================================mybatis-plus config===============================================
mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml
  typeAliasesPackage: io.seata.samples.integration.*.entity
  configuration:
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      id-type: auto

#====================================Seata Config===============================================
seata:
  enabled: true
  application-id: order-seata-example
  tx-service-group: my_test_tx_group # 事务群组(可以每个应用独立取名,也可以使用相同的名字)
  client:
    rm-report-success-enable: true
    rm-table-meta-check-enable: false # 自动刷新缓存中的表结构(默认false)
    rm-report-retry-count: 5 # 一阶段结果上报TC重试次数(默认5)
    rm-async-commit-buffer-limit: 10000 # 异步提交缓存队列长度(默认10000)
    rm:
      lock:
        lock-retry-internal: 10 # 校验或占用全局锁重试间隔(默认10ms)
        lock-retry-times: 30 # 校验或占用全局锁重试次数(默认30)
        lock-retry-policy-branch-rollback-on-conflict: true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
    tm-commit-retry-count: 3 # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    tm-rollback-retry-count: 3 # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
    undo:
      undo-data-validation: true # 二阶段回滚镜像校验(默认true开启)
      undo-log-serialization: jackson # undo序列化方式(默认jackson)
      undo-log-table: undo_log  # 自定义undo表名(默认undo_log)
    log:
      exceptionRate: 100 # 日志异常输出概率(默认100)
    support:
      spring:
        datasource-autoproxy: true
  service:
    vgroup-mapping:
      my_test_tx_group: default # TC 集群(必须与seata-server保持一致)
    enable-degrade: false # 降级开关
    disable-global-transaction: false # 禁用全局事务(默认false)
    grouplist:
      default: 127.0.0.1:8091
  transport:
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true # 客户端事务消息请求是否批量合并发送(默认true)
  registry:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      server-addr: localhost:8848
      namespace:
      cluster: default
      group: SEATA_GROUP
      username: nacos
      password: nacos
  config:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      namespace:
      server-addr: localhost:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      dataId: nacos-server.properties

stock-server配置:


server:
  port: 8109
#====================================stream =============================================
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.80.141:3306/seata?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: 123456
  application:
    name: dubbo-stock-example

#====================================Dubbo config===============================================
dubbo:
  application:
    id: dubbo-stock-example
    name: dubbo-stock-example
    qosEnable: false
  protocol:
    id: dubbo
    name: dubbo
    port: 20888
  registry:
    id: dubbo-stock-example-registry
    address: nacos://127.0.0.1:8848
  config-center:
    address: nacos://127.0.0.1:8848
  metadata-report:
    address: nacos://127.0.0.1:8848
#====================================mybatis-plus config===============================================
mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml
  typeAliasesPackage: io.seata.samples.integration.*.entity
  configuration:
    map-underscore-to-camel-case: true
  global-config:
    db-config:
      id-type: auto

#====================================Seata Config===============================================
seata:
  enabled: true
  application-id: stock-seata-example
  tx-service-group: my_test_tx_group # 事务群组(可以每个应用独立取名,也可以使用相同的名字)
  client:
    rm-report-success-enable: true
    rm-table-meta-check-enable: false # 自动刷新缓存中的表结构(默认false)
    rm-report-retry-count: 5 # 一阶段结果上报TC重试次数(默认5)
    rm-async-commit-buffer-limit: 10000 # 异步提交缓存队列长度(默认10000)
    rm:
      lock:
        lock-retry-internal: 10 # 校验或占用全局锁重试间隔(默认10ms)
        lock-retry-times: 30 # 校验或占用全局锁重试次数(默认30)
        lock-retry-policy-branch-rollback-on-conflict: true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
    tm-commit-retry-count: 3 # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    tm-rollback-retry-count: 3 # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
    undo:
      undo-data-validation: true # 二阶段回滚镜像校验(默认true开启)
      undo-log-serialization: jackson # undo序列化方式(默认jackson)
      undo-log-table: undo_log  # 自定义undo表名(默认undo_log)
    log:
      exceptionRate: 100 # 日志异常输出概率(默认100)
    support:
      spring:
        datasource-autoproxy: true
  service:
    vgroup-mapping:
      my_test_tx_group: default # TC 集群(必须与seata-server保持一致)
    enable-degrade: false # 降级开关
    disable-global-transaction: false # 禁用全局事务(默认false)
    grouplist:
      default: 127.0.0.1:8091
  transport:
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true # 客户端事务消息请求是否批量合并发送(默认true)
  registry:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      server-addr: localhost:8848
      namespace:
      cluster: default
      group: SEATA_GROUP
      username: nacos
      password: nacos
  config:
#    file:
#      name: file.conf
    type: nacos
    nacos:
      namespace:
      server-addr: localhost:8848
      group: SEATA_GROUP
      username: nacos
      password: nacos
      dataId: nacos-server.properties

配置完成之后依次启动各个服务。

三,结果查看

上述步骤完成之后,就可以在Nacos注册中心,看到各个服务已经完成启动并注册到了注册中心。后续我将以该环境为基础,深入分析Seata的运行机制与代码实现,敬请期待。

图片

欢迎关注微信公众号,获取小王第一手技术文章:小王聊技术

扫码_搜索联合传播样式-标准色版.png