springcloud 集成lcn 5.0.2分布式事务

项目地址 :https://github.com/heng1234/springcl-lcn-demo

 

官网文档地址:https://www.txlcn.org/zh-cn/docs/start.html

  1. 创建数据表

CREATE TABLE `t_tx_exception`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `transaction_state` tinyint(4) NULL DEFAULT NULL,
  `registrar` tinyint(4) NULL DEFAULT NULL,
  `remark` varchar(4096) NULL DEFAULT  NULL,
  `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 未解决 1已解决',
  `create_time` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

idea新建一个maven lcn项目

在刚刚建立好的项目右键new module建立springboot项目 tx-manager

 

pom.xml加入


        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tm</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

启动类

加上注解

@EnableTransactionManagerServer

application.properties

#应用名称
spring.application.name=tx-manager
server.port=7970
#DataSource配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
#redis 配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

#注册中心地址
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
# 注册名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
# 设置注册到服务的为ip
eureka.instance.prefer-ip-address=true
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。  default:false
mybatis.configuration.use-generated-keys=true

# tx-manager ip(client请求ip)
#tx-lcn.manager.host=127.0.0.1
# client 请求端口
#tx-lcn.manager.port=8070

tx-lcn.client.manager-address=127.0.0.1:8070
# 心跳检测时间 单位:ms
tx-lcn.manager.heart-time=12000
# 事务执行总时间
tx-lcn.manager.dtx-time=10000
# 参数延迟删除时间单位ms
tx-lcn.message.netty.attr-delay-time=10000
tx-lcn.manager.concurrent-level=128
# 开启日志
tx-lcn.logger.enabled=true
logging.level.com.codingapi=debug
tx-lcn.logger.driver-class-name=${spring.datasource.driver-class-name}
tx-lcn.logger.jdbc-url=${spring.datasource.url}
tx-lcn.logger.username=${spring.datasource.username}
tx-lcn.logger.password=${spring.datasource.password}
tx-lcn.manager.ex-url=/provider/email-to/xiaoluoheng@foxmail.com
tx-lcn.manager.ex-url-enabled=true
spring.jpa.hibernate.ddl-auto=update
tx-lcn.manager.admin-key=123456

新建2个client    client_one client_two  

pom.xml加入jar

 <!--lcn事务-->
      <dependency>
          <groupId>com.codingapi.txlcn</groupId>
          <artifactId>txlcn-tc</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>com.codingapi.txlcn</groupId>
          <artifactId>txlcn-txmsg-netty</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>

启动类加上注解

@EnableDistributedTransaction

然后再添加2个增加方法

client_one

  /**
     * 新增
     */
    @Transactional
    @LcnTransaction
    public Integer saveUser(User user){
        //user.setId(9L);
        user.setEmail("lcn.@qq.com");
        user.setPassword("lcn123456");
        return userLcnMapper.insert(user);
    }

client_two 

 @Transactional
    @LcnTransaction
    public Integer saveUser(){
        User user = new User();
        user.setEmail("lcnxxx@xx.com");
        user.setAge(17);
        user.setName("lcn");
        int res = 1/0;
      return   userMapper.insert(user);
    }

feign

pom里面也加入jar

 <!--lcn事务-->
      <dependency>
          <groupId>com.codingapi.txlcn</groupId>
          <artifactId>txlcn-tc</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>com.codingapi.txlcn</groupId>
          <artifactId>txlcn-txmsg-netty</artifactId>
          <version>5.0.2.RELEASE</version>
      </dependency>

启动类加上注解

 

 

@EnableDistributedTransaction

 

 代码

   @Autowired
    private Client2 client2;
    @Autowired
    private Client3 client3;

    @RequestMapping("testLcn")
    @LcnTransaction
    public  String testLcn(){
        Map<String, Object> map  = new HashMap<>();
        Integer res = (Integer) client2.getMethod("tuser/saveUser",map);
        Integer res1 = (Integer) client3.getMethod("userTwo/insertUser",map);
       return res+"-"+res1;

    }

 访问controller地址http://localhost:8765/feign/testLcn

 浏览器出现 

 

 表示回滚成功

 tx-manager访问地址

http://localhost:7970/admin/index.html#/builder

具体看项目地址 :https://github.com/heng1234/springcl-lcn-demo 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值