Spring cloud integrates with Seata and uses Nacos as the registration center and configuration center to build the initial experience

Keywords: Spring MySQL Database github

Recently, we are introducing distributed transaction technology for the project. At present, Seata is a good choice. I plan to take a look at it. This article is purely a personal learning note, without any responsibility. In this article, nacos-1.2.0, SpringBoot-2.3.0, seata-1.2.0, mysql-5.7 are used as the database of Seata server's highly available db mode to build the demo of distributed transaction.

Step 1: nacos installation

Download nacos from nacos official website at: https://github.com/alibaba/nacos/releases Currently, the latest version is 1.2.0. Download it directly in standalone mode. This article does not focus on nacos, so a single node can be used.

Step 2: seata server installation

1. The online tutorials are basically based on the old version. The deployment of seata-1.2.0 is a little different, with some configuration files missing. Let's download the executable first. Address: https://seata.io/zh-cn/blog/download.html , and modify conf after decompression/ registry.conf File:

registry {
  type = "nacos"
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1"
    namespace = ""
    cluster = "default"
  }
}
config {
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1"
    namespace = ""
    group = "SEATA_GROUP"
  }
}

Because nacos is used as the registry, the file.conf Don't worry. Then you can start seata directly: sh bin/seata-server.sh , you can see a service named seata server in nacos.

2. Because seata uses mysql as the highly available database of db, it is necessary to create a seata database in mysql and import the database script. The script address is: https://github.com/seata/seata/tree/develop/script/server/db , I also have the database script in the source code I submitted. In the DOC of the project/ seata.sql You need to import undo in the business library_ Log table, script in https://github.com/seata/seata/tree/develop/script/client/at/db In my source code.

3. Import the configuration to the configuration center of nacos, and https://github.com/seata/seata/tree/develop/script/config-center Download here confix.txt Configuration file and download nacos to nacos directory- config.sh Scripts, modifying config.txt Document content:

service.vgroupMapping.my_test_tx_group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=username
store.db.password=password
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

It's about vgroupMapping.my_ test_ Tx_ The group is consistent with the configuration in the project. Execute nacos-config.sh hold config.txt File imported to nacos configuration center, execution failed to find config.txt If you want to change nacos-config.sh It's about config.txt The path points to your local one.

Step 3: add related dependencies and global transactions to the program,

nacos registry and configuration dependency:

<!--Registry client-->
<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-nacos-config</artifactId>
</dependency>

seata relies on:

<!--Seata package-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-seata</artifactId>
    <version>2.2.0.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

application.yml Document plus:

seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: my_test_tx_group
  config:
    type: nacos
    nacos:
      namespace:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP

  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace:

Of course, feign's dependence is also indispensable:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Then annotate where distributed transactions are needed: @ global transactional (rollback for= Exception.class )

@GlobalTransactional(rollbackFor = Exception.class)
public String booking(String userId, String productId, int count) {
    BigDecimal money = new BigDecimal(1000);
    log.info("Order, user:{},Products:{},number:{}",userId,productId,count);
    /**Here are the call order service, user service and product service*/
    orderClient.create(userId,productId,count,money);
    userClient.debit(userId,money);
    productClient.deduct(productId,count);
    return "success";
}

Call the interface test, you can see whether the distributed transaction is effective or not. You can also download the following source code for testing. First, put the spring cloud sea ta- demo.sql After the script imports the database and starts all the projects, POST accesses http://localhost:9999/business/booking , see the interface for specific parameters.

 

Source address: Click download

 

Posted by fluvius on Mon, 08 Jun 2020 23:16:09 -0700