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
歡迎拍磚