10 (spring cloud Alibaba) introduction to distributed things

Posted by EricTRocks on Mon, 20 Dec 2021 19:57:43 +0100

Distributed things

thing:

To ensure the ACID of data in a database

We need to do things. In a database, multiple sql statements are either executed or not executed

There are many conditions, including item isolation level

Distributed things

The current architectures are all distributed architectures. In order to ensure that multiple databases can be implemented

A database, B database, C database. Sql in is either executed or not executed. Distributed things

The single application is split into micro service applications, and the original three modules are split into three independent applications, using three independent data sources respectively,
Business operations need to call three services to complete. At this time, the data consistency within each service is guaranteed by local transactions, but the global data consistency cannot be guaranteed.

Seata

Seata is an open source distributed transaction solution, which is committed to providing high-performance and easy-to-use distributed transactions under the microservice architecture

Official website

http://seata.io/zh-cn/

You can view the documents on the official website and download them

term

  1. TC (Transaction Coordinator) - Transaction Coordinator
    Maintain the status of global and branch transactions and drive global transaction commit or rollback.
  2. TM (Transaction Manager) - transaction manager
    Define the scope of a global transaction: start a global transaction, commit or roll back a global transaction.
  3. RM (Resource Manager) - Resource Manager
    Manage the resources of branch transaction processing, talk with TC to register branch transactions and report the status of branch transactions, and drive branch transaction commit or rollback.

Processing process

An ID + three component model for distributed transaction processing

  1. TM applies to TC to start a global transaction. The global transaction is successfully created and a globally unique XID is generated;
  2. XID propagates in the context of the microservice invocation link;
  3. RM registers branch transactions with TC and brings them under the jurisdiction of global transactions corresponding to XID;
  4. TM initiates a global commit or rollback resolution for XID to TC;
  5. TC schedules all branch transactions under XID to complete the commit or rollback request.

Installation and configuration of Seata

There are many online statements in this part

The installation must be very simple, needless to say.

The download is a file.

The windows version is different from the Linux version

to configure

We all know that the configuration file must be in the conf directory

In order to ensure security, we can first clone a file for operation configuration

The formula we're going to operate on next is file Conf file

Main modifications:

Name of custom transaction group

service module

The transaction log storage mode is DB + database connection information

store module

Adding databases and tables to a database

We specified the database information during configuration. So for later use. We have corresponding operations in the data.

Create a seata database

Creating tables in Seata

The documents shown in table are provided in Seata

Modify GIStry Conf configuration

Designated registry

start-up

Start Nacos start Seata

alibaba's technology is very powerful. Integrate our distributed technologies into a component-by-component technology.

  1. nacos: registering
  2. sentinel: Service fuse
  3. seata: distributed transactions

Build environment

Distributed things use Seata here. I built an environment based on Silicon Valley

If you can, you'd better take a look at the original video of Silicon Valley. I feel very low below myself

If you can, you'd better take a look at the original video of Silicon Valley. I feel very low below myself

If you can, you'd better take a look at the original video of Silicon Valley. I feel very low below myself

Silicon Valley spring cloud framework development tutorial (spring cloud Alibaba microservice distributed architecture Spring Cloud)_ Beep beep beep_ bilibili

The following content can be said to be written for myself.....

Import dependency

 <dependencies>
        <!--nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>seata-all</artifactId>
                    <groupId>io.seata</groupId>
                </exclusion>
            </exclusions>
        </dependency>
     
<!--  ##     This is written to make the dependency of different versions more appropriate -- >
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <version>0.9.0</version>
        </dependency>
        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--web-actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--mysql-druid-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

pom

spring:
  application:
    name: seata-order-service
  cloud:
    alibaba:
      seata:
        #The user-defined transaction group name needs to correspond to that in Seata server
        tx-service-group: fsp_tx_group
    nacos:
      discovery:
        server-addr: localhost:8848
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/seata_order
    username: root
    password: 123456

file.conf

Introduce your own seata configuration file here

registry.conf

Also introduce your own seata configuration file

Other layers

dao layer, entity layer, business logic layer, controller layer, etc

These are self written.

Configuration class

Here, the configuration of Mybatis is shown in the form of configuration class.

It's because yaml may conflict with in Seata

/**
 * @auther zzyy
 * @create 2019-12-11 16:57
 */
@Configuration
@MapperScan({"com.atguigu.springcloud.alibaba.dao"})
public class MyBatisConfig {
}
/**
 * @auther zzyy
 * @create 2019-12-11 16:58
 * Proxy data sources using Seata
 */
@Configuration
public class DataSourceProxyConfig {

    @Value("${mybatis.mapperLocations}")
    private String mapperLocations;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    @Bean
    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
        return new DataSourceProxy(dataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceProxy);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }

}

Main start

/**
 * @auther zzyy
 * @create 2019-12-11 17:02
 */
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//Cancel automatic creation of data source
public class SeataOrderMainApp2001
{

@GlobalTransactional annotation

In controller

Add when calling database operations of other microservices. So as to open the global database transaction

Of course, other micro service configurations should be similar to those here

Topics: Distribution seata