当前位置:网站首页>Theoretical basis of distributed services
Theoretical basis of distributed services
2022-06-10 21:04:00 【Fairy wants carry】
Catalog
The difference between microservices and distributed
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
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
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: SHDeploy seata After success , The print service on the console is registered to seata On

边栏推荐
- node(express)实现增删改查、分页等接口
- Canvas advanced functions (medium)
- Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security
- Heap sorting and hardening heap code for memory
- 暗黑破坏神不朽数据库怎么用 暗黑破坏神手游不朽数据库使用方法
- 获取列表中最大最小值的前n个数值的位置索引的四种方法
- Mysql database foundation
- Stacked bar graph move the mouse into the tooltip to prompt that the filter is 0 element, so as to realize custom bubbles
- 游戏兼容性测试(通用方案)
- P5723 【深基4.例13】质数口袋
猜你喜欢

CET-6 - Business English - the last recitation before the test

Software definition boundary (SDP)

Knowledge map / relationship visualization

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0

pytorch深度学习——神经网络卷积层Conv2d

连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors

Construction of RT thread smart win10 64 bit compilation environment

Tutoriel Microsoft Word "5", comment changer les marges de page et créer une barre de nouvelles en word?

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
随机推荐
Vertical bar of latex tips absolute value \left\right|
Power consumption development experience sharing: design power consumption board
聊聊服务器性能优化~(建议收藏)
Recommend a crud tool that may become the best crud tool in 2019 during the National Day
Magic tower game implementation source code and level generation
Self attention and multi head attention
canvas 高级功能(上)
leetcode 划分数组使最大差为 K
Talk about server performance optimization ~ (recommended Collection)
[computer use] how to set software startup without auto startup
保姆级教程:如何成为Apache Linkis文档贡献者
LeetCode:1037. Effective boomerang - simple
The new audio infinix machine appears in the Google product library, and Tecno CaMon 19 is pre installed with Android 13
mixin--混入
pdf.js-----js解析pdf文件實現預覽,並獲取pdf文件中的內容(數組形式)
中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
MySQL ---- 常用函数
View play and earn will lead crypto games astray
京东发布基于张量网络加速的大规模、分布式量子机器学习平台TeD-Q
Microsoft Word 教程「5」,如何在 Word 中更改页边距、创建新闻稿栏?