官网下载 选择版本1.5.2
https://github.com/seata/seata/releases
修改seata/conf/application.yml文件内容:
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ./logs/seata
# extend:
# logstash-appender:
# destination: 127.0.0.1:4560
# kafka-appender:
# bootstrap-servers: 127.0.0.1:9092
# topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos 、 consul 、 apollo 、 zk 、 etcd3
type: nacos
nacos:
# nacos ip地址
server-addr: 123.249.xx.xxx:8848
group: dev
namespace: dev
username:
password:
dataId: seata-server.properties
registry:
# support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa
type: nacos
nacos:
application: seata-server
# nacos ip地址
server-addr: 123.249.xx.xxx:8848
group: dev
namespace: dev
cluster: default
username:
password:
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
创建seata数据库
seata库创建表 三张
CREATE TABLE `branch_table` (
`branch_id` bigint(20) NOT NULL,
`xid` varchar(128) COLLATE utf8mb4_hungarian_ci NOT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`resource_group_id` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`resource_id` varchar(256) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`lock_key` varchar(128) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`branch_type` varchar(8) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`status` tinyint(4) DEFAULT NULL,
`client_id` varchar(64) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`application_data` varchar(2000) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
CREATE TABLE `global_table` (
`xid` varchar(128) COLLATE utf8mb4_hungarian_ci NOT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`status` tinyint(4) NOT NULL,
`application_id` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`transaction_service_group` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`transaction_name` varchar(128) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`timeout` int(11) DEFAULT NULL,
`begin_time` bigint(20) DEFAULT NULL,
`application_data` varchar(2000) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
CREATE TABLE `lock_table` (
`row_key` varchar(128) COLLATE utf8mb4_hungarian_ci NOT NULL,
`xid` varchar(96) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`transaction_id` mediumtext COLLATE utf8mb4_hungarian_ci,
`branch_id` mediumtext COLLATE utf8mb4_hungarian_ci,
`resource_id` varchar(256) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`table_name` varchar(32) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`pk` varchar(36) COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`row_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
sc-order 业务库创建日志表 undo_log
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
seata-server.properties nacos配置文件
store.mode=db
store.lock.mode=db
store.session.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://49.7.xx.xxx:20006/sc_dev_seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=sx-admin
store.db.password=xxxxxx
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
service.vgroupMapping.default_tx_group=default
启动seata服务:
==如果报错/无法启动 : - 必须bin目录 ==
./seata-server.sh -h 123.249.xx.xxx
客户端添加seata依赖
<!--移除alibaba-seata自带的seata-spring-boot-starter,因为自带的版本太低 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加seata-spring-boot-starter依赖,版本需要与服务端版本相对应,此版本使用1.5.2 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
客户端配置文件 -nacos配置文件
seata:
# 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
enabled: true
# Seata 应用编号,默认为 ${spring.application.name}
application-id: ${spring.application.name}
# Seata 事务组编号,用于 TC 集群名。该配置需要与服务端提到的group相对应,也需要与下面的相对应 sc-order-web-group
tx-service-group: default_tx_group # ${spring.application.name}-group
# 关闭自动代理
enable-auto-data-source-proxy: false
# 服务配置项
service:
# 虚拟组和分组的映射
vgroupMapping:
default_tx_group: default #该配置需要与服务端提到的group相对应
grouplist:
default: 123.249.xx.xxx:8091
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
serverAddr: 123.249.xx.xxx:8848
namespace: dev # 需要与服务端添加的配置文件相同
group: dev
username: nacos
password: nacos
dataId: seata-server.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
serverAddr: 123.249.xx.xxx:8848
namespace: dev # 需要与服务端添加的配置文件相同
group: dev # 需要与服务端添加的配置文件相同
username: nacos
password: nacos
dataId: seata-server.properties
mybatis-plus:
global-config:
banner: false
configuration:
#jdbc-type-for-null: null
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.deltaphone.sc.material.domain.entity
手动代理 - 添加
@Configuration
@Data
public class DataSourceProxyConfig {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String userName;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driveClassName;
@Bean("dataSource")
public DataSource druidDataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(url);
hikariDataSource.setUsername(userName);
hikariDataSource.setPassword(password);
hikariDataSource.setDriverClassName(driveClassName);
return hikariDataSource;
}
@Bean
@Primary
public DataSourceProxy dataSourceProxy(DataSource dataSource) {
return new DataSourceProxy(dataSource);
}
}
结果
1.服务端客户端分别在nacos有一个配置文件
2.需要这个配置
3.docker运行需要2个端口
7091 8091