The use scenario of Steata distributed transactions is a back-end management system where the amount of concurrency is not very large.
Steate introduces and uses the official website: http://seata.io/zh-cn/docs/overview/what-is-seata.html
The distributed transaction solution we use here is the AT mode, which is the 2pc mode
we use It is version 0.7.1. Different versions may use different ways.
-
seata wants to control distributed transactions
-
1) Every microservice database must create a uodo_log table
– note that 0.7.1+ adds the field context
CREATE TABLEundo_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,
PRIMARY KEY (id
),
UNIQUE KEYux_undo_log
(xid
,branch_id
)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-
2), install the transaction coordinator seata-server https://github.com/seata.seata/releases
registry.conf registry configuration modification registry type=nacos, the address is changed to the connection address of nacos.
Then start the service -
3) Integration
1. Import dependency spring-cloud-starter-alibaba-seata | seata-all-0.7.1
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
- 3. All microservices that want to use distributed transactions use seata DataSourceProxy
@Configuration
public class MySeataConfig {
@Autowired
DataSourceProperties dataSourceProperties;
@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
if (StringUtils.hasText(dataSourceProperties.getName())) {
dataSource.setPoolName(dataSourceProperties.getName());
}
return new DataSourceProxy(dataSource);
}
}
- 4. Import
registry.conf for each service.
The service.vgroup_mapping configuration of file.conf file.conf must be
set consistent with spring.application.name: vgroup_mapping.gulimall-order-fescar-service-group = "default", or Modify the suffix by configuring spring.cloud.alibaba.seata.tx-service-group, but it must be consistent with the configuration in file.conf
- 5. Big transaction: @GlobalTransactional
- 6. Remote small transactions: @Transactional
is used at the entrance of @Transactional , while other remote calls to open transactions are local transactions @Transactional.