一、前言

关于2PC的理论知识请见:分布式_理论_03_2PC

这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验。

二、源码

源码请见:

https://github.com/yu199195/Raincat

相关视频

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&amp;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&amp;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>

将协调者的地址 以及 事务补偿数据库链接配置成正确的

(2)然后在启动类上增加如下注解,以配置生效

@ImportResource({"classpath:applicationContext.xml"})

4.分布式事务处理

在需要进行分布式事务处理的接口上,增加如下注解:

@TxTransaction

四、启动demo示例

作者提供了示例工程,以便使用者能快速体验raincat。

地址见:

quick start (springcloud)

1.clone & build

打开git bash 运行如下命令

git clone git@github.com:yu199195/Raincat.git
cd Raincat
mvn -DskipTests clean install -U

2.启动TxManagerApplication

此工程为分布式事务的协调者

  • 配置txManaager, 修改application.properties中你自己的redis配置
  • 启动TxManagerApplication

3.引入依赖包(sample已经引入)

   <dependency>
<groupId>com.raincat</groupId>
<artifactId>raincat-springcloud</artifactId>
<version>${your.version}</version>
</dependency>

4.数据库准备

执行 raincat-springcloud-sample 工程 sql文件 springcloud-sample.sql

5.配置文件

(1)在每个工程下 application.yml 中配置您的数据库连接(只需要改ip和端口)

(2)在每个工程下 applicationContext.xml中的TxDbConfig 配置您的补偿数据库连接,提供单独的数据库来存储。

6. @TxTransaction

在需要做分布式事务的接口上加上注解 @TxTransaction (sample已经加上)

7.启动服务

依次启动

  • AliPayApplication
  • WechatApplication
  • PayApplication

8.体验测试

访问API接口列表,进行体验测试

http://localhost:8881/pay-service/swagger-ui.html

分布式事务_01_2PC框架raincat快速体验的更多相关文章

  1. 分布式_事务_01_2PC框架raincat快速体验1

    一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...

  2. 分布式事务_03_2PC框架raincat源码解析-事务提交过程

    一.前言 前面两节,我们已经将raincat的demo工程启动,并简单分析了下事务协调者与事务参与者的启动过程. 这一节,我们来看下raincat的事务提交过程. 二.事务提交过程概览 1.二阶段对应 ...

  3. 分布式事务_02_2PC框架raincat源码解析-启动过程

    一.前言 上一节已经将raincat demo工程运行起来了,这一节来分析下raincat启动过程的源码 主要包括: 事务协调者启动过程 事务参与者启动过程 二.协调者启动过程 主要就是在启动类中通过 ...

  4. Hmily:高性能异步分布式事务TCC框架

    Hmily框架特性 无缝集成Spring,Spring boot start. 无缝集成Dubbo,SpringCloud,Motan等rpc框架. 多种事务日志的存储方式(redis,mongdb, ...

  5. 分布式_事务_02_2PC框架raincat源码解析

    一.前言 上一节已经将raincat demo工程运行起来了,这一节来分析下raincat的源码 二.协调者启动过程 主要就是在启动类中通过如下代码来启动 netty nettyService.sta ...

  6. 高性能异步分布式事务TCC框架(资料汇总)

    https://github.com/yu199195/hmily tcc源码解析系列(一)之项目结构 https://yu199195.github.io/2017/10/11/TCC/tcc-on ...

  7. 分布式事务专题笔记(一) 基础概念 与 CAP 理论

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.基础概念 1.什么是事务 什么是事务?举个生活中的例子:你去小卖铺买东西,“一手交钱,一手交货”就是 ...

  8. Album++:分布式事务专辑-基础概念

    (一)基础概念:↓ ↓ ↓ 1.1)什么是事务 什么是事务?举个生活中的例子:你去小卖铺买东西,"一手交钱,一手交货"就是一个事务的例子,交钱和交货必 须全部成功, 事务才算成功, ...

  9. Spring Cloud Alibaba分布式事务组件 seata 详解(小白都能看懂)

    一,什么是事务(本地事务)? 指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 简单的说,事务就是并发控制的单位,是用户定义的一个操作序列.      而一个逻辑工作单元要成 ...

随机推荐

  1. SpringBoot入门1—简介及helloworld

    Spring Boot简介 Spring Boot让我们的Spring应用变的更轻量化.比如:你可以仅仅依靠一个Java类来运行一个Spring引用.你也可以打包你的应用为jar并通过使用java - ...

  2. PyQt4 颜色选择,字体选择代码

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  3. JavaScript实现自适应窗口大小的网页

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  4. django用户信息扩展

    Django封装了好多东西,拿来用就可以了,帮我们封装类用户的登录认证,用户的表 所以Django自带有用户表,当扩展用户表后一些表就会被替换 用户认证相关的    功能放在django.contri ...

  5. mssql 中文乱码 字库集 问题解决方法

    The database could not be exclusively locked to perform the operation(SQL Server 5030错误解决办法)   SQL S ...

  6. Serv-u 外网访问内网的FTP服务器

    1. 背景简介 最近研究如何在内网搭架FTP服务器,同时要保证外网(公网)能访问的到.终成正果,但走了一些弯路,在此记下,以飨后人. 2. 基础知识 FTP 使用 2 个端口,一个数据端口和一个命令端 ...

  7. VSCode隐藏node_modules目录

    使用VSCode,打开一个工程时,发现node_modules目录包含到工程中了,问题: settings.json配置如下,可以自己定制忽略的文件夹: search.exclude 用来忽略搜索的文 ...

  8. 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?

    重定向与请求分派 “局部”参数——ServletConfig——servlet初始化参数 “全局”参数——ServletContext——上下文初始化参数 Web app的“构造器”——Servlet ...

  9. nodejs模块Phantom,无界面浏览器

    PhantomJS 是一个无界面的 webkit 内核浏览器,

  10. 生信概念之global alignment VS local alignment