-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathchapter18.html
642 lines (286 loc) · 26.2 KB
/
chapter18.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<title>11.服务端SDK · MPUSH开发文档</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.0">
<meta name="author" content="夜色">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-donate/plugin.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-navigator/plugin.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="chapter19.html" />
<link rel="prev" href="chapter16.md" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="输入并搜索" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
0.Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="chapter1.html">
<a href="chapter1.html">
1.系统架构
</a>
</li>
<li class="chapter " data-level="1.3" data-path="chapter2.html">
<a href="chapter2.html">
2.服务依赖关系
</a>
</li>
<li class="chapter " data-level="1.4" data-path="chapter3.html">
<a href="chapter3.html">
3.协议
</a>
</li>
<li class="chapter " data-level="1.5" data-path="chapter4.html">
<a href="chapter4.html">
4.主要流程
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="chapter6.html">
<a href="chapter6.html">
4.1.密钥交换方案
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="chapter7.html">
<a href="chapter7.html">
4.2.握手及快速重连
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="chapter8.html">
<a href="chapter8.html">
4.3.绑定用户及路由发布
</a>
</li>
<li class="chapter " data-level="1.5.4" data-path="chapter9.html">
<a href="chapter9.html">
4.4.解绑用户
</a>
</li>
<li class="chapter " data-level="1.5.5" data-path="chapter10.html">
<a href="chapter10.html">
4.5.消息推送
</a>
</li>
<li class="chapter " data-level="1.5.6" data-path="chapter11.html">
<a href="chapter11.html">
4.6.Http代理
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="chapter12.html">
<a href="chapter12.html">
5.服务部署
</a>
</li>
<li class="chapter " data-level="1.7" data-path="chapter13.html">
<a href="chapter13.html">
6.源码测试
</a>
</li>
<li class="chapter " data-level="1.8" data-path="chapter14.html">
<a href="chapter14.html">
7.配置详解
</a>
</li>
<li class="chapter " data-level="1.9" data-path="chapter17.html">
<a href="chapter17.html">
8.Alloc实现
</a>
</li>
<li class="chapter " data-level="1.10" data-path="chapter15.html">
<a href="chapter15.html">
9.Android SDK
</a>
<ul class="articles">
<li class="chapter " data-level="1.10.1" data-path="chapter15.1.html">
<a href="chapter15.1.html">
9.1.SDK系统架构
</a>
</li>
<li class="chapter " data-level="1.10.2" data-path="chapter15.2.html">
<a href="chapter15.2.html">
9.2.MpushClient架构
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.11" data-path="chapter16.md">
<span>
10.IOS SDK
</a>
</li>
<li class="chapter active" data-level="1.12" data-path="chapter18.html">
<a href="chapter18.html">
11.服务端SDK
</a>
</li>
<li class="chapter " data-level="1.13" data-path="chapter19.html">
<a href="chapter19.html">
12.HTTP代理
</a>
</li>
<li class="chapter " data-level="1.14" data-path="chapter20.html">
<a href="chapter20.html">
13.Server源码分析
</a>
<ul class="articles">
<li class="chapter " data-level="1.14.1" data-path="chapter20.1.html">
<a href="chapter20.1.html">
13.1.模块依赖关系
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.15" data-path="chapter21.html">
<a href="chapter21.html">
14.SPI定制化
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 发布
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >11.服务端SDK</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="服务端SDK">服务端SDK</h1><h2 id="使用方式">1. 使用方式</h2><ol><li><p>添加maven依赖</p><pre><code class="lang-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>dependency</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>groupId</span><span class="token punctuation">></span></span>com.github.mpusher<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>groupId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>artifactId</span><span class="token punctuation">></span></span>mpush-client<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>artifactId</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>version</span><span class="token punctuation">></span></span>0.0.2<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>dependency</span><span class="token punctuation">></span></span></code></pre>
<p>​</p></li>
<li><p>在工程<code>resources</code>目录增加配置文件<code>application.conf</code>,并配置<code>zookeeper</code></p><pre><code class="lang-java"> mp<span class="token punctuation">.</span>zk<span class="token punctuation">.</span>namespace<span class="token operator">=</span>mpush
mp<span class="token punctuation">.</span>zk<span class="token punctuation">.</span>server<span class="token operator">-</span>address<span class="token operator">=</span><span class="token string">"127.0.0.1:2181"</span></code></pre>
<p>​</p></li>
<li><p>使用<code>com.mpush.api.push.PushSende.java</code>进行推送,使用其<code>create</code>方法创建服务,<code>start</code>方法启动服务,<code>stop</code>方法停止服务。推送接口定义如下:</p><pre><code class="lang-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">PushSender</span> <span class="token keyword">extends</span> <span class="token class-name">Service</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 创建PushSender实例
*
* @return PushSender
*/</span>
<span class="token keyword">static</span> PushSender <span class="token function">create<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> SpiLoader<span class="token punctuation">.</span><span class="token function">load<span class="token punctuation">(</span></span>PusherFactory<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">/**
* 推送push消息
*
* @param context 推送参数
* @return FutureTask 可用于同步调用
*/</span>
FutureTask<span class="token operator"><</span>Boolean<span class="token operator">></span> <span class="token function">send<span class="token punctuation">(</span></span>PushContext context<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<p>​</p></li></ol>
<h2 id="推送流程">2. 推送流程</h2><h3 id="流程图">2.1. 流程图</h3><p><img src="MPush接消息推送.png"></p><h3 id="流程分析">2.2. 流程分析</h3><ol><li><code>PushSender</code>启动后首先从ZK里获取可用的<code>GatewayServer</code>列表,然后创建相应的<code>Client</code>分别连接到对应的<code>GatewayServer</code>
</li>
<li>当调用<code>send</code>方法去推送时,<code>PushSender</code>首先会通过<code>RemoteRouterManager</code>查询要推送的用户当前所登录的机器IP,然后通过IP选择<code>GatewayServer</code>并通过第1中对应的<code>Client</code>把消息发送到该机器,因为该机器拥有用户的链接。
</li>
<li><code>GatewayServer</code>接收到<code>Client</code>发送过来的消息后,首先通过查询本地路由<code>LocalRouterManager</code>找到用户连接<code>Connection</code>,该链接是连接到<code>ConnectionServer</code>的。
</li>
<li>如果连接存在,<code>ConnectionServer</code>会通过此连接把消息下发到客户端。
</li>
<li>如果推送成功,<code>GatewayServer</code>会发送消息推送成功的消息给<code>PushSender</code>所持有的<code>Client</code>
</li>
<li><code>PushSender</code>收到推送成功消息后,会通过<code>Callback#onSuccess</code>回调调用方,整个推送流程结束。
</li>
<li>如果中间有任何失败则回调<code>Callback#onFailure</code>。
</li>
<li>如果用户不在线则回调<code>Callback#onOffline</code>。
</li>
<li>如果在一定时间内<code>PushSender</code>没有收到<code>GatewayServer</code>响应的消息则推送超时,回调<code>Callback#onTimeout</code>通知调用方。
</li></ol>
<h2 id="源码解读">3. 源码解读</h2><ol><li><p><code>PushSender</code>的实现类为<code>com.mpush.client.push.PushClient.java</code></p></li>
<li><p><code>PushClient</code>使用的是<code>ConnectionRouterManager</code>该类继承自<code>RemoteRouterManager</code>增加了本地缓存可在消息频繁时减轻<code>Redis</code>压力,但会存在一定情况的误判。</p></li>
<li><p><code>com.mpush.client.push.PushRequestBus.java</code>用于维持异步推送任务,线程的调整可通过配置设置,任务的拒绝策略为<strong>在调用线程执行Callback</strong>。具体见<code>DefaultThreadPoolFactory.java</code>。线程池默认配置如下:</p><pre><code class="lang-java">mp<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>pool<span class="token punctuation">.</span>push<span class="token operator">-</span>callback<span class="token operator">=</span><span class="token punctuation">{</span>
min<span class="token operator">:</span><span class="token number">2</span><span class="token comment" spellcheck="true">//核心线程数</span>
max<span class="token operator">:</span><span class="token number">2</span><span class="token comment" spellcheck="true">//最大线程数</span>
queue<span class="token operator">-</span>size<span class="token operator">:</span><span class="token number">0</span><span class="token comment" spellcheck="true">//队列大小</span>
<span class="token punctuation">}</span></code></pre>
<p>​</p></li>
<li><p><code>GatewayClient</code>会根据<code>GatewayServer</code>的运行状态自行调整,如果有<code>GatewayServer</code>宕机对应的<code>Client</code>会及时销毁,如果有新的机器进来,对应<code>Client</code>也会自动创建。具体参见<code>GatewayClientFactory.java</code>。</p><p>​</p></li></ol>
<footer class="page-footer"><span class="copyright">Copyright © ohun.com 2016 all right reserved,powered by Gitbook</span><span class="footer-modification">该文件修订时间:
2016-09-23 19:44:41
</span></footer>
<div id="anchors-navbar"><i class="fa fa-anchor"></i><ul><p><a href="#服务端SDK">服务端SDK</a></p><li><a href="#使用方式">1. 使用方式</a></li><li><a href="#推送流程">2. 推送流程</a></li><ul><li><a href="#流程图">2.1. 流程图</a></li><li><a href="#流程分析">2.2. 流程分析</a></li></ul><li><a href="#源码解读">3. 源码解读</a></li></ul></div><a href="#使用方式" id="goTop"><i class="fa fa-arrow-up"></i></a>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="chapter16.md" class="navigation navigation-prev " aria-label="Previous page: 10.IOS SDK">
<i class="fa fa-angle-left"></i>
</a>
<a href="chapter19.html" class="navigation navigation-next " aria-label="Next page: 12.HTTP代理">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"11.服务端SDK","level":"1.12","depth":1,"next":{"title":"12.HTTP代理","level":"1.13","depth":1,"path":"chapter19.md","ref":"chapter19.md","articles":[]},"previous":{"title":"10.IOS SDK","level":"1.11","depth":1,"path":"chapter16.md","ref":"chapter16.md","articles":[]},"dir":"ltr"},"config":{"plugins":["prism","sharing","search","-highlight","-page-treeview","-anchorjs","baidu","donate","tbfed-pagefooter","navigator","duoshuo","editlink","splitter"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright © ohun.com 2016","modify_label":"该文件修订时间:","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{},"editlink":{"label":"编辑本页","multilingual":false,"base":"https://github.com/mpusher/docs/tree/book"},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"duoshuo":{"short_name":"mpush","theme":"default"},"donate":{"alipay":"alipay.jpg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"wxpay.png","wechatText":"微信打赏"},"fontsettings":{"theme":"sepia","family":"sans","size":2},"navigator":{},"baidu":{"token":"74e8d02bb383b3d88072165c31989fb2"},"sharing":{"all":["facebook","google","twitter","weibo","instapaper"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchorjs":{"icon":"#","placement":"right","visible":"always","class":"custom-class another-one"}},"theme":"default","author":"夜色","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MPUSH开发文档","language":"zh-hans","gitbook":"*","description":"mpush开发手册"},"file":{"path":"chapter18.md","mtime":"2016-09-23T11:44:41.000Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2016-10-26T16:14:58.733Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-baidu/plugin.js"></script>
<script src="gitbook/gitbook-plugin-donate/plugin.js"></script>
<script src="gitbook/gitbook-plugin-duoshuo/duoshuo.js"></script>
<script src="gitbook/gitbook-plugin-editlink/plugin.js"></script>
<script src="gitbook/gitbook-plugin-splitter/splitter.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>