Skip to content
lorne edited this page Apr 2, 2018 · 5 revisions

dubbo LCN分布式事务示例v4.0 示例demo

项目介绍

demo项目下分为jdbc/mybatis/hibernate 三种版本的demo,可根据自己的项目需求来对应查看相关的demo

其中jdbc版本的demo涉及到了5个模块的调用,他们的调用关系图如下:

mybatis/hibernate 版本的demo只有两个模块,他们的调用关系图如下:

依赖的服务

dubbo-lcn-demo 需要依赖的服务有:

  1. TxManager
  2. Mysql
  3. Redis
  4. Eureka
  5. Zookeeper

TxManager的启动与配置见:

TxManager启动说明

  • TxManager启动说明中包含Eureka的配置环境

数据库配置与设置

#数据库链接配置
db.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
db.username = root
db.password = root

数据库为test,账号密码为root/root,使用者可根据自己的环境调整。

数据库的初始化脚本

USE test;

DROP TABLE IF EXISTS `t_test`;

CREATE TABLE `t_test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


dubbo-lcn 配置教程

  1. 添加maven依赖。请及时关注maven中心库的最新版本,尽量使用最新版本。
     <dependency>
        <groupId>com.codingapi</groupId>
        <artifactId>transaction-dubbo</artifactId>
        <version>${lcn.last.version}</version>
    </dependency>

    <dependency>
        <groupId>com.codingapi</groupId>
        <artifactId>tx-plugins-db</artifactId>
        <version>${lcn.last.version}</version>
    </dependency>

  1. 添加tx.properties配置

#txmanager地址
url=http://127.0.0.1:8899/tx/manager/

  1. 配置dubbo
    
    <dubbo:application name="mybatis_demo1"   />

    <!--所有参与分布式事务的模块以及TxManager都必须要在同一个服务下-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" port="20882" />

    <!--只要存在远程调用其他模块都需添加Filter配置-->
    <dubbo:consumer  filter="transactionFilter" />
 
    <dubbo:reference interface="com.demo.service.Test2Service" id="test2Service" />

    <dubbo:protocol accesslog="true" name="dubbo"  host="127.0.0.1" />

  1. 添加com.codingapi.tx包路径的bean扫描
    <context:component-scan base-package="com.codingapi.tx.*"/>

启动说明

所有的demo启动以后的访问地址都是. 该项目下的demo1 都是web项目需要基于Tomcat等web容器启动,其他的demo均在main包run的方式启动。

http://localhost:port/projectName/save

请求以后会出现 / by zero异常。这是由于在demo1的最后一句代码上写有int v = 100/0;

当把这句代码注释掉以后再执行时,此时观察数据你会发现没有任何数据在数据库下将会看到有两条数据。

Clone this wiki locally