前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分布式_事务_01_2PC框架raincat快速体验1

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

作者头像
shirayner
发布2018-10-10 11:35:24
3790
发布2018-10-10 11:35:24
举报

一、前言

关于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.体验测试

访问http://localhost:8881/pay-service/swagger-ui.html 进行体验测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、源码
  • 三、接入步骤
    • 1.启动 TxManagerApplication
      • 2.引入依赖
        • 3.配置文件
          • 4.分布式事务处理
          • 四、启动demo示例
            • 1.clone & build
              • 2.启动TxManagerApplication
                • 3.引入依赖包(sample已经引入)
                  • 4.数据库准备
                    • 5.配置文件
                      • 6. @TxTransaction
                        • 7.启动服务
                          • 8.体验测试
                          相关产品与服务
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档