How to Use SpringBoot, Dubbo, and Seata to Solve Distributed Transaction Data Inconsistency
This article walks through integrating SpringBoot, Dubbo, and Seata to implement reliable distributed transactions, covering project setup, Maven dependencies, service definitions, Nacos registration, configuration files, testing normal commits and rollbacks, and troubleshooting version incompatibilities.
Background
Within Spring Cloud there are two remote‑call mechanisms: HTTP‑based tools (e.g., OpenFeign, HttpClient) and Dubbo, which uses TCP for higher efficiency. The guide uses Dubbo to integrate with Seata for distributed transaction consistency.
Solution Practice
2.1 Create Service Interface
Create a Maven module seata-dubbo-api and define the service contract:
public interface StockApi {
/**
* Reduce stock
* @param productCode
* @param count
* @return
*/
boolean deduct(String productCode, int count);
}2.2 Create Stock Service
Build a Spring Boot project seata-dubbo-stock. Add the following dependencies (Spring Boot 2.2.5.RELEASE, Spring Cloud Hoxton.SR3, Spring Cloud Alibaba 2.2.1.RELEASE, Dubbo starter, Seata starter, MySQL driver, MyBatis, Nacos discovery, etc.) to pom.xml.
Configure application.properties:
spring.application.name=seata-dubbo-stock
server.port=9002
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata-stock
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.scan.base-packages=com.example.cloud.nacos.dubbo.seata
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://${spring.cloud.nacos.discovery.server-addr}
seata.application-id=seata-dubbo-stock
seata.tx-service-group=my_test_tx_group
seata.service.vgroup-mapping.my_test_tx_group=default
seata.service.grouplist.default=127.0.0.1:8091Implement the Dubbo service:
@com.alibaba.dubbo.config.annotation.Service
public class StockApiImpl implements StockApi {
@Autowired
private StockService stockService;
@Override
public boolean deduct(String productCode, int count) {
try {
stockService.deduct(productCode, count);
return true;
} catch (Exception e) {
return false;
}
}
}Start the service and verify registration in Nacos.
2.3 Create Order Service
Create a Spring Boot module seata-dubbo-order with the same dependencies. Add the following configuration (port 9001, application name seata-dubbo-order) to application.properties:
spring.application.name=seata-dubbo-order
server.port=9001
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/seata-stock
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.scan.base-packages=com.example.cloud.nacos.dubbo.seata
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://${spring.cloud.nacos.discovery.server-addr}
dubbo.consumer.check=false
seata.application-id=seata-dubbo-order
seata.tx-service-group=my_test_tx_group
seata.service.vgroup-mapping.my_test_tx_group=default
seata.service.grouplist.default=127.0.0.1:8091Implement OrderService that calls the Dubbo‑exposed StockApi and is annotated with @GlobalTransactional:
@Component
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@com.alibaba.dubbo.config.annotation.Reference
private StockApi stockApi;
@GlobalTransactional
public void create(String userId, String productCode, int orderCount) throws Exception {
// remote stock deduction via Dubbo
stockApi.deduct(productCode, orderCount);
Order order = new Order();
order.setUserId(userId);
order.setProductCode(productCode);
order.setCount(orderCount);
order.setMoney(orderCount * 100);
// persist order
orderMapper.insert(order);
}
}Start the order service and confirm its registration in Nacos.
2.4 Service Testing
2.4.1 Normal Transaction Commit
Initialize MySQL databases seata-stock and seata-order with sample data. Invoke the order creation endpoint:
http://127.0.0.1:9001/order/create?userId=张三&productCode=wahaha&orderCount=1The request triggers (1) a Dubbo call that reduces the stock of wahaha by 1 and (2) insertion of an order record if the stock deduction succeeds. After the call, the stock table shows a decrement of 1 and a new row appears in the order table. Log output contains the “Branch commit result” indicating successful two‑phase commit.
2.4.2 Exception Rollback
Modify OrderService.create() to throw an exception after the order insertion. Re‑initialize the databases and invoke the same endpoint. Both the stock and order tables remain unchanged, confirming that Seata rolls back the distributed transaction.
Seata Service Address Configuration
When the number of Seata TC servers grows, hard‑coding their addresses becomes unwieldy. Register the TC server in Nacos so that clients discover it dynamically.
3.1 Server‑Side Configuration
Copy the store.registry section from conf/application.example.yml to conf/application.yml and set registry.type to nacos. Restart the Seata TC server.
3.2 Client‑Side Configuration
Add the following to each service’s application.properties:
seata.registry.type=nacos
seata.registry.nacos.application=seata-server
seata.registry.nacos.server-addr=127.0.0.1:8848
seata.registry.nacos.group=SEATA_GROUPRestart the services and repeat the order‑creation test; results are unchanged.
3.3 Error Troubleshooting
If an error such as “Seata client version incompatible with server” appears, check the client and server versions. The example uses Seata server 1.5.2 and client 1.1.0, which are incompatible. Upgrading the client (e.g., to 1.3.0) via spring-cloud-alibaba.version resolves the issue.
Conclusion
The guide shows how to combine Spring Boot, Dubbo, and Seata to achieve reliable distributed transactions, covering project scaffolding, Maven configuration, service implementation, Nacos registration, testing of commit and rollback scenarios, and version‑compatibility troubleshooting.
References
https://seata.apache.org/zh-cn/docs/overview/what-is-seata/
https://www.iocoder.cn/Seata/install/
https://www.iocoder.cn/Spring-Boot/Seata/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
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.
