2-Alibaba-Seata(数据库准备)学习笔记2020.10.27

2-Alibaba-Seata(数据库与微服务准备)学习笔记2020.10.27

前言: (官网)

根据官网用例: 我们需要创建3个数据库, 分别是: 仓储服务(对应仓储) 、 订单服务(对应订单库) 、 帐户服务(对应订单库)

业务流程是: 订单服务根据采购需求创建订单然后去调用仓储服务去扣减库存, 扣减库存成功在去从用户帐户中扣除余额

要保证要么一起成功然后提交, 要么一起失败大家都进行回滚, 不能出现扣除账户余额失败了, 但库存与订单没回滚或者其他事务范围中间某一步失败了没回滚的情况。

在以往单机的情况下, 操作数据库都是在同一个服务中, 以上的表都是存储在一个库中, 我们通常的做法是在 创建订单流程中开启本地事务, 然后去调用仓储与帐户的Dao, 出现异常情况后, 本地事务将会回滚。

下面开始准备3个数据库与表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W9s7cohJ-1603880513227)(https://s1.ax1x.com/2020/10/27/BMcnC6.jpg)] 建表语句都在官网上有, 分别在3个库上建立对应的表与日记表

1.0 搭建spring-cloud-alibaba环境的3个微服 (官网例子)

千万千万要注意版本问题: 本学习demo使用的版本是:

<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.3.RELEASE</spring.cloud.alibaba.version>

自己搭建启动的时候, 遇到了各种问题, 其中就有openfegin版本问题。

1.1.1 引入依赖

PS 用了官方推荐依赖配置方式,

<dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--openfeign, 只在订单模块引入-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

1.1.2 编写application.yml

server:
  port: 8080
spring:
  application:
    name: seata-order-service
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://119.29.xxx.xxx:3307/seata_order?serverTimezone=UTC
      username: root
      password: root
  cloud:
    nacos:
      discovery:
        server-addr: 119.29.xxx.xxx:8868
    alibaba:
      seata:
	# Seata 事务组名称,对应file.conf文件中 service中vgroup_mapping.xxx="default"
        tx-service-group: order-service-seata-service-group

# 不知道为什么不配这个就报错找不到服务, 按照官网例子没配置这个, 正常来说registry.type=file才需要在这里配置, 但是现在情况是配置文件需要配, 这里也要, 只要其中一个地方没配就报错。
seata:
  service:
    grouplist:
      key: 119.29.xxx.xxx
      value: 8091
      
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mappers/*.xml
  type-aliases-package: com.zhihao.entity
# 开放监控管理路径
management:
  endpoints:
    web:
      exposure:
        include: '*'

1.1.3 编写file.confregistry.conf (定义seata参数)

拷贝 https://github.com/seata/seata-samples/tree/master/spring-cloud-alibaba-samples/sca-provider/src/main/resources 中的file.confregistry.conf进行修改

transport {
  # tcp udt unix-domain-socket, 连接采用TCP协议
  type = "TCP"
  #NIO NATIVE, 采用NIO模式流
  server = "NIO"
  #enable heartbeat, 启用心跳
  heartbeat = true
  # the client batch send request enable, 客户端批量发送请求启用
  enableClientBatchSendRequest = true
  #thread factory for netty, 网络线程工厂
  threadFactory {
    bossThreadPrefix = "NettyBoss"
    workerThreadPrefix = "NettyServerNIOWorker"
    serverExecutorThread-prefix = "NettyServerBizHandler"
    shareBossWorker = false
    clientSelectorThreadPrefix = "NettyClientSelector"
    clientSelectorThreadSize = 1
    clientWorkerThreadPrefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    bossThreadSize = 1
    #auto default pin or 8
    workerThreadSize = "default"
  }
  shutdown {
    # when destroy server, wait seconds, 销毁服务器时,请等待几秒钟
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}
service {
  #transaction service group mapping
  vgroupMapping.order-service-seata-service-group = "default"
  #only support when registry.type=file, please don't set multiple addresses, 仅在registry.type = file时才支持,请不要设置多个地址
  default.grouplist = "119.29.xxx.xxx:8091"
  #degrade, current not support, 降级,当前不支持
  enableDegrade = false
  #disable seata
  disableGlobalTransaction = false
}

client {
  rm {
    asyncCommitBufferLimit = 10000  # 异步提交缓存队列长度(默认10000)
    lock {
      retryInterval = 10 # 校验或占用全局锁重试间隔(默认10ms)
      retryTimes = 30 # 校验或占用全局锁重试次数(默认30)
      retryPolicyBranchRollbackOnConflict = true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
    }
    reportRetryCount = 5 # 一阶段结果上报TC重试次数(默认5)
    tableMetaCheckEnable = false # 自动刷新缓存中的表结构(默认false)
    reportSuccessEnable = true #报告成功启用
  }
  tm {
    commitRetryCount = 5    # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    rollbackRetryCount = 5  # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1}
  undo {
    dataValidation = true   # 二阶段回滚镜像校验(默认true开启)
    logSerialization = "jackson"    # undo序列化方式(默认jackson)
    logTable = "undo_log"   # 自定义undo表名(默认undo_log)
  }
  log {
    exceptionRate = 100     # 日志异常输出概率(默认100}
}

//---------------------------------------分隔符, 下面是registry.conf-----------------------
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "119.29.xxx.xx:8868"
    namespace = ""
    username = ""
    password = ""
  }
   .............下面没改动省略号
}

config {
  # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig
  type = "file"
  .............下面没改动省略号
}

1.1.4 编写启动类(引导类)

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //仅在订单模块加上这个注解
//依赖 seata-spring-boot-starter 时,自动代理数据源,无需额外处理。
//依赖 seata-all 时,使用 @EnableAutoDataSourceProxy (since 1.1.0) 注解或者手动配置
@EnableAutoDataSourceProxy //1.1版本后支持注解自动代理数据源, 不支持XA数据源自动代理
public class SeataOrderService
{
    public static void main( String[] args )
    {
        SpringApplication.run(SeataOrderService.class,args);
    }
}

1.1.5 编写具体创建订单业务流程

dao-services-controller 、 fegin … 省略

1.1.6 另外2个模块accountstorage参考上面的自行搭建

最终的项目结构如下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DLqllXPj-1603880513230)(https://s1.ax1x.com/2020/10/27/BQwip4.jpg)]

1.17 全部搭建完成后, 启动项目

启动3个应用, 看看是否正常启动。 启动之前要先启动好nacosSeata服务器。

1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懵懵懂懂程序员

如果节省了你的时间, 请鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值