Step-by-Step Guide to Setting Up Nacos and Seata for Distributed Transactions in Spring Cloud Alibaba
This tutorial walks through the complete environment setup, including rapid installation of Nacos with MySQL mode, configuring Seata 1.5.0 for distributed transactions, creating the required MySQL tables, building micro‑services with Spring Boot, defining Feign clients, enabling global transactions, testing the workflow, and noting important version compatibility issues.
The article begins with an environment setup section, showing how to quickly launch the latest Nacos version in MySQL mode. The default console credentials are nacos/nacos , and the MySQL accounts are root/root and nacos/nacos .
Next, it details the Seata 1.5.0 installation. After downloading the Seata SpringBoot project, the application.yml file is edited to point to Nacos as the registry and to use a MySQL storage mode. The essential configuration snippet is:
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
seata:
config:
type: file
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
namespace:
group: SEATA_GROUP
cluster: default
username: nacos
password: nacos
store:
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true
user: root
password: root
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/loginDatabase tables required by Seata are then created. The SQL scripts for global_table , branch_table , lock_table , distributed_lock , and the sample business tables ( t_account , t_order , t_stock , undo_log ) are provided in full, for example:
CREATE TABLE IF NOT EXISTS `global_table` (
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`status` TINYINT NOT NULL,
`application_id` VARCHAR(32),
`transaction_service_group` VARCHAR(32),
`transaction_name` VARCHAR(128),
`timeout` INT,
`begin_time` BIGINT,
`application_data` VARCHAR(2000),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`xid`),
KEY `idx_status_gmt_modified` (`status`,`gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;After the tables are created, Seata is started with the command seata-server . The console login uses seata/seata .
The project construction section describes a sample e‑commerce scenario involving three micro‑services: Warehouse, Order, and Account. An architecture diagram (image) illustrates the interaction flow.
Business tables for the sample application are defined, including t_account , t_order , and t_stock . Their DDL statements are shown, for instance:
CREATE TABLE `t_account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) DEFAULT NULL,
`amount` double(14,2) DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;Service creation focuses on the Order service. The required Maven dependencies are listed, such as Spring Boot starter web, Alibaba Nacos discovery, Seata starter, Druid, MyBatis‑Plus, and MySQL connector. The pom.xml fragment is:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>The corresponding application.properties configuration sets the server port, application name, Nacos discovery address, datasource details, and MyBatis‑Plus settings.
server.port=81
spring.application.name=order
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata_samples?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImplA simple REST endpoint for creating orders is provided:
@RequestMapping("/add")
public void add(String userId, String commodityCode, Integer count, BigDecimal amount) {
Order order = new Order();
order.setOrderNo(UUID.randomUUID().toString());
order.setUserId(userId);
order.setAmount(amount);
order.setCommodityCode(commodityCode);
order.setCount(count);
orderService.save(order);
}The aggregation (business) service imports the same dependencies plus OpenFeign. Its application.properties runs on port 80 and disables global transaction for the service itself.
server.port=80
spring.application.name=business
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata_samples?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
service.disableGlobalTransaction=false
ribbon.ConnectTimeout=3000
ribbon.ReadTimeout=5000Feign clients for the three services are declared, for example:
@FeignClient(value = "account")
public interface AccountFeign {
@RequestMapping("/account/reduce")
void reduce(@RequestParam("userId") String userId, @RequestParam("amount") BigDecimal amount);
}The business service method that orchestrates the distributed transaction is annotated with @GlobalTransactional and calls the three Feign interfaces sequentially:
@GlobalTransactional
@RequestMapping("/toOrder")
public void toOrder(String userId, String commodityCode, Integer count, BigDecimal amount) {
accountFeign.reduce(userId, amount);
stockFeign.deduct(commodityCode, count);
orderFeign.add(userId, commodityCode, count, amount);
}Testing instructions show how to invoke the business endpoint via a browser URL, observe logs from the account and stock services, and verify that the database state rolls back when the stock quantity is insufficient.
Important notes include replacing the MySQL driver jar (use version 8.x instead of 5.x), ensuring compatibility among Spring Boot, Spring Cloud, and Spring Cloud Alibaba versions, and matching Druid with the appropriate driver version.
The article concludes with a link to the code repository: https://gitee.com/codeWBG/springcloud_alibaba .
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.