当前位置:网站首页>Microservice automation
Microservice automation
2022-06-12 00:06:00 【The third kind of people I】
etcd Introduce
etcd Is a highly consistent distributed key value (key-value) Storage , It provides a reliable way to store data that needs to be accessed by distributed systems or machine clusters . It can gracefully handle the leader election during network partition , Even in Machine failures can also be tolerated in the leader node .
etcd Yes, it is Go language-written , It has excellent cross platform support , Small binaries and powerful communities .etcd Communication between machines is through Raft Consensus algorithm processing .
Service discovery
Service discovery is also one of the most common problems in distributed systems , That is, processes or services in the same distributed cluster , How to find each other and establish a connection
Service discovery is to find out whether there are processes listening in the cluster udp or tcp port , And through the name you can find and connect .
To solve the problem of service discovery , We need to have three pillars , Be short of one cannot :
A strong consistency 、 Highly available service storage directory . be based on Raft Algorithm etcd By nature, it is such a highly consistent and available service storage directory .
A mechanism for registering services and monitoring the health status of services . Users can go to etcd Registration service in , And set the registered service key TTL, Keep the heartbeat of the service regularly to achieve the effect of monitoring the health status .
A mechanism for finding and connecting services . By means of etcd The services registered under the specified topic can also be found under the corresponding topic . To make sure the connection , We can deploy one... On every service machine Proxy Mode etcd, That way To ensure access etcd The services of the cluster can be connected to each other .
Raft Election algorithm
A cluster is usually composed of two or more servers , Each server is a node . Database cluster 、 Management cluster ... The database cluster provides read and write functions , The management cluster provides management 、 Fault recovery and other functions .
For a cluster , The coordination and management of multiple nodes is very important . The master node realizes collaboration and management , The existence of the master node , It can ensure the orderly operation of other nodes , And the write data in the database cluster on each node The consistency of .
Consistency here means , The data is the same in each cluster node , There is no difference . The function of distributed election is to select a master node , It coordinates and manages other nodes , To ensure the orderly operation of the cluster and the exchange of data between nodes Uniformity .
Raft The algorithm is a typical majority voting algorithm , Its election mechanism is similar to the democratic voting mechanism in our daily life , The core idea is “ The minority is subordinate to the majority ”
Raft In the algorithm, , The node that gets the most votes becomes the master node .
use Raft Algorithmic election , The roles of cluster nodes are 3 Kind of :
Leader: The primary node , There is only one... At the same time Leader, Responsible for coordinating and managing other nodes ;
Candidate: The candidate , Every node can be Candidate, Nodes can only be selected as new under this role Leader;
Follower: Leader Follower , You can't have an election .
Raft Election process , It can be divided into the following steps :
1. On initialization , All nodes are Follower state .
2. When you start the election , The state of all nodes is determined by Follower Turn into Candidate, And send election requests to other nodes .
3. Other nodes according to the order of election requests received , Reply whether you agree to become the master . What needs to be noted here is , In every election round , A node can only cast one vote .
4. If the node initiating the election request gets more than half of the votes , Then it becomes the master node , Its state is transformed into Leader, The state of other nodes is determined by Candidate Reduced to Follower.Leader Node and Follower Nodes send messages regularly Heartbeat bag , To detect whether the master node is alive .
5. When Leader The term of office of the node has arrived , That is to say, when other servers start the next round of main selection cycle ,Leader The state of a node is determined by Leader Downgrade to Follower, Enter a new round of election

etcd The term :
Raft:etcd The algorithm used to ensure the strong consistency of the distributed system .
Node: One Raft State machine instance .
Member: One etcd example . It manages a Node, And can provide services for client requests .
Cluster: By multiple Member To form something that can work together etcd colony .
Peer: To the same etcd Another one in the cluster Member Address of .
Client: towards etcd Cluster send HTTP Requested clients .
WAL: Pre written logs ,etcd Log format for persistent storage .
snapshot:etcd prevent WAL Snapshot set due to too many files , Storage etcd Data status .
Proxy:etcd A model of , by etcd The cluster provides reverse proxy services .
Leader( The leader ):Raft The nodes in the algorithm that process all the data submitted through the election .
Follower( follower ): The node of the election defeat is Raft Slave nodes in , Provide strong consistency guarantee for algorithm .
Candidate: When Follower Can't receive... After a certain period of time Leader My heart rate changes to Candidate Start Leader campaign for .
Term: A node becomes Leader The time period until the beginning of the next election , Is called a Term.
Index: Data item number .Raft Pass through Term and Index To locate data .
etcd install (centos)
Upload etcd Installation package ( The Internet download is too slow )
tar -zxvf etcd-v3.4.3-linux-amd64.tar.gz
Switch to etcd root directory , take etcd and etcdctl Binary files are copied to /usr/local/bin Directory so that the system can directly call etcd/etcdctl These two programs
cp etcd etcdctl /usr/local/bin
![]()
see etcd edition
etcd --version
Enter the command etcd, You can start a single node etcd service ,ctrl+c You can stop the service

xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: name = default
xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: data dir = default.etcd
xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: member dir = default.etcd/member
xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: heartbeat = 100ms
xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: election = 1000ms
xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: snapshot count = 100000
xxxx-xx-xx xx:xx:xx.xxxxxx I | embed: advertise client URLs = http://localhost:2379
xxxx-xx-xx xx:xx:xx.xxxxxx I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
1.name Represents the node name , The default is default.
2.data-dir The directory where logs and snapshots are kept , The default is the current working directory default.etcd/ Under the table of contents .
3. stay http://localhost:2380 Communicating with other nodes in the cluster .
4. stay http://localhost:2379 Provide client interaction .
5.heartbeat by 100ms, The function of this parameter is leader How often does it send a heartbeat to followers, The default value is 100ms.
6.election by 1000ms, The function of this parameter is the timeout for re voting , If follow No heartbeat packets were received at this time interval , Will trigger a new vote , The default is 1000ms.
7.snapshot count by 10000, This parameter is used to specify how many transactions are committed , Trigger snapshot capture to save to disk .
8. The cluster and each node will generate a uuid.
9. When it starts, it will run raft, Election out leaderCreate a etcd service
establish etcd Related contents ( That is, the storage location of data files and configuration files )
mkdir -p /var/lib/etcd/ && mkdir -p /etc/etcd/
![]()
establish etcd The configuration file
Stand-alone version conf
# The name of the node
ETCD_NAME="etcd0"
# Specify the data file storage location
ETCD_DATA_DIR="/var/lib/etcd/"
vim /etc/etcd/etcd.conf
![]()
establish systemd The configuration file
Stand-alone version service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target[Service]
User=root
Type=notify
WorkingDirectory=/var/lib/etcd/
## Modify... According to the actual situation EnvironmentFile and ExecStart These two parameter values
## 1.EnvironmentFile That is, the location of the configuration file , Be careful “-” It can't be less
EnvironmentFile=-/etc/etcd/etcd.conf
## 2.ExecStart namely etcd Start program location
ExecStart=/usr/local/bin/etcd
Restart=on-failure
LimitNOFILE=65536[Install]
WantedBy=multi-user.target
vim /etc/systemd/system/etcd.service
![]()
start-up / stop it / see etcd service
systemctl daemon-reload && systemctl enable etcd && systemctl start etcd && systemctl status etcd
![]()
etcd Basic use
etcdctl Is a command line client , It can be done to etcd Test the service or manually modify the database content . in addition ,etcdctl And support HTTP API( Later on ).
etcdctl The supported commands are generally divided into database operation and non database operation
Help order
etcdctl -h
put Put in the data
etcdctl put /testdir/testkey "Hello world"
![]()
--sort Sort results
--consistent Send the request to the master , Ensure consistency of acquired content .
del Delete data
Empty data
etcdctl del / --prefix
![]()
Delete all /test Prefix node
etcdctl del /test --prefix
watch, Monitoring the change of a key value , Once the key value is updated , Will output the latest value and exit
etcdctl watch key1
![]()
etcd Foundation and use
High availability cluster
边栏推荐
- (dp+ longest common subsequence) acwing 897 Longest common subsequence
- Do you want to take the postgraduate entrance examination? Will you be able to find a good job after graduate school?
- Cube technology interpretation | detailed explanation of cube applet Technology
- 730.Count Different Palindromic Subsequences
- Two configurations of data tables in efcore
- 【juc学习之路第6天】并发计算器与线程随机因子
- Handwritten simple promise
- Gin integrated graphic verification code
- Use select to switch coroutines
- (interval DP | dfs+ memory) acwing 282 Stone merging
猜你喜欢

平衡二叉树(AVL 树)
![[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties](/img/aa/821804e951e2fbb63c72f4e28756a1.jpg)
[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties

The road of global evolution of vivo global mall -- multilingual solution

Pycharm file name taboo

gin集成图形验证码
![[day 5 of JUC learning] reference atomic classes and attribute modifiers](/img/e5/3d39d34d8c423ec71da59f9b418f5a.png)
[day 5 of JUC learning] reference atomic classes and attribute modifiers

(linear DP | monotone queue) acwing 895 Longest ascending subsequence

Unified certification center oauth2 high availability pit

Jenkins basic configuration

sonarqube介紹和安裝步驟
随机推荐
[day 5 of JUC learning] reference atomic classes and attribute modifiers
SAP SD 创建/修改价格表
I2C read / write process
730.Count Different Palindromic Subsequences
Two configurations of data tables in efcore
require. context
[signals and systems] (XXII) Laplace transform and complex frequency domain analysis - s-domain analysis
In order to stimulate inspiration and creativity, Shanghai daoning united with XMIND to bring you full-featured mind mapping and brainstorming software
JS——防止自动恢复页面位置
[tense] 1. General present tense 2. Do not translate Chinese
Here we go! Dragon lizard community enters PKU classroom
Stm32f103c8t6 related knowledge
Acwing's first question solution attracted the first fan!!! Happy~~~
(dp+ longest common subsequence) acwing 897 Longest common subsequence
Detailed explanation of various objects in browser object
926. Flip String to Monotone Increasing
Lake Shore - supertran continuous flow cryogenic thermostat system
Achievements in science and Technology (XV)
[flume] notes
Teach you to play with SSM framework



