当前位置:网站首页>Theoretical basis of distributed services

Theoretical basis of distributed services

2022-06-10 21:04:00 Fairy wants carry

Catalog

Preface

The difference between microservices and distributed

CAP Theorem

BASE theory

 Seata

technological process :

seata Deploy

Microservice Integration seata


 

Preface

Monomer architecture :

1. The project is too bloated , All services together , A business has been suspended , The whole project will not work ;

2. Resources cannot be isolated , All businesses use one resource , Share a database , If a business suddenly increases ——> The database is down , Then other businesses will be affected ;

3. Inflexible expansion , If we want to extend a service , It will expand the whole system horizontally ;

4. The upper limit of all functions , Deploy together ;

Distributed :

1. Data consistency : Let's start with the most obvious case : In our previous single architecture , The database is shared by multiple services , So the business is ACID Of , But distributed , Each microservice has its own independent database , You have multiple services in a business , Then the atomicity of the microservice link under the distribution cannot be guaranteed ——> For example, below :

  Example : Create order , Deduct user balance , Deducting the inventory ( There are three services involved ), Call two services remotely in the order business , We create an order , Assume inventory is 10, We place an order 11 individual , We think that inventory service, order service and account service will be rolled back , Because the order exceeds the stock , But in fact, only the inventory service will rollback its own database , Other account services and order services will be successful ;

reason : Each service is independent , Have your own database , So transactions are also independent ;

2. Network issues need to be considered , Because the service invocation is very network dependent , In particular, there are many nodes , When the link is very long

3. asynchronous : Various middleware are introduced ,GateWay,mq,nacos,seata wait , Asynchronous communication increases the complexity of function implementation ;

4. O & M costs : A system is broken down into multiple services , Each service has to be configured , Deploy ;

In general , It is to divide the original system into multiple services that call and communicate with each other ;

scene : A business spans multiple services or data sources , Each service is a branch transaction , Each service corresponds to a database , Equivalent to a node ;

The difference between microservices and distributed

 (26 Bar message ) What is the difference between microservices and distributed ?_BUG Log machine blog -CSDN Blog _ The difference between distributed and micro services

CAP Theorem

CAP Guide the direction of distributed system transactions :

Partition : Because of network failure, some nodes in the distribution lose connection with other nodes , So as to form an independent partition ——> There must be ; 

  solve :

Give Way node03 The node waits for the network to recover , Access is not allowed until recovery ;——> Consistency is satisfied , Not satisfied with availability

Because in Distributed Systems , Our service nodes must be connected through the network , Then there must be partition problems , After all, your network is not guaranteed to be 100% good ;

  reflection :

  because es colony , When a node goes down , After a certain time it will be eliminated , Then we won't be able to access it , At the expense of usability , But the data on the node will be distributed to other nodes , This ensures consistency , So it is CP;

  Let's review es colony :

(25 Bar message ) elasticsearch Set up the cluster _Fairy want carry The blog of -CSDN Blog

Suddenly it occurred to me EureKa and Nacos The difference in also involves AP、CP

difference :

Nacos Support the server to actively detect the status of service providers : The temporary instance adopts heartbeat mode , The non temporary instance adopts the server active access mode ;

The temporary instance will be kicked out if its heartbeat is abnormal , Non temporary instances will not be rejected ;

Nacos Support service list message push mode , Instant update ;

Add (6/1)nacos、EureKa involve ap、cp Pattern

(25 Bar message ) CP and AP Yes what is it ? What's the difference? ?_Wayyyyyyyy The blog of -CSDN Blog _ap and cp The difference between


BASE theory

  Where the bull is strong : Part of the loss is available (A), Temporary inconsistency ——> Final consistency (C), Balance

 

  A transaction coordinator is needed to feel the transactions of each subsystem , Ensure that the transaction status of each subsystem is consistent  

  summary :

 Seata

Purpose : Solve the transaction problem of distributed system

technological process :

1. First TC It is to maintain and coordinate the overall affairs , Help transactions commit and rollback , It is equivalent to a general hodgepodge of distributed transactions ——>2. As a distributed system , There is an entrance , Because of the calls between our microservices , Every time a service is called, a transaction occurs , That is to say, our portal manages a range of service calls , So TM The transaction manager acts as an entry point for global transactions , The transaction scope is defined , Then start the global transaction ——>3.TC, It will determine whether our global service is committed and rolled back ——>4. But before that , Because there are branch transactions in the global transaction , The branch transaction is committed with RM of , Manage branch transactions , towards TC Conduct registration transactions , And execute the corresponding sql, Then report the status to TC( however RM At this point, there is no rollback or commit effect , To put it bluntly, the service is just executed , But did not submit )——>5.TC It will verify the service status to determine whether to commit or rollback , from TM Handle ;

summary :TM and RM It is related to business , Managing services , and TC It's independent , Maintaining branch service status ;

seata Deploy

  Creating a new configuration + Configure the content

#  Data storage mode ,db On behalf of the database 
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&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123
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
#  Business 、 Log and other configurations 
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000

#  Transmission mode between client and server 
transport.serialization=seata
transport.compressor=none
#  close metrics function , Improve performance 
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
```

  The above database for our data storage is seata, We also need to define a global transaction table and a branch transaction table


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Branch transaction table 
-- ----------------------------
DROP TABLE IF EXISTS `branch_table`;
CREATE TABLE `branch_table`  (
  `branch_id` BIGINT(20) NOT NULL,
  `xid` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `transaction_id` BIGINT(20) NULL DEFAULT NULL,
  `resource_group_id` VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `resource_id` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `branch_type` VARCHAR(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `status` TINYINT(4) NULL DEFAULT NULL,
  `client_id` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `application_data` VARCHAR(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gmt_create` DATETIME NULL DEFAULT NULL,
  `gmt_modified` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`branch_id`) USING BTREE,
  INDEX `idx_xid`(`xid`) USING BTREE
) ENGINE = INNODB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
--  Global transaction table 
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table`  (
  `xid` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `transaction_id` BIGINT(20) NULL DEFAULT NULL,
  `status` TINYINT(4) NOT NULL,
  `application_id` VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `transaction_service_group` VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `transaction_name` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `timeout` INT(11) NULL DEFAULT NULL,
  `begin_time` BIGINT(20) NULL DEFAULT NULL,
  `application_data` VARCHAR(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gmt_create` DATETIME NULL DEFAULT NULL,
  `gmt_modified` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`xid`) USING BTREE,
  INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
  INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
) ENGINE = INNODB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

SET FOREIGN_KEY_CHECKS = 1;

start-up seata

 seata Deployment success

Purpose : Complete distributed transaction management , formation TM,RM, Proxy our distributed transactions ;

Microservice Integration seata

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <!-- Lower version ,1.3.0, Thus eliminate -->
        <exclusion>
            <artifactId>seata-spring-boot-starter</artifactId>
            <groupId>io.seata</groupId>
        </exclusion>
    </exclusions>
</dependency>
<!--seata starter  use 1.4.2 edition -->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>${seata.version}</version>
</dependency>

 seata-tc-server The service needs to be determined again nacos Search the registry : Service address +namespace The default value is + grouping group+application+ Transaction group + colony

  Transaction group : It is equivalent to managing micro services in distributed systems ( Order , stock ....), These services manage them , These large groups of management are transaction groups , We can get from this TC node

 

server:
  port: 8081
spring:
  application:
    name: storage-service
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///seata_demo?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
    username: Wuyuhang
    password: 2002514wyh11
  cloud:
    nacos:
      server-addr: localhost:8848
mybatis-plus:
  global-config:
    db-config:
      insert-strategy: not_null
      update-strategy: not_null
      id-type: auto
logging:
  level:
    org.springframework.cloud.alibaba.seata.web: debug
    cn.itcast: debug
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS

seata:
  registry:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: ""
      group: DEFAULT_GROUP
      application: seata-tc-server
      username: nacos
      password: nacos
      # Transaction group name 
  tx-service-group: seata-demo
  service:
    # Configure mapping relationships 
    vgroup-mapping:
      seata-demo: SH

Deploy seata After success , The print service on the console is registered to seata On

 

 

原网站

版权声明
本文为[Fairy wants carry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101952074403.html