-
Notifications
You must be signed in to change notification settings - Fork 2
/
grpc_balancer.html
172 lines (170 loc) · 14.2 KB
/
grpc_balancer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-01-23 Sat 18:03 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>GRPC golang版源码分析之客户端(二)</title>
<meta name="generator" content="Org mode" />
<link rel="stylesheet" type="text/css" href="./css/org-css.css"/>
<link rel="stylesheet" type="text/css" href="./css/gitalk.css"/> <script src="./js/gitalk.min.js"></script>
</head>
<body>
<div id="org-div-home-and-up">
<a accesskey="h" href="index.html"> UP </a>
|
<a accesskey="H" href="index.html"> HOME </a>
</div><div id="content">
<h1 class="title">GRPC golang版源码分析之客户端(二)</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org5288d76">1. 前言</a></li>
<li><a href="#orge36d607">2. 负载均衡</a></li>
<li><a href="#org3270768">3. 相关链接</a></li>
</ul>
</div>
</div>
<div id="outline-container-org5288d76" class="outline-2">
<h2 id="org5288d76"><span class="section-number-2">1</span> 前言</h2>
<div class="outline-text-2" id="text-1">
<p>
前面一篇文章分析了一个grpc call的大致调用流程,顺着源码走了一遍,但是grpc中有一些特性并没有进行分析,这篇blog主要就是分析负载均衡这个特性。负载均衡可以让你在使用grpc调用方法是连接不同的服务器。在启动的时候,grpc client会建立对所有服务器的连接,然后通过轮询算法拿不同的服务器。
</p>
</div>
</div>
<div id="outline-container-orge36d607" class="outline-2">
<h2 id="orge36d607"><span class="section-number-2">2</span> 负载均衡</h2>
<div class="outline-text-2" id="text-2">
<p>
负载均衡是在客户端实现的,通过在初始化一个grpc客户端传入一个负载均衡器,默认是通过轮询算法每次调用时切换调用的服务器,由于是在客户端做的,所以只能让服务器平摊压力,并不能实时的根据服务器的状态来进行负载均衡。比如某一刻所有的grpc客户端都轮到了同一个服务器(这里只是举例)。
</p>
<div class="org-src-container">
<pre class="src src-go"><span style="color: #676E95;">//</span><span style="color: #676E95;">在clientconn.go的DialContext函数中</span>
<span style="color: #89DDFF;">go</span> <span style="color: #89DDFF;">func</span>() {
<span style="color: #89DDFF;">var</span> <span style="color: #ffcb6b;">addrs</span> []<span style="color: #c792ea;">Address</span>
<span style="color: #89DDFF;">if</span> cc.dopts.balancer == <span style="color: #f78c6c;">nil</span> {
<span style="color: #676E95;">// </span><span style="color: #676E95;">Connect to target directly if balancer is nil.</span>
<span style="color: #676E95;">//</span><span style="color: #676E95;">如果没有设置负载均衡器,则直接连接</span>
addrs = <span style="color: #82aaff;">append</span>(addrs, <span style="color: #c792ea;">Address</span>{<span style="color: #f78c6c;">Addr</span>: target})
} <span style="color: #89DDFF;">else</span> {
<span style="color: #89DDFF;">var</span> <span style="color: #ffcb6b;">credsClone</span> <span style="color: #c792ea;">credentials.TransportCredentials</span>
<span style="color: #89DDFF;">if</span> creds != <span style="color: #f78c6c;">nil</span> {
credsClone = creds.<span style="color: #82aaff;">Clone</span>()
}
<span style="color: #ffcb6b;">config</span> := <span style="color: #c792ea;">BalancerConfig</span>{
<span style="color: #f78c6c;">DialCreds</span>: credsClone,
}
<span style="color: #676E95;">//</span><span style="color: #676E95;">启动一个负载均衡器,start函数会启动一个watch监听地址的变化,而Notify返回一个通道,在每次服务器地址变化后的最新地址信息.</span>
<span style="color: #89DDFF;">if</span> <span style="color: #ffcb6b;">err</span> := cc.dopts.balancer.<span style="color: #82aaff;">Start</span>(target, config); err != <span style="color: #f78c6c;">nil</span> {
waitC <- err
<span style="color: #89DDFF;">return</span>
}
<span style="color: #ffcb6b;">ch</span> := cc.dopts.balancer.<span style="color: #82aaff;">Notify</span>()
<span style="color: #89DDFF;">if</span> ch == <span style="color: #f78c6c;">nil</span> {
<span style="color: #676E95;">// </span><span style="color: #676E95;">There is no name resolver installed.</span>
addrs = <span style="color: #82aaff;">append</span>(addrs, <span style="color: #c792ea;">Address</span>{<span style="color: #f78c6c;">Addr</span>: target})
} <span style="color: #89DDFF;">else</span> {
addrs, ok = <-ch
<span style="color: #89DDFF;">if</span> <span style="color: #89DDFF; font-weight: bold;">!</span>ok || <span style="color: #82aaff;">len</span>(addrs) == 0 {
waitC <- errNoAddr
<span style="color: #89DDFF;">return</span>
}
}
}
<span style="color: #676E95;">//</span><span style="color: #676E95;">对每一个地址进行连接, 因为这是客户端启动时,所以需要对所有地址操作.</span>
<span style="color: #89DDFF;">for</span> <span style="color: #ffcb6b;">_</span>, <span style="color: #ffcb6b;">a</span> := <span style="color: #89DDFF;">range</span> addrs {
<span style="color: #89DDFF;">if</span> <span style="color: #ffcb6b;">err</span> := cc.<span style="color: #82aaff;">resetAddrConn</span>(a, <span style="color: #f78c6c;">false</span>, <span style="color: #f78c6c;">nil</span>); err != <span style="color: #f78c6c;">nil</span> {
waitC <- err
<span style="color: #89DDFF;">return</span>
}
}
<span style="color: #82aaff;">close</span>(waitC)
}()
..............
<span style="color: #676E95;">//</span><span style="color: #676E95;">省略一些代码</span>
<span style="color: #89DDFF;">if</span> ok {
<span style="color: #676E95;">//</span><span style="color: #676E95;">这里开启一个监听goroutine,主要是监听服务器地址变化并对新的地址建立连接,对老的地址关闭连接</span>
<span style="color: #89DDFF;">go</span> cc.<span style="color: #82aaff;">lbWatcher</span>()
}
</pre>
</div>
<p>
上面的代码大致是说明了一个负载均衡器是怎么使用的,下面看看负载均衡具体的结构:
</p>
<div class="org-src-container">
<pre class="src src-go"><span style="color: #89DDFF;">type</span> <span style="color: #c792ea;">Balancer</span> <span style="color: #89DDFF;">interface</span> {
<span style="color: #676E95;">//</span><span style="color: #676E95;">启动一个负载均衡,内部会启动一个名称服务器的watcher,不断监听地址的变化</span>
<span style="color: #82aaff;">Start</span>(<span style="color: #ffcb6b;">target</span> <span style="color: #c792ea;">string</span>, <span style="color: #ffcb6b;">config</span> <span style="color: #c792ea;">BalancerConfig</span>) <span style="color: #c792ea;">error</span>
<span style="color: #82aaff;">Up</span>(<span style="color: #ffcb6b;">addr</span> <span style="color: #c792ea;">Address</span>) (<span style="color: #ffcb6b;">down</span> <span style="color: #89DDFF;">func</span>(<span style="color: #c792ea;">error</span>))
<span style="color: #676E95;">//</span><span style="color: #676E95;">得到下一次连接的地址</span>
<span style="color: #82aaff;">Get</span>(<span style="color: #ffcb6b;">ctx</span> <span style="color: #c792ea;">context.Context</span>, <span style="color: #ffcb6b;">opts</span> <span style="color: #c792ea;">BalancerGetOptions</span>) (<span style="color: #ffcb6b;">addr</span> <span style="color: #c792ea;">Address</span>, <span style="color: #ffcb6b;">put</span> <span style="color: #89DDFF;">func</span>(), <span style="color: #ffcb6b;">err</span> <span style="color: #c792ea;">error</span>)
<span style="color: #676E95;">//</span><span style="color: #676E95;">从服务器得到更新的地址,这个地址是更新后的所有地址</span>
<span style="color: #82aaff;">Notify</span>() <-<span style="color: #89DDFF;">chan</span> []<span style="color: #c792ea;">Address</span>
<span style="color: #676E95;">//</span><span style="color: #676E95;">关闭负载均衡器</span>
<span style="color: #82aaff;">Close</span>() <span style="color: #c792ea;">error</span>
}
</pre>
</div>
<p>
负载均衡器有个默认实现在balancer.go中,使用的轮询算法。这个实现中包含了一个名字服务器的服务,我们可以通过实现一个名字服务器的接口,然后封装到这个负载均衡器中,这样就不需要自己实现整个负载均衡器。名字服务器的接口如下:
</p>
<div class="org-src-container">
<pre class="src src-go"><span style="color: #89DDFF;">type</span> <span style="color: #c792ea;">Update</span> <span style="color: #89DDFF;">struct</span> {
<span style="color: #676E95;">// </span><span style="color: #676E95;">Op indicates the operation of the update.</span>
Op <span style="color: #c792ea;">Operation</span>
<span style="color: #676E95;">// </span><span style="color: #676E95;">Addr is the updated address. It is empty string if there is no address update.</span>
Addr <span style="color: #c792ea;">string</span>
<span style="color: #676E95;">// </span><span style="color: #676E95;">Metadata is the updated metadata. It is nil if there is no metadata update.</span>
<span style="color: #676E95;">// </span><span style="color: #676E95;">Metadata is not required for a custom naming implementation.</span>
Metadata <span style="color: #89DDFF;">interface</span>{}
}
<span style="color: #676E95;">// </span><span style="color: #676E95;">Resolver creates a Watcher for a target to track its resolution changes.</span>
<span style="color: #89DDFF;">type</span> <span style="color: #c792ea;">Resolver</span> <span style="color: #89DDFF;">interface</span> {
<span style="color: #676E95;">// </span><span style="color: #676E95;">Resolve creates a Watcher for target.</span>
<span style="color: #676E95;">//</span><span style="color: #676E95;">通过一个名字得到一个watcher,监听服务器地址变化。</span>
<span style="color: #82aaff;">Resolve</span>(<span style="color: #ffcb6b;">target</span> <span style="color: #c792ea;">string</span>) (<span style="color: #c792ea;">Watcher</span>, <span style="color: #c792ea;">error</span>)
}
<span style="color: #676E95;">// </span><span style="color: #676E95;">Watcher watches for the updates on the specified target.</span>
<span style="color: #89DDFF;">type</span> <span style="color: #c792ea;">Watcher</span> <span style="color: #89DDFF;">interface</span> {
<span style="color: #676E95;">// </span><span style="color: #676E95;">Next blocks until an update or error happens. It may return one or more</span>
<span style="color: #676E95;">// </span><span style="color: #676E95;">updates. The first call should get the full set of the results. It should</span>
<span style="color: #676E95;">// </span><span style="color: #676E95;">return an error if and only if Watcher cannot recover.</span>
<span style="color: #676E95;">// </span><span style="color: #676E95;">得到下次更新的地址</span>
<span style="color: #82aaff;">Next</span>() ([]*<span style="color: #c792ea;">Update</span>, <span style="color: #c792ea;">error</span>)
<span style="color: #676E95;">// </span><span style="color: #676E95;">Close closes the Watcher.</span>
<span style="color: #82aaff;">Close</span>()
}
</pre>
</div>
<p>
负载均衡器的全貌大概就这些了,其中还有些细节我没有看到,目前来说我也不希望过早深入这些细节。所以负载均衡就到这里啦。
</p>
</div>
</div>
<div id="outline-container-org3270768" class="outline-2">
<h2 id="org3270768"><span class="section-number-2">3</span> 相关链接</h2>
<div class="outline-text-2" id="text-3">
<ol class="org-ol">
<li><a href="http://www.grpc.io/docs/guides/auth.html#overview">http://www.grpc.io/docs/guides/auth.html#overview</a> 验证文档</li>
</ol>
</div>
</div>
</div>
<div id="postamble" class="status">
<div id="gitalk" /> <script> var gitalk = new Gitalk({
clientID: 'f30e66bb5ab9089aa742',
clientSecret: '5d256d445447bd4db16540c2aab0e0884218ed12',
repo: 'guidao.github.io',
owner: 'guidao',
admin: ['guidao'],
id: location.pathname, // Ensure uniqueness and length less than 50
distractionFreeMode: false // Facebook-like distraction free mode
})
gitalk.render('gitalk') </script>
</div>
</body>
</html>