分布式_事务_01_2PC框架raincat快速体验
一、前言
关于2PC的理论知识请见:分布式_理论_03_2PC
这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验。
二、源码
源码请见:
相关视频
http://www.iqiyi.com/u/1243078745/v
三、接入步骤
1.启动 TxManagerApplication
此工程为分布式事务的协调者
配置txManaager, 修改application.properties中你自己的redis配置
启动TxManagerApplication
2.引入依赖
在需要进行分布式事务处理的服务的pom.xml中引入如下依赖:
<dependency> <groupId>com.raincat</groupId> <artifactId>raincat-springcloud</artifactId> <version>${your.version}</version> </dependency>
3.配置文件
(1)新建applicationContext.xml,增加如下配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <context:component-scan base-package="com.raincat.*"/> <aop:aspectj-autoproxy expose-proxy="true"/> <bean id="txTransactionBootstrap" class="com.raincat.core.bootstrap.TxTransactionBootstrap"> <property name="txManagerUrl" value="http://localhost:8761"/> <property name="serializer" value="kryo"/> <property name="nettySerializer" value="kryo"/> <property name="compensationCacheType" value="db"/> <property name="compensation" value="true"/> <property name="txDbConfig"> <bean class="com.raincat.common.config.TxDbConfig"> <property name="url" value="jdbc:mysql://localhost:3306/tx?useUnicode=true&characterEncoding=utf8"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> </property> </bean> <!-- <property name="compensationCacheType" value="db"/> <property name="txDbConfig"> <bean class="com.raincat.common.config.TxDbConfig"> <property name="url" value="jdbc:mysql://192.168.1.68:3306/alipay?useUnicode=true&characterEncoding=utf8"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="password" value="Wgj@555888"/> <property name="username" value="xiaoyu"/> </bean> </property> <property name="compensationCacheType" value="redis"/> <property name="txRedisConfig"> <bean class="com.raincat.common.config.TxRedisConfig"> <property name="hostName" value="192.168.1.78"/> <property name="port" value="6379"/> <property name="password" value=""/> </bean> </property> <property name="compensationCacheType" value="zookeeper"/> <property name="txZookeeperConfig"> <bean class="com.raincat.common.config.TxZookeeperConfig"> <property name="host" value="192.168.1.132:2181"/> <property name="sessionTimeOut" value="100000"/> <property name="rootPath" value="/tx"/> </bean> </property> <property name="compensationCacheType" value="mongodb"/> <property name="txMongoConfig"> <bean class="com.raincat.common.config.TxMongoConfig"> <property name="mongoDbUrl" value="192.168.1.78:27017"/> <property name="mongoDbName" value="happylife"/> <property name="mongoUserName" value="xiaoyu"/> <property name="mongoUserPwd" value="123456"/> </bean> </property> <property name="compensationCacheType" value="file"/> <property name="txFileConfig"> <bean class="com.raincat.common.config.TxFileConfig"> <property name="path" value=""/> <property name="prefix" value="tx"/> </bean> </property> --> </beans>
View Code
将协调者的地址 以及 事务补偿数据库链接配置成正确的
(2)然后在启动类上增加如下注解,以配置生效
@ImportResource({"classpath:applicationContext.xml"})
4.分布式事务处理
在需要进行分布式事务处理的接口上,增加如下注解:
@TxTransaction
四、启动demo示例
作者提供了示例工程,以便使用者能快速体验raincat。
地址见: