创建一个SpringBoo web项目,先贴下完整的项目结构:
SpringCloud其他依赖这里不做展示,仅展示byteTCC的依赖,由于gitHub提供的demo中依赖较多,可能是作者在框架内部使用了很多依赖,下面的依赖,如果没有jar冲突,建议保留,如果删除,请做个测试。(下文依赖做过多次调试和测试才保留的,建议慎重删除):
org.bytesoft
bytetcc-supports-springcloud
0.4.18
asm
asm
org.springframework
spring-beans
4.3.7.RELEASE
org.springframework
spring-core
4.3.7.RELEASE
org.springframework
spring-expression
4.3.7.RELEASE
org.aspectj
aspectjweaver
1.8.10
org.springframework
spring-web
4.3.4.RELEASE
org.springframework
spring-webmvc
4.3.4.RELEASE
org.apache.commons
commons-dbcp2
2.1.1
mysql
mysql-connector-java
5.1.37
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.cloud
spring-cloud-starter-zipkin
1.2.4.RELEASE
待修改
server:
port: 8022
datasource:
url: jdbc:mysql://x.x.x.x:3306/user?useAffectedRows=true
username: xxxx
password: xxxxxx
driver-class-name: com.mysql.jdbc.Driver
eureka:
client:
service-url:
default: http://127.0.0.1:8761/eureka
healthcheck:
enabled: true
#mybatis:
# mapper-locations: classpath:mapper/*.xml
# type-aliases-package: com.java4all.entity
spring:
application:
name: bank-server4
在byteTCC 0.4.x版本中我们需要封装一下数据源,补偿型service中应该使用org.bytesoft.bytejta.supports.jdbc.LocalXADataSource封装过的数据源。0.5.x版本不需要此步骤。数据建议从配置文件取,不要在java文件中写死。
package com.java4all.config;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.bytesoft.bytejta.supports.jdbc.LocalXADataSource;
import org.bytesoft.bytetcc.supports.springcloud.config.SpringCloudConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* 配置数据源
*
* 补偿型service中应该使用org.bytesoft.bytejta.supports.jdbc.LocalXADataSource封装过的数据源。
*/
@Import(SpringCloudConfiguration.class)
@Configuration
public class ProviderConfig {
@Bean(name = "dataSource")
public DataSource getDataSource() {
LocalXADataSource dataSource = new LocalXADataSource();
dataSource.setDataSource(this.invokeGetDataSource());
return dataSource;
}
public DataSource invokeGetDataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://xx.xx.xx.xx:3306/inst01");
bds.setUsername("xxxxx");
bds.setPassword("xxxxx");
bds.setMaxTotal(50);
bds.setInitialSize(20);
bds.setMaxWaitMillis(60000);
bds.setMinIdle(6);
bds.setLogAbandoned(true);
bds.setRemoveAbandonedOnBorrow(true);
bds.setRemoveAbandonedOnMaintenance(true);
bds.setRemoveAbandonedTimeout(1800);
bds.setTestWhileIdle(true);
bds.setTestOnBorrow(false);
bds.setTestOnReturn(false);
bds.setValidationQuery("select 'x' ");
bds.setValidationQueryTimeout(1);
bds.setTimeBetweenEvictionRunsMillis(30000);
bds.setNumTestsPerEvictionRun(20);
return bds;
}
@Bean
public JdbcTemplate getJdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(this.getDataSource());
return jdbcTemplate;
}
}
启动类中需要引入配置文件,文件由框架提供默认值,注解导入即可。
package com.java4all;
import org.bytesoft.bytetcc.supports.springcloud.config.SpringCloudConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
//引入byteTCC配置
@ImportResource({"classpath:bytetcc-supports-springcloud.xml"})
@Import(SpringCloudConfiguration.class)
//配置扫描的应用包
@SpringBootApplication(scanBasePackages = "com.java4all")
@EnableEurekaClient
@EnableDiscoveryClient
public class Bank4Application {
public static void main(String[] args) {
SpringApplication.run(Bank4Application.class, args);
}
}
在参与分布式事务操作的表所在的库,必须添加bytejta表;
这里同时添加一个user表,方便演示:
CREATE TABLE `user` (
`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 `user` (`id`, `money`, `frozen`) VALUES ('1', '10000', '0');