这里详细记录下,SpringCloud框架整合byteTCC分布式事务框架的过程。
1.SpringCloud 2.SpringBoot 3.byteTCC
创建一个SpringBoo web项目,先贴下完整的项目结构:
SpringCloud其他依赖这里不做展示,仅展示byteTCC的依赖:
<!--byteTcc-->
<dependency>
<groupId>org.bytesoft</groupId>
<artifactId>bytetcc-supports-springcloud</artifactId>
<version>0.5.0-BETA2</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper-discovery</artifactId>
</exclusion>
<exclusion>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-discovery</artifactId>
</exclusion>
</exclusions>
</dependency>
在启动类需要添加几个注解:
package com.java4all;
import org.bytesoft.bytetcc.supports.springcloud.config.SpringCloudSecondaryConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Import;
/**
* 按请求粒度负载均衡(使用MongoDB存储事务日志):需引入SpringCloudConfiguration;
* 按事务粒度负载均衡(使用文件系统存储事务日志):需引入SpringCloudSecondaryConfiguration;
*/
@EnableEurekaClient
@SpringBootApplication(scanBasePackages = "com.java4all")
@MapperScan("com.java4all.dao")
@Import(SpringCloudSecondaryConfiguration.class)
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
//使用文件存储时,不需要配置mongodb
public class CompanyServerApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyServerApplication.class, args);
}
}
配置数据库时,注意type参数需要指定:
server:
port: 8013
spring:
application:
name: company-server
datasource:
url: jdbc:mysql://116.62.62.26:3306/company?useAffectedRows=true
username: root
password: 14789632
driver-class-name: com.mysql.jdbc.Driver
type: org.apache.commons.dbcp2.BasicDataSource
#这个type必须指定,否则会抛出: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection;
eureka:
client:
service-url:
default: http://localhost:8761/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.java4all.entity
在参与分布式事务操作的表所在的库,必须添加bytejta表;
这里同时添加一个company表,方便演示:
CREATE TABLE `company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`money` decimal(10,0) DEFAULT NULL,
`frozen` decimal(10,0) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `bytejta` (
`xid` varchar(32) NOT NULL,
`gxid` varchar(40) DEFAULT NULL,
`bxid` varchar(40) DEFAULT NULL,
`ctime` bigint(20) DEFAULT NULL,
PRIMARY KEY (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `company` (`id`, `money`, `frozen`) VALUES ('1', '10000', '0');
# 涉及分布式事务的数据库必须创建bytejta表