zoukankan      html  css  js  c++  java
  • 分布式_事务_01_2PC框架raincat快速体验1

    一、前言

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

  • 相关阅读:
    Web上传大文件的解决方案
    JS上传大文件的解决方案
    网页上传大文件的解决方案
    B/S上传大文件的解决方案
    Unity UGUI——提供可视功能的UI组件(Text)
    Java设计模式透析之 —— 策略(Strategy)
    【边做项目边学Android】小白会遇到的问题--Appcompat_V7问题
    高度平衡树 -- AVL 树
    成长这事儿,不可不说-------Day36
    Cocos2D-X2.2.3学习笔记5(UI系统)
  • 原文地址:https://www.cnblogs.com/shirui/p/9697867.html
Copyright © 2011-2022 走看看