-
Notifications
You must be signed in to change notification settings - Fork 34
Home
lorne edited this page Apr 2, 2018
·
5 revisions
demo项目下分为jdbc/mybatis/hibernate 三种版本的demo,可根据自己的项目需求来对应查看相关的demo
其中jdbc版本的demo涉及到了5个模块的调用,他们的调用关系图如下:
mybatis/hibernate 版本的demo只有两个模块,他们的调用关系图如下:
dubbo-lcn-demo 需要依赖的服务有:
- TxManager
- Mysql
- Redis
- Eureka
- Zookeeper
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;
- 添加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>
- 添加tx.properties配置
#txmanager地址
url=http://127.0.0.1:8899/tx/manager/
- 配置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" />
- 添加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;
当把这句代码注释掉以后再执行时,此时观察数据你会发现没有任何数据在数据库下将会看到有两条数据。