spring cloud alibaba 分布式事务解决方案之seata-1.3.0

1.github上下载seata-1.3.0.zip包

链接:https://github.com/seata/seata/releases/download/v1.3.0/seata-server-1.3.0.zip

2.打开seata压缩包,找到conf文件夹下的registry.conf修改注册方式和配置方式为nacos,如下:

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

  nacos {
    application = "tianxia-seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
}

3.GitHub下载seata源码,链接:https://github.com/seata/seata/tree/v1.3.0

解压后找到script文件夹下的config-center,修改该文件夹下的config.txt文件。
config.txt文件内容都是键值对,后面要通过脚本把该文件中的配置导入到nacos中。修改的键值对项如下:

#用来配置分布式事务集群名称
service.vgroupMapping.tianxia_article_tx_group=default
service.vgroupMapping.tianxia_member_tx_group=default
#存储方式改成数据库
store.mode=db
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=username
store.db.password=password

修改后执行nacos文件夹下的nacos-config.sh 脚本导入配置到nacos
导入成功后如图所示:


image.png

返回到script文件夹,进入到client目录下的spring文件夹内,复制application.yml文件内的配置到自己的项目内,修改如下配置:

seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: ${spring.application.name}_tx_group
  service:
 #   vgroup-mapping:
 #     my_test_tx_group: default
  config:
    type: nacos
    nacos:
      namespace:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      username: "nacos"
      password: "nacos"
  registry:
    type: nacos
    nacos:
      application: tianxia-seata-server
      server-addr: 127.0.0.1:8848
      group : "SEATA_GROUP"
      namespace:
      username: "nacos"
      password: "nacos"

到此可以启动seata服务端了。
启动成功可以再nacos看到注册的seata服务,如下:


image.png

4.上一步项目中已经加入了seata的application.yml配置,还没有加入seata依赖:

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-seata
            
                
                    io.seata
                    seata-spring-boot-starter
                
            
        
        
            io.seata
            seata-spring-boot-starter
            ${seata.version}
        

5.编写业务代码

在方法的入口处加上注解@GlobalTransactional

    @GlobalTransactional
    @Override
    public boolean saveArticle() {
        GcArticle build = GcArticle.builder().content("asdadad").title("asdadsdf").viewCount(1).build();
        boolean save1 = this.save(build);
        String save = iRemoteUserService.save();
        if(true){
            throw new BusinessException("asda");
        }
        return true;
    }

被调用的远程方法加上spring事务注解 @Transactional
事务id可以通过RootContext.getXID();获取到

    @Transactional
    @Override
    public boolean saveMember() {
        String xid = RootContext.getXID();
        log.info("xid:{}",xid);
        GcMember member = GcMember.builder().age(22).nickname("hhy").phone("15857193035").build();
        boolean save = this.save(member);
        return save;
    }

6.到此可以启动项目进行测试了

祝大家一遍过~~

你可能感兴趣的:(spring cloud alibaba 分布式事务解决方案之seata-1.3.0)