SpringCloud集成分布式事务管理框架lcn

parent-pom.xml:
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>springcloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <name>springcloud</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <!--子模块-->
    <modules>
        <module>eureka-server</module>
        <module>member-service</module>
        <module>order-service</module>
        <module>zuul-service</module>
        <module>config-service</module>
        <module>config-client</module>
        <!--接口包-->
        <module>base-model-order</module>
        <module>lcn-manager</module>
    </modules>

    <dependencies>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>


        <!--lcn事务-->
        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-txmsg-netty</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
注意SpringBoot跟SpringCloud的版本:这是个大坑
lcn-manager.pom
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tm</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>lcn-manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lcn-manager</name>
    <description>Demo project for Spring Boot</description>
</project>
--------------------------------------------------------------------------------
lcn-manager.properties: 注意数据库选择一个能连接上的即可,不需要手动创建,不存在会自动创建

#应用名称
spring.application.name=tx-manager
server.port=7970
#DataSource配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=1
#redis 配置
spring.redis.host=122.51.71.115
spring.redis.port=6379
spring.redis.password=

#注册中心地址
eureka.client.serviceUrl.defaultZone= http://localhost:8080/eureka/
# 注册名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
# 设置注册到服务的为ip
eureka.instance.prefer-ip-address=true
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。  default:false
mybatis.configuration.use-generated-keys=true

# tx-manager ip(client请求ip)
#tx-lcn.manager.host=127.0.0.1
# client 请求端口
#tx-lcn.manager.port=8070

tx-lcn.client.manager-address=127.0.0.1:8070
# 心跳检测时间 单位:ms
tx-lcn.manager.heart-time=12000
# 事务执行总时间
tx-lcn.manager.dtx-time=10000
# 参数延迟删除时间单位ms
tx-lcn.message.netty.attr-delay-time=10000
tx-lcn.manager.concurrent-level=128
# 开启日志
tx-lcn.logger.enabled=true
logging.level.com.codingapi=debug
tx-lcn.logger.driver-class-name=${spring.datasource.driver-class-name}
tx-lcn.logger.jdbc-url=${spring.datasource.url}
tx-lcn.logger.username=${spring.datasource.username}
tx-lcn.logger.password=${spring.datasource.password}
tx-lcn.manager.ex-url=/provider/email-to/xiaoluoheng@foxmail.com
tx-lcn.manager.ex-url-enabled=true
spring.jpa.hibernate.ddl-auto=update
tx-lcn.manager.admin-key=123456


--------------------------------------------------------------------------------
package com.example.lcnmanager;

import com.codingapi.txlcn.tm.config.EnableTransactionManagerServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
//注册到Eureka中
@EnableEurekaClient
//分布式事务管理server
@EnableTransactionManagerServer
public class LcnManagerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LcnManagerApplication.class, args);
    }

}

--------------------------------------------------------------------------------
保证lcn-manager项目正常启动


集成SringCloud:

在不同的微服务中启动类上加:
@EnableDistributedTransaction //开启分布式事务管理

/**
 * 新增数据
 *
 * @param user 实例对象
 * @return 实例对象
 */
@Override
//调用方与被调用方的业务方法上加注解即可
@LcnTransaction
@Transactional
public User insert(User user) {
    this.userDao.insert(user);
    return user;
}

 最坑的是版本的兼容性,这个东西只能试验试错,总结

项目下载地址:https://gitee.com/dongfangxuri/lcn-sprongcloud.git
欢迎拍砖

 

posted @ 2020-02-25 18:26  洞玄巅峰  阅读(1039)  评论(0编辑  收藏  举报