1.准备环境:springboot2.0以上+mysql,redis+feign
redis安装:
2.下载压缩包
3.解压后的文件拷贝到 /user/local/目录下
- 切换到:cd /usr/local/redis-4.0.10/
- 编译测试 sudo make test
- 编译安装 sudo make install
- 安装成功后,输入redis-server启动redis
ps axu|grep redis
Kill -9 pid
配置Tx-Manager
2 将“txlcn-tm”module导入到springcloud项目中
启动类加上注解:@EnableDistributedTransaction
@EnableFeignClients(basePackages="boss.net.service")
@EnableEurekaClient
@SpringBootApplication
@MapperScan("boss.net.mapper")
@EnableDistributedTransaction
publicclassSdwanExceptionhandler5001{
publicstaticvoidmain(String[]args){
SpringApplication.run(SdwanExceptionhandler5001.class,args);
}
}
3 创建数据库 tx-manager
4 修改配置,主要配置mysql,注册中心
spring.application.name=tx-Manager
server.port=7970
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://10.21.19.86:3306/sdwan_co_db_v3?characterEncoding=utf-8
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-Manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
#spring.jpa.hibernate.ddl-auto=update
#指定注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:6001/eureka/
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
#TxManagerHostIp
tx-lcn.manager.host=127.0.0.1
#TxClient连接请求端口
tx-lcn.manager.port=8070
#心跳检测时间(ms)
tx-lcn.manager.heart-time=15000
#分布式事务执行总时间
tx-lcn.manager.dtx-time=30000
#参数延迟删除时间单位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
#redis主机
spring.redis.host=127.0.0.1
#redis端口
spring.redis.port=6379
#redis密码
spring.redis.password=
5 。启动这个服务(启动这个服务之前先启动redis)
http://127.0.0.1:7970/admin/index.html#/ 密码:codingapi
6 配置Tx-client
在需要启用事务的模块的pom加上依赖:
<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>
启动类上加上注解 @EnableDistributedTransaction
@EnableFeignClients(basePackages="boss.net.service")
@EnableEurekaClient
@SpringBootApplication
@MapperScan("boss.net.mapper")
@EnableDistributedTransaction
publicclassSdwanExceptionhandler5001{
publicstaticvoidmain(String[]args){
SpringApplication.run(SdwanExceptionhandler5001.class,args);
}
}
在需要事务的方法上加上注解@LcnTransaction
txlcn-tm的地址 https://download.csdn.net/my