-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1858 lines (817 loc) · 151 KB
/
index.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="alternate" href="/atom.xml" title="LVPHP" type="application/atom+xml" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />
<meta property="og:type" content="website">
<meta property="og:title" content="LVPHP">
<meta property="og:url" content="http://www.lvphp.com/index.html">
<meta property="og:site_name" content="LVPHP">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="LVPHP">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Mist',
sidebar: {"position":"left","display":"post"},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://www.lvphp.com/"/>
<title> LVPHP </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">LVPHP</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about" rel="section">
<i class="menu-item-icon fa fa-fw fa-user"></i> <br />
关于
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://www.lvphp.com/2017/02/20/zookeeper/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="lvphpwb">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="LVPHP">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="LVPHP" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/02/20/zookeeper/" itemprop="url">
试试zookeeper
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-20T18:51:31+08:00">
2017-02-20
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2017/02/20/zookeeper/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/02/20/zookeeper/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h4><p>Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等</p>
<h4 id="安装和配置详解"><a href="#安装和配置详解" class="headerlink" title="安装和配置详解"></a>安装和配置详解</h4><p>Zookeeper的安装和配置十分简单, 既可以配置成单机模式, 也可以配置成集群模式<br>1.单机模式<br>到<a href="http://zookeeper.apache.org/releases.html" target="_blank" rel="external">官方网站</a>下载zookeeper的安装包之后, 解压到合适目录. 进入zookeeper目录下的conf子目录,复制一份zoo_sample.cfg为zoo.cfg,根据情况修改配置:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">tickTime=2000 </div><div class="line">dataDir=/home/apple/zookeeper/data </div><div class="line">dataLogDir=/home/apple/zookeeper/logs </div><div class="line">clientPort=4180</div></pre></td></tr></table></figure></p>
<p>参数说明: </p>
<ul>
<li>tickTime: zookeeper中使用的基本时间单位, 毫秒值.</li>
<li>dataDir: 数据目录. 可以是任意目录.</li>
<li>dataLogDir: log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和dataDir相同的设置.</li>
<li>clientPort: 监听client连接的端口号.<br>至此, zookeeper的单机模式已经配置好了. 启动server只需运行脚本:<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./bin/zkServer.sh start</div></pre></td></tr></table></figure>
</li>
</ul>
<p>Server启动之后, 就可以启动client连接server了, 执行脚本:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./bin/zkCli.sh -server localhost:4180</div></pre></td></tr></table></figure></p>
<p>2.伪集群模式<br>所谓伪集群, 是指在单台机器中启动多个zookeeper进程, 并组成一个集群. 以启动3个zookeeper进程为例.<br>将zookeeper的目录拷贝2份:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">|--zookeeper1</div><div class="line">|--zookeeper2</div><div class="line">|--zookeeper3</div></pre></td></tr></table></figure></p>
<p>更改各目录下的conf/zoo.cfg的配置,例如下:<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">tickTime=2000 </div><div class="line">initLimit=5 </div><div class="line">syncLimit=2 </div><div class="line">dataDir=/home/apple/zookeeper0/data </div><div class="line">dataLogDir=/home/apple/zookeeper0/logs </div><div class="line">clientPort=4180</div><div class="line">server.1=127.0.0.1:8880:7770 </div><div class="line">server.2=127.0.0.1:8881:7771 </div><div class="line">server.3=127.0.0.1:8882:7772</div></pre></td></tr></table></figure></p>
<p>新增了几个参数, 其含义如下: </p>
<ul>
<li>initLimit: zookeeper集群中的包含多台server, 其中一台为leader, 集群中其余的server为follower. initLimit参数配置初始化连接时, follower和leader之间的最长心跳时间. 此时该参数设置为5, 说明时间限制为5倍tickTime, 即5*2000=10000ms=10s.</li>
<li>syncLimit: 该参数配置leader和follower之间发送消息, 请求和应答的最大时间长度. 此时该参数设置为2, 说明时间限制为2倍tickTime, 即4000ms.</li>
<li>server.X=A:B:C 其中X是一个数字, 表示这是第几号server. A是该server所在的IP地址. B配置该server和集群中的leader交换消息所使用的端口. C配置选举leader时所使用的端口. 由于配置的是伪集群模式, 所以各个server的B, C参数必须不同.<br>在之前设置的dataDir中新建myid文件, 写入一个数字, 该数字表示这是第几号server. 该数字必须和zoo.cfg文件中的server.X中的X一一对应。<br>/home/apple/zookeeper1/data/myid文件中写入1, /home/apple/zookeeper2/data/myid文件中写入2, /home/apple/zookeeper3/data/myid文件中写入3.<br>3.集群模式<br>集群模式的配置和伪集群基本一致.由于集群模式下, 各server部署在不同的机器上, 因此各server的conf/zoo.cfg文件可以完全一样.<br>下面是一个示例:<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">tickTime=2000 </div><div class="line">initLimit=5 </div><div class="line">syncLimit=2 </div><div class="line">dataDir=/home/zookeeper/data </div><div class="line">dataLogDir=/home/zookeeper/logs </div><div class="line">clientPort=4180</div><div class="line">server.43=10.1.39.43:2888:3888</div><div class="line">server.47=10.1.39.47:2888:3888 </div><div class="line">server.48=10.1.39.48:2888:3888</div></pre></td></tr></table></figure>
</li>
</ul>
<p>示例中部署了3台zookeeper server, 分别部署在10.1.39.43, 10.1.39.47, 10.1.39.48上. 需要注意的是, 各server的dataDir目录下的myid文件中的数字必须不同.<br>10.1.39.43 server的myid为43, 10.1.39.47 server的myid为47, 10.1.39.48 server的myid为48.</p>
<h4 id="node客户端链接zookeeper"><a href="#node客户端链接zookeeper" class="headerlink" title="node客户端链接zookeeper"></a>node客户端链接zookeeper</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span>;</div><div class="line"><span class="keyword">const</span> zookeeper = <span class="built_in">require</span>(<span class="string">'node-zookeeper-client'</span>);</div><div class="line"><span class="keyword">const</span> client = zookeeper.createClient(<span class="string">"127.0.0.1:4180,127.0.0.1:4181,127.0.0.1:4182"</span>);</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPath</span>(<span class="params">path</span>)</span>{</div><div class="line"> client.create(</div><div class="line"> path,</div><div class="line"> <span class="keyword">new</span> Buffer(<span class="string">'aaaaaaaa'</span>),</div><div class="line"> zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL,</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">error, path</span>) </span>{</div><div class="line"> <span class="keyword">if</span> (error) {</div><div class="line"> <span class="built_in">console</span>.log(error.stack);</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Node: %s is created.'</span>, path);</div><div class="line"> }</div><div class="line"> );</div><div class="line">}</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">getData</span>(<span class="params">path</span>)</span>{</div><div class="line"> client.getData(</div><div class="line"> path,</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Got event: %s.'</span>, event);</div><div class="line"> getData(path);</div><div class="line"> },</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">error, data, stat</span>) </span>{</div><div class="line"> <span class="keyword">if</span> (error) {</div><div class="line"> <span class="built_in">console</span>.log(error.stack);</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Got data: %s'</span>, data.toString(<span class="string">'utf8'</span>));</div><div class="line"> }</div><div class="line"> );</div><div class="line">}</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">listChildren</span>(<span class="params">path</span>) </span>{</div><div class="line"> client.getChildren(</div><div class="line"> path,</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Got watcher event: %s'</span>, event.getPath());</div><div class="line"> listChildren(path);</div><div class="line"> },</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">error, children, stat</span>) </span>{</div><div class="line"> <span class="keyword">if</span> (error) {</div><div class="line"> <span class="built_in">console</span>.log(</div><div class="line"> <span class="string">'Failed to list children of %s due to: %s.'</span>,</div><div class="line"> path,</div><div class="line"> error</div><div class="line"> );</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Children of %s are: %j.'</span>, path, children);</div><div class="line"> }</div><div class="line"> );</div><div class="line">}</div><div class="line"></div><div class="line">client.once(<span class="string">'connected'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Connected to ZooKeeper.'</span>);</div><div class="line"> createPath(<span class="string">"/test/node4"</span>);</div><div class="line"> listChildren(<span class="string">"/test"</span>);</div><div class="line">});</div><div class="line"></div><div class="line">client.connect();</div></pre></td></tr></table></figure>
<h4 id="node-server使用zookeeper"><a href="#node-server使用zookeeper" class="headerlink" title="node server使用zookeeper"></a>node server使用zookeeper</h4><p><a href="https://github.com/terrywh/node-utility" target="_blank" rel="external">local_address</a><br><a href="https://github.com/alexguan/node-zookeeper-client" target="_blank" rel="external">node-zookeeper-client</a><br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span>;</div><div class="line"><span class="keyword">const</span> program = <span class="built_in">require</span>(<span class="string">"commander"</span>),</div><div class="line"> events = <span class="built_in">require</span>(<span class="string">'events'</span>),</div><div class="line"> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>),</div><div class="line"> util = <span class="built_in">require</span>(<span class="string">"util"</span>),</div><div class="line"> debug = <span class="built_in">require</span>(<span class="string">'debug'</span>)(<span class="string">"server"</span>),</div><div class="line"> local_address = <span class="built_in">require</span>(<span class="string">"./lib/local_address"</span>),</div><div class="line"> zookeeper = <span class="built_in">require</span>(<span class="string">'node-zookeeper-client'</span>),</div><div class="line"> net = <span class="built_in">require</span>(<span class="string">'net'</span>);</div><div class="line">program</div><div class="line"> .version(<span class="string">'0.0.1'</span>)</div><div class="line"> .usage(<span class="string">'[options] <file ...>'</span>)</div><div class="line"> .option(<span class="string">'-e, --env [develop|test|bjac]'</span>, <span class="string">'Run ENV'</span>)</div><div class="line"> .parse(process.argv);</div><div class="line"></div><div class="line"><span class="keyword">if</span>(<span class="keyword">typeof</span> program.env == <span class="string">"undefined"</span> ){</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">"argv error"</span>);</div><div class="line"> process.exit();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">let</span> _env = program.env;</div><div class="line"></div><div class="line"><span class="keyword">let</span> configfile = __dirname + <span class="string">"/config/config_"</span> + _env + <span class="string">".js"</span>;</div><div class="line"><span class="keyword">if</span>(!fs.existsSync(configfile)){</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">"no config file"</span>);</div><div class="line"> process.exit();</div><div class="line">}</div><div class="line"><span class="keyword">var</span> Config = <span class="built_in">require</span>(configfile);</div><div class="line"><span class="keyword">let</span> nodename = local_address();</div><div class="line"></div><div class="line"><span class="keyword">var</span> Server = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line"> <span class="keyword">let</span> self = <span class="keyword">this</span>;</div><div class="line"> self.ready = <span class="literal">false</span>;</div><div class="line"> self.name = nodename;</div><div class="line"> self.connectZK();</div><div class="line"> self.once(<span class="string">"ready"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line"> self.ready = <span class="literal">true</span>;</div><div class="line"> self.init();</div><div class="line"> });</div><div class="line">};</div><div class="line">util.inherits(Server, events.EventEmitter);</div><div class="line">Server.prototype.connectZK = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">let</span> self = <span class="keyword">this</span>;</div><div class="line"> <span class="keyword">let</span> zk = self.zk = zookeeper.createClient(Config.connectionString, Config.zkOptions);</div><div class="line"> zk.once(<span class="string">'connected'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Connected to ZooKeeper.'</span>);</div><div class="line"> createPath(<span class="string">"/test/"</span>+self.name, <span class="keyword">new</span> Buffer(self.name+<span class="string">":"</span>+Config.port));</div><div class="line"> });</div><div class="line"> zk.on(<span class="string">"error"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">error</span>)</span>{</div><div class="line"> <span class="built_in">console</span>.log(error);</div><div class="line"> self.emit(<span class="string">"error"</span>, error);</div><div class="line"> });</div><div class="line"> zk.connect();</div><div class="line"> <span class="function"><span class="keyword">function</span> <span class="title">createPath</span>(<span class="params">path, data</span>)</span>{</div><div class="line"> self.zk.create(</div><div class="line"> path,</div><div class="line"> data,</div><div class="line"> zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL,</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">error, path</span>) </span>{</div><div class="line"> <span class="keyword">if</span> (error) {</div><div class="line"> <span class="built_in">console</span>.log(error.stack);</div><div class="line"> self.emit(<span class="string">"error"</span>, error);</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> self.emit(<span class="string">"ready"</span>);</div><div class="line"> }</div><div class="line"> );</div><div class="line"> }</div><div class="line">};</div><div class="line">Server.prototype.init = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line"> <span class="keyword">let</span> self = <span class="keyword">this</span>;</div><div class="line"> self.server = net.createServer(<span class="function">(<span class="params">c</span>) =></span> {</div><div class="line"> <span class="comment">// 'connection' listener</span></div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'client connected'</span>);</div><div class="line"> c.on(<span class="string">'end'</span>, () => {</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'client disconnected'</span>);</div><div class="line"> });</div><div class="line"> c.write(<span class="string">'hello\r\n'</span>);</div><div class="line"> c.pipe(c);</div><div class="line"> });</div><div class="line"> self.server.on(<span class="string">'error'</span>, (err) => {</div><div class="line"> <span class="keyword">throw</span> err;</div><div class="line"> });</div><div class="line"> self.server.listen(Config.port, () => {</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'server bound'</span>);</div><div class="line"> });</div><div class="line">}</div><div class="line"><span class="keyword">var</span> sv = <span class="keyword">new</span> Server();</div></pre></td></tr></table></figure></p>
<h4 id="列出所有节点"><a href="#列出所有节点" class="headerlink" title="列出所有节点"></a>列出所有节点</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span>;</div><div class="line"><span class="keyword">const</span> zookeeper = <span class="built_in">require</span>(<span class="string">'node-zookeeper-client'</span>);</div><div class="line"><span class="keyword">const</span> client = zookeeper.createClient(<span class="string">"127.0.0.1:4180,127.0.0.1:4181,127.0.0.1:4182"</span>);</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">listChildren</span>(<span class="params">path</span>) </span>{</div><div class="line"> client.getChildren(</div><div class="line"> path,</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">event</span>) </span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Got watcher event: %s'</span>, event.getPath());</div><div class="line"> listChildren(path);</div><div class="line"> },</div><div class="line"> <span class="function"><span class="keyword">function</span> (<span class="params">error, children, stat</span>) </span>{</div><div class="line"> <span class="keyword">if</span> (error) {</div><div class="line"> <span class="built_in">console</span>.log(</div><div class="line"> <span class="string">'Failed to list children of %s due to: %s.'</span>,</div><div class="line"> path,</div><div class="line"> error</div><div class="line"> );</div><div class="line"> <span class="keyword">return</span>;</div><div class="line"> }</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Children of %s are: %j.'</span>, path, children);</div><div class="line"> }</div><div class="line"> );</div><div class="line">}</div><div class="line">client.once(<span class="string">'connected'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">'Connected to ZooKeeper.'</span>);</div><div class="line"> listChildren(<span class="string">"/test"</span>);</div><div class="line">});</div><div class="line"></div><div class="line">client.connect();</div></pre></td></tr></table></figure>
<h4 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h4><p>zookeeper创建目录节点的时候有四种模式: </p>
<ul>
<li>PERSISTENT:持久化目录节点,存储的数据不会丢失。</li>
<li>PERSISTENT_SEQUENTIAL:顺序自动编号的持久化目录节点,存储的数据不会丢失,并且根据当前已近存在的节点数自动加 1,然后返回给客户端已经成功创建的目录节点名。</li>
<li>EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口也就是session 超时,这种节点会被自动删除。</li>
<li>EPHEMERAL_SEQUENTIAL:临时自动编号节点,一旦创建这个节点的客户端与服务器端口也就是session 超时,这种节点会被自动删除,并且根据当前已近存在的节点数自动加 1,然后返回给客户端已经成功创建的目录节点名。<br>ZooKeeper中一共由三种方法可以实现Watch,分别为getData、exists和getChildren,但这三种方法仅仅监控一次数据变化,要想永久监控,需要在对应的事件回调中继续增加watch。</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://www.lvphp.com/2016/12/18/php-swool/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="lvphpwb">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="LVPHP">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="LVPHP" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/18/php-swool/" itemprop="url">
初试 php-swool
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-18T23:27:23+08:00">
2016-12-18
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/18/php-swool/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/18/php-swool/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>swool官网网站:<a href="http://www.swoole.com/" target="_blank" rel="external">swool</a><br>PHP的异步、并行、高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。<br>Swoole内置了Http/WebSocket服务器端/客户端、Http2.0服务器端。<br>Swoole可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用PHP+Swoole作为网络通信框架,可以使企业IT研发团队的效率大大提升,更加专注于开发创新产品。 </p>
<h4 id="一个简单的http-server"><a href="#一个简单的http-server" class="headerlink" title="一个简单的http server"></a>一个简单的http server</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">$serv = <span class="keyword">new</span> Swoole\Http\Server(<span class="string">"127.0.0.1"</span>, <span class="number">9502</span>);</div><div class="line">$serv->on(<span class="string">'Request'</span>, <span class="function"><span class="keyword">function</span><span class="params">($request, $response)</span> </span>{</div><div class="line"> var_dump($request->get);</div><div class="line"> var_dump($request->post);</div><div class="line"> var_dump($request->cookie);</div><div class="line"> var_dump($request->files);</div><div class="line"> var_dump($request->header);</div><div class="line"> var_dump($request->server);</div><div class="line"> $response->cookie(<span class="string">"User"</span>, <span class="string">"Swoole"</span>);</div><div class="line"> $response->header(<span class="string">"X-Server"</span>, <span class="string">"Swoole"</span>);</div><div class="line"> $response->end(<span class="string">"<h1>Hello Swoole!</h1>"</span>);</div><div class="line">});</div><div class="line">$serv->start();</div></pre></td></tr></table></figure>
<h4 id="使用中的修改"><a href="#使用中的修改" class="headerlink" title="使用中的修改"></a>使用中的修改</h4><h5 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">+/config--------配置文件</div><div class="line"> |/config_develop.php</div><div class="line"> |/config_test.php</div><div class="line"> |/config_product.php</div><div class="line"> |/swool.lvphp.com.conf</div><div class="line">+/controller----控制器</div><div class="line"> |/IndexController.php</div><div class="line">+/process-------进程</div><div class="line"> |/Worker.php worker进程</div><div class="line"> |/Tasker.php task进程</div><div class="line">+/libs----------类库文件</div><div class="line"> |/Application.php</div><div class="line"> |/RedisDB.php</div><div class="line">+/public--------静态文件</div><div class="line"> |/index.html</div><div class="line"> |/favicon.ico</div><div class="line">+server.php-----启动文件</div></pre></td></tr></table></figure>
<h5 id="启动程序"><a href="#启动程序" class="headerlink" title="启动程序"></a>启动程序</h5><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line">server.php</div><div class="line"><span class="meta"><?php</span></div><div class="line">define(<span class="string">"ROOT_PATH"</span>, dirname(<span class="keyword">__FILE__</span>));</div><div class="line">define(<span class="string">"LOG_NAME"</span>, <span class="string">"swool.lvphp.com"</span>);</div><div class="line">define(<span class="string">"LOG_PATH"</span>, <span class="string">"/data/logs/php/"</span>);</div><div class="line">define(<span class="string">"API_CALLER"</span>, <span class="string">"swool"</span>);</div><div class="line"><span class="comment">//note 加载配置文件</span></div><div class="line">$server_type = get_cfg_var(<span class="string">'yaf.environ'</span>);</div><div class="line">$conf_file = ROOT_PATH . <span class="string">"/config/config_{$server_type}.php"</span>;</div><div class="line"><span class="keyword">if</span>(!is_file($conf_file)){</div><div class="line"> <span class="keyword">exit</span>(<span class="string">"no config file"</span>);</div><div class="line">}</div><div class="line"><span class="keyword">require</span>(ROOT_PATH . <span class="string">'/libs/Application.php'</span>);</div><div class="line">Application::load(<span class="keyword">array</span>(<span class="string">"dir"</span>=><span class="keyword">array</span>(ROOT_PATH . <span class="string">"/controller"</span>, ROOT_PATH . <span class="string">"/libs"</span>, ROOT_PATH . <span class="string">"/process"</span>), <span class="string">"file"</span>=><span class="keyword">array</span>(<span class="string">"Config"</span>=>$conf_file)));</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">HttpServer</span></span>{</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $instance;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">($conf)</span> </span>{</div><div class="line"> <span class="comment">//note 开启服务</span></div><div class="line"> $http = <span class="keyword">new</span> swoole_http_server(Config::SWOOL_ADDR, Config::SWOOL_PORT);</div><div class="line"> $http->set($conf);</div><div class="line"> $http->on(<span class="string">"start"</span>, <span class="function"><span class="keyword">function</span><span class="params">()</span></span>{ cli_set_process_title(Config::SERVER_NAME . <span class="string">"-master"</span>); });</div><div class="line"> $http->on(<span class="string">'WorkerStart'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv, $worker_id)</span></span>{</div><div class="line"> <span class="keyword">if</span>($worker_id >= $serv->setting[<span class="string">'worker_num'</span>]) {</div><div class="line"> cli_set_process_title(Config::SERVER_NAME . <span class="string">"-task-"</span> . $worker_id);</div><div class="line"> <span class="comment">//note task初始化</span></div><div class="line"> Tasker::start($serv, $worker_id);</div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> cli_set_process_title(Config::SERVER_NAME . <span class="string">"-worker-"</span> . $worker_id);</div><div class="line"> <span class="comment">//note worker初始化</span></div><div class="line"> Worker::start($serv, $worker_id);</div><div class="line"> }</div><div class="line"> });</div><div class="line"> $http->on(<span class="string">"request"</span>, <span class="string">"Application::onRequest"</span>); <span class="comment">//note 处理请求</span></div><div class="line"> $http->on(<span class="string">"task"</span>, <span class="string">"Tasker::onTask"</span>); <span class="comment">//note 处理task</span></div><div class="line"> $http->on(<span class="string">"finish"</span>, <span class="string">"Worker::onFinish"</span>); <span class="comment">//note task finish</span></div><div class="line"> $http->start();</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getInstance</span><span class="params">($conf)</span> </span>{</div><div class="line"> <span class="keyword">if</span> (!<span class="keyword">self</span>::$instance) {</div><div class="line"> <span class="keyword">self</span>::$instance = <span class="keyword">new</span> HttpServer($conf);</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="keyword">self</span>::$instance;</div><div class="line"> }</div><div class="line">}</div><div class="line">HttpServer::getInstance(Config::$swool);</div></pre></td></tr></table></figure>
<h5 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h5><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">config_develop.php</div><div class="line"><span class="meta"><?php</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Config</span></span></div><div class="line">{</div><div class="line"> <span class="keyword">const</span> ENV = <span class="string">"beta"</span>;</div><div class="line"></div><div class="line"> <span class="keyword">const</span> SERVER_NAME = <span class="string">"swool"</span>;</div><div class="line"> <span class="keyword">const</span> SWOOL_ADDR = <span class="string">"0.0.0.0"</span>;</div><div class="line"> <span class="keyword">const</span> SWOOL_PORT = <span class="number">9505</span>;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $swool = <span class="keyword">array</span>(</div><div class="line"> <span class="string">"max_request"</span> => <span class="number">100000</span>,</div><div class="line"> <span class="string">"heartbeat_idle_time"</span> => <span class="number">600</span>,</div><div class="line"> <span class="string">"worker_num"</span> => <span class="number">4</span>,</div><div class="line"> <span class="string">"task_worker_num"</span> => <span class="number">1</span>,</div><div class="line"> <span class="string">"heartbeat_check_interval"</span> => <span class="number">60</span>,</div><div class="line"> <span class="string">"dispatch_mode"</span> => <span class="number">1</span>,</div><div class="line"> <span class="string">"log_file"</span> => <span class="string">"/data/logs/php/swool.lvphp.com.log"</span>,</div><div class="line"> <span class="string">"log_level"</span> => <span class="number">4</span>,</div><div class="line"> <span class="string">"daemonize"</span> => <span class="number">0</span></div><div class="line"> );</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $redis = <span class="keyword">array</span>(</div><div class="line"> <span class="string">"cache.read"</span> => <span class="keyword">array</span>(</div><div class="line"> <span class="string">"name"</span> => <span class="string">"cache.read"</span>,</div><div class="line"> <span class="string">"host"</span> => <span class="string">"127.0.0.1"</span>,</div><div class="line"> <span class="string">"port"</span> => <span class="string">"14000"</span>,</div><div class="line"> <span class="string">"pwd"</span> => <span class="string">"lvphp"</span>,</div><div class="line"> <span class="string">"timeout"</span> => <span class="number">3</span>,</div><div class="line"> <span class="string">"prefix"</span> => <span class="string">"cache:"</span>,</div><div class="line"> )</div><div class="line"> );</div><div class="line">}</div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">swool.lvphp.com.conf</div><div class="line">server {</div><div class="line"> listen <span class="number">80</span>;</div><div class="line"> server_name swool.lvphp.com;</div><div class="line"> root /data/htdocs/swool.lvphp.com/<span class="keyword">public</span>/;</div><div class="line"> index index.html;</div><div class="line"></div><div class="line"> location / {</div><div class="line"> <span class="keyword">if</span> (!-e $request_filename) {</div><div class="line"> proxy_pass http:<span class="comment">//127.0.0.1:9505;</span></div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="comment">#access_log /data/logs/nginx/swool.lvphp.com.access.log main;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<h4 id="类库"><a href="#类库" class="headerlink" title="类库"></a>类库</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div></pre></td><td class="code"><pre><div class="line">Application.php</div><div class="line"><span class="meta"><?php</span></div><div class="line">defined(<span class="string">'ROOT_PATH'</span>) || <span class="keyword">exit</span>(<span class="string">"NO ROOT_PATH!"</span>);</div><div class="line">defined(<span class="string">'LOG_PATH'</span>) || <span class="keyword">exit</span>(<span class="string">"NO LOG_PATH!"</span>);</div><div class="line">defined(<span class="string">'LOG_NAME'</span>) || <span class="keyword">exit</span>(<span class="string">"NO LOG_NAME!"</span>);</div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Application</span></span>{</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $starttime = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $controller = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $method = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $argTypes = <span class="keyword">array</span>(<span class="string">"p"</span>=><span class="string">"post"</span>, <span class="string">"g"</span>=><span class="string">"get"</span>, <span class="string">"c"</span>=><span class="string">"cookie"</span>);</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $directories = <span class="keyword">array</span>();</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $files = <span class="keyword">array</span>();</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $inputs = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $server = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">()</span> </span>{}</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">load</span><span class="params">($loadData)</span></span>{</div><div class="line"> <span class="keyword">if</span>(<span class="keyword">isset</span>($loadData[<span class="string">"dir"</span>])){ <span class="keyword">self</span>::$directories = $loadData[<span class="string">"dir"</span>];}</div><div class="line"> <span class="keyword">if</span>(<span class="keyword">isset</span>($loadData[<span class="string">"file"</span>])){ <span class="keyword">self</span>::$files = $loadData[<span class="string">"file"</span>];}</div><div class="line"> spl_autoload_register(<span class="string">"Application::autoLoad"</span>);</div><div class="line"> }</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">autoLoad</span><span class="params">($className)</span></span>{</div><div class="line"> $dsClassName = str_replace(<span class="string">"_"</span>, <span class="string">"/"</span>, $className);</div><div class="line"> <span class="keyword">if</span>(is_array(<span class="keyword">self</span>::$directories)){</div><div class="line"> <span class="keyword">foreach</span>(<span class="keyword">self</span>::$directories <span class="keyword">as</span> $directorie){</div><div class="line"> $filePath = rtrim($directorie, <span class="string">"/"</span>) . <span class="string">"/"</span> . $dsClassName . <span class="string">".php"</span>;</div><div class="line"> <span class="keyword">if</span>(is_file($filePath)){</div><div class="line"> <span class="keyword">require</span> $filePath;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">if</span>(is_array(<span class="keyword">self</span>::$files)){</div><div class="line"> <span class="keyword">foreach</span>(<span class="keyword">self</span>::$files <span class="keyword">as</span> $key => $file){</div><div class="line"> <span class="keyword">if</span>($key == $className){</div><div class="line"> <span class="keyword">require</span> $file;</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"No found class "</span> . $className, <span class="number">500</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">writeLog</span><span class="params">($type, $data, $addTime = true)</span></span>{</div><div class="line"> $data = $addTime ? (date(<span class="string">"Y-m-d H:i:s"</span>) . <span class="string">"\t"</span> . $data) : $data;</div><div class="line"> swoole_async_writefile(LOG_PATH . <span class="string">"/"</span> . LOG_NAME . <span class="string">"_{$type}.log"</span>, $data . <span class="string">"\r\n"</span>, <span class="function"><span class="keyword">function</span><span class="params">()</span></span>{}, FILE_APPEND);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">display</span><span class="params">($result)</span></span>{</div><div class="line"> <span class="comment">//note 请求响应日志</span></div><div class="line"> $consume = (int)round((microtime(<span class="keyword">true</span>) - Application::$starttime) * <span class="number">1000</span>, <span class="number">1</span>);</div><div class="line"> $logInfo = <span class="keyword">self</span>::$inputs;</div><div class="line"> $logInfo[<span class="string">'client_ip'</span>] = <span class="keyword">self</span>::getIP();</div><div class="line"> $logInfo[<span class="string">'logtime'</span>] = date(<span class="string">"Y-m-d H:i:s"</span>, intval(Application::$starttime));</div><div class="line"> $logInfo[<span class="string">'consume'</span>] = $consume;</div><div class="line"> $logInfo[<span class="string">'errno'</span>] = $result[<span class="string">'errno'</span>];</div><div class="line"> $logInfo[<span class="string">'errmsg'</span>] = $result[<span class="string">'errmsg'</span>];</div><div class="line"> <span class="keyword">self</span>::writeLog(<span class="string">'request'</span>, json_encode($logInfo), <span class="keyword">false</span>);</div><div class="line"> <span class="keyword">return</span> json_encode($result);</div><div class="line"> }</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">get</span><span class="params">($name, $hash = <span class="string">'get'</span>, $default = null)</span> </span>{</div><div class="line"> $hash = strtolower ( $hash );</div><div class="line"> <span class="keyword">switch</span> ($hash) {</div><div class="line"> <span class="keyword">case</span> <span class="string">'get'</span> : $input = <span class="keyword">self</span>::$inputs[<span class="string">'get'</span>];<span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> <span class="string">'post'</span> : $input = <span class="keyword">self</span>::$inputs[<span class="string">'post'</span>];<span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> <span class="string">'cookie'</span> : $input = <span class="keyword">self</span>::$inputs[<span class="string">'cookie'</span>];<span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> <span class="string">'files'</span> : $input = <span class="keyword">self</span>::$inputs[<span class="string">'files'</span>];<span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> <span class="string">'server'</span> : $input = <span class="keyword">self</span>::$server;<span class="keyword">break</span>;</div><div class="line"> <span class="keyword">default</span> : $input = <span class="keyword">isset</span>(<span class="keyword">self</span>::$inputs[$hash]) ? <span class="keyword">self</span>::$inputs[$hash] : <span class="keyword">NULL</span>;<span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> (<span class="keyword">isset</span> ($input[$name]) ? $input[$name] : $default);</div><div class="line"> }</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">set</span><span class="params">($name, $hash = <span class="string">'get'</span>, $value = <span class="string">""</span>)</span></span>{</div><div class="line"> <span class="keyword">self</span>::$inputs[strtolower($hash)][$name] = $value;</div><div class="line"> }</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getIP</span><span class="params">()</span></span>{</div><div class="line"> $ip = <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">if</span> (getenv(<span class="string">"REMOTE_ADDR"</span>) && strcasecmp(getenv(<span class="string">"REMOTE_ADDR"</span>), <span class="string">"unknown"</span>)){</div><div class="line"> $ip = getenv(<span class="string">"REMOTE_ADDR"</span>);</div><div class="line"> }<span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">isset</span>(<span class="keyword">self</span>::$server[<span class="string">'remote_addr'</span>]) && <span class="keyword">self</span>::$server[<span class="string">'remote_addr'</span>] && strcasecmp(<span class="keyword">self</span>::$server[<span class="string">'remote_addr'</span>], <span class="string">"unknown"</span>)){</div><div class="line"> $ip = <span class="keyword">self</span>::$server[<span class="string">'remote_addr'</span>];</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> $ip;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">Route</span><span class="params">()</span></span>{</div><div class="line"> $method = <span class="keyword">self</span>::get(<span class="string">'method'</span>);</div><div class="line"> <span class="keyword">if</span>($method){</div><div class="line"> $info = explode(<span class="string">"."</span>, $method);</div><div class="line"> <span class="keyword">if</span>(count($info) == <span class="number">2</span>){</div><div class="line"> $controllerName = ucfirst($info[<span class="number">0</span>]) . <span class="string">'Controller'</span>;</div><div class="line"> $actionName = strtolower($info[<span class="number">1</span>]) . <span class="string">'Action'</span>;</div><div class="line"> <span class="comment">//note 实例化控制器</span></div><div class="line"> <span class="keyword">self</span>::$controller = <span class="keyword">new</span> $controllerName();</div><div class="line"> <span class="keyword">if</span>(method_exists(<span class="keyword">self</span>::$controller, $actionName)){</div><div class="line"> $className = get_class(<span class="keyword">self</span>::$controller);</div><div class="line"> $class = <span class="keyword">new</span> ReflectionClass($className);</div><div class="line"> <span class="keyword">self</span>::$method = $class->getMethod($actionName);</div><div class="line"> }<span class="keyword">else</span>{</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"No method exists"</span>, <span class="number">500</span>);</div><div class="line"> }</div><div class="line"> }<span class="keyword">else</span>{</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"Error method param"</span>, <span class="number">401</span>);</div><div class="line"> }</div><div class="line"> }<span class="keyword">else</span>{</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"No method param"</span>, <span class="number">401</span>);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">prepareParams</span><span class="params">()</span></span>{</div><div class="line"> $args = <span class="keyword">array</span>();</div><div class="line"> $params = <span class="keyword">self</span>::$method->getParameters();</div><div class="line"> <span class="keyword">if</span>($params){</div><div class="line"> <span class="keyword">foreach</span>($params <span class="keyword">as</span> $p){</div><div class="line"> $input = <span class="keyword">false</span>;</div><div class="line"> $pargName = $p->getName();</div><div class="line"> $tmp = explode(<span class="string">"_"</span>, $pargName);</div><div class="line"> <span class="keyword">if</span>(<span class="keyword">isset</span>(<span class="keyword">self</span>::$argTypes[$tmp[<span class="number">0</span>]])){</div><div class="line"> $pargType = <span class="keyword">self</span>::argTypes[$tmp[<span class="number">0</span>]];</div><div class="line"> $pargName = substr($pargName, <span class="number">2</span>);</div><div class="line"> }<span class="keyword">else</span>{</div><div class="line"> $pargType = <span class="string">"get"</span>;</div><div class="line"> }</div><div class="line"> $input = <span class="keyword">isset</span>( <span class="keyword">self</span>::$inputs[$pargType][$pargName] ) ? <span class="keyword">self</span>::$inputs[$pargType][$pargName] : <span class="keyword">false</span>;</div><div class="line"> $default = $p->isDefaultValueAvailable();</div><div class="line"> <span class="keyword">if</span>(<span class="number">0</span> != $input && (<span class="keyword">false</span> === $input || <span class="keyword">empty</span>($input)) && <span class="keyword">false</span> === $default){</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"No {$pargName} {$pargType} param"</span>, <span class="number">401</span>);</div><div class="line"> }</div><div class="line"> $args[$pargName] = <span class="keyword">false</span> !== $input ? $input : $p->getDefaultValue();</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> $args;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">Run</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">self</span>::Route();</div><div class="line"> <span class="keyword">return</span> <span class="keyword">self</span>::$method->invokeArgs(<span class="keyword">self</span>::$controller, <span class="keyword">self</span>::prepareParams());</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">onRequest</span><span class="params">($req, $res)</span></span>{</div><div class="line"> $result = <span class="keyword">array</span>(<span class="string">"errno"</span>=><span class="number">0</span>, <span class="string">"errmsg"</span>=><span class="string">""</span>, <span class="string">"data"</span>=><span class="string">""</span>);</div><div class="line"> <span class="keyword">self</span>::$starttime = microtime(<span class="keyword">true</span>);</div><div class="line"> <span class="keyword">self</span>::$server = $req->server;</div><div class="line"> <span class="keyword">self</span>::$inputs = <span class="keyword">array</span>(</div><div class="line"> <span class="string">"get"</span> => $req->get,</div><div class="line"> <span class="string">"post"</span> => $req->post,</div><div class="line"> <span class="string">"cookie"</span> => $req->cookie,</div><div class="line"> <span class="string">"files"</span> => $req->files,</div><div class="line"> <span class="string">"input"</span> => <span class="keyword">empty</span>($req->post) ? $req->rawContent() : <span class="keyword">null</span>,</div><div class="line"> );</div><div class="line"> <span class="keyword">try</span>{</div><div class="line"> <span class="comment">//note 程序运行</span></div><div class="line"> $result[<span class="string">'data'</span>] = <span class="keyword">self</span>::run();</div><div class="line"> }<span class="keyword">catch</span>(<span class="keyword">Exception</span> $e){</div><div class="line"> $errno = $e->getCode();</div><div class="line"> <span class="keyword">empty</span>($errno) && $errno = <span class="number">500</span>;</div><div class="line"> $result[<span class="string">'errno'</span>] = $errno;</div><div class="line"> $result[<span class="string">'errmsg'</span>] = $e->getMessage();</div><div class="line"> }</div><div class="line"> <span class="comment">//note 输出结果</span></div><div class="line"> $res->header(<span class="string">"Content-Type"</span>, <span class="string">"application/json; charset=UTF-8"</span>);</div><div class="line"> $res->header(<span class="string">"Cache-Control"</span>, <span class="string">"no-store"</span>);</div><div class="line"> $res->end(<span class="keyword">self</span>::display($result));</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">RedisDB.php</div><div class="line"><span class="meta"><?php</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">RedisDB</span></span>{</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $pool = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getInstance</span><span class="params">($conf)</span></span>{</div><div class="line"> <span class="keyword">if</span>($conf){</div><div class="line"> $name = $conf[<span class="string">'name'</span>];</div><div class="line"> <span class="keyword">if</span>(!<span class="keyword">isset</span>(<span class="keyword">self</span>::$pool[$name])){</div><div class="line"> <span class="keyword">try</span>{</div><div class="line"> <span class="keyword">self</span>::$pool[$name] = <span class="keyword">new</span> Redis();</div><div class="line"> $result = <span class="keyword">self</span>::$pool[$name]->pconnect($conf[<span class="string">'host'</span>], $conf[<span class="string">'port'</span>], $conf[<span class="string">'timeout'</span>]);</div><div class="line"> <span class="keyword">if</span>(!$result){</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"Redis {$name} connect error"</span>, <span class="number">500</span>);</div><div class="line"> }</div><div class="line"> <span class="keyword">if</span>(<span class="keyword">isset</span>($conf[<span class="string">'pwd'</span>])){</div><div class="line"> <span class="keyword">self</span>::$pool[$name]->auth($conf[<span class="string">"pwd"</span>]);</div><div class="line"> }</div><div class="line"> <span class="keyword">if</span>(<span class="keyword">isset</span>($conf[<span class="string">'prefix'</span>])){</div><div class="line"> <span class="keyword">self</span>::$pool[$name]->setOption(Redis::OPT_PREFIX, $conf[<span class="string">'prefix'</span>]);</div><div class="line"> }</div><div class="line"> }<span class="keyword">catch</span>(<span class="keyword">Exception</span> $e){</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(ErrorMessage::$errmsg[ErrorMessage::INVALID_REQUEST], ErrorMessage::INVALID_REQUEST);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> <span class="keyword">self</span>::$pool[$name];</div><div class="line"> }<span class="keyword">else</span>{</div><div class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="keyword">Exception</span>(<span class="string">"No Redis config"</span>, <span class="number">500</span>);</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h5 id="进程"><a href="#进程" class="headerlink" title="进程"></a>进程</h5><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">Tasker.php</div><div class="line"><span class="meta"><?php</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tasker</span></span>{</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $serv = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $tasker_id = <span class="keyword">null</span>;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $redisClient = <span class="keyword">null</span>;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">start</span><span class="params">($serv, $tasker_id)</span></span>{</div><div class="line"> <span class="keyword">self</span>::$serv = $serv;</div><div class="line"> <span class="keyword">self</span>::$tasker_id = $tasker_id;</div><div class="line"> <span class="keyword">self</span>::$redisClient = RedisDB::getInstance(Config::$redis[<span class="string">"cache.read"</span>]);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">onTask</span><span class="params">($serv, $task_id, $src_worker_id, $data)</span></span>{</div><div class="line"> <span class="keyword">self</span>::$redisClient->incr($data);</div><div class="line"> <span class="keyword">self</span>::$serv->finish(<span class="string">"finish"</span>);</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">Worker.php</div><div class="line"><span class="meta"><?php</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Worker</span></span>{</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $serv = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $worker_id = <span class="keyword">null</span>;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $redisClient = <span class="keyword">null</span>;</div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> $rand;</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">start</span><span class="params">($serv, $worker_id)</span></span>{</div><div class="line"> <span class="keyword">self</span>::$serv = $serv;</div><div class="line"> <span class="keyword">self</span>::$worker_id = $worker_id;</div><div class="line"> <span class="keyword">self</span>::$redisClient = RedisDB::getInstance(Config::$redis[<span class="string">"cache.read"</span>]);</div><div class="line"> <span class="keyword">self</span>::$rand = rand(<span class="number">0</span>, <span class="number">999999</span>);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">onFinish</span><span class="params">($serv, $task_id, $data)</span></span>{</div><div class="line"></div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h5 id="控制器"><a href="#控制器" class="headerlink" title="控制器"></a>控制器</h5><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">IndexController.php</div><div class="line"><span class="meta"><?php</span></div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">IndexController</span></span>{</div><div class="line"></div><div class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">indexAction</span><span class="params">($name = <span class="string">"hehe"</span>, $type)</span></span>{</div><div class="line"> <span class="keyword">if</span>($name ==<span class="string">"aaa"</span>){</div><div class="line"> sleep(<span class="number">3</span>);</div><div class="line"> }</div><div class="line"> Worker::$serv->taskwait(<span class="string">"wbtest"</span>, $timeout = <span class="number">0.5</span>);</div><div class="line"> $data = Worker::$redisClient->incr(<span class="string">"wbtest"</span>);</div><div class="line"> <span class="keyword">return</span> Application::get(<span class="string">"name"</span>) . <span class="string">"-"</span> . Worker::$rand . <span class="string">"-"</span> . $data;</div><div class="line"> }</div><div class="line"></div><div class="line">}</div></pre></td></tr></table></figure>
<h4 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h4><p>以上代码只是一个demo,做了一下简单的路由,可以自己根据实际情况使用,task worker根据自己的需要确定是否使用。</p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://www.lvphp.com/2016/12/09/zimg-imgserver/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="lvphpwb">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="LVPHP">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="LVPHP" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/09/zimg-imgserver/" itemprop="url">
zimg imageserver
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-09T17:15:21+08:00">
2016-12-09
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/09/zimg-imgserver/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/09/zimg-imgserver/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>zimg是<a href="http://blog.buaa.us/" target="_blank" rel="external">@招牌疯子</a>开源的一个图片存储程序,主要的优点是可以根据请求实时处理图片,并且进行压缩和存储,一是方便前端用户,二来降低流量。</p>
<h4 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h4><h5 id="安装依赖"><a href="#安装依赖" class="headerlink" title="安装依赖"></a>安装依赖</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ sudo yum install openssl-devel cmake libevent-devel libjpeg-devel giflib-devel libpng-devel libwebp-devel ImageMagick-devel libmemcached-devel</div></pre></td></tr></table></figure>
<h4 id="安装zimg"><a href="#安装zimg" class="headerlink" title="安装zimg"></a>安装zimg</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$ git <span class="built_in">clone</span> https://github.com/buaazp/zimg -b master --depth=1</div><div class="line">$ <span class="built_in">cd</span> zimg</div><div class="line">$ make</div><div class="line">$ <span class="built_in">cd</span> bin</div><div class="line">$ ./zimg conf/zimg.lua</div></pre></td></tr></table></figure>
<h4 id="zimg配置文件"><a href="#zimg配置文件" class="headerlink" title="zimg配置文件"></a>zimg配置文件</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">--zimg server config</div><div class="line"></div><div class="line">--server config</div><div class="line">--是否后台运行</div><div class="line">is_daemon = 1</div><div class="line">--绑定IP</div><div class="line">ip = <span class="string">'0.0.0.0'</span></div><div class="line">--端口</div><div class="line">port = 4869</div><div class="line">--运行线程数,默认值为服务器CPU数</div><div class="line">--thread_num = 4</div><div class="line">backlog_num = 1024</div><div class="line">max_keepalives = 1</div><div class="line">retry = 3</div><div class="line">system = io.popen(<span class="string">'uname -sn'</span>):<span class="built_in">read</span>(<span class="string">'*l'</span>)</div><div class="line"><span class="built_in">pwd</span> = io.popen(<span class="string">'pwd'</span>):<span class="built_in">read</span>(<span class="string">'*l'</span>)</div><div class="line"></div><div class="line">--header config</div><div class="line">--返回时所带的HTTP header</div><div class="line">headers = <span class="string">'Cache-Control:max-age=7776000'</span></div><div class="line">--是否启用etag缓存</div><div class="line">etag = 1</div></pre></td></tr></table></figure>
<p>默认使用4869端口,打开地址<code>http://{ip}:4869/</code>就可以上传图片了</p>
<h4 id="图片自定义处理"><a href="#图片自定义处理" class="headerlink" title="图片自定义处理"></a>图片自定义处理</h4><p><code>http://demo.buaa.us/5f189d8ec57f5a5a0d3dcba47fa797e2?w=500&h=500&g=0&x=0&y=0&r=45&q=75&f=jpeg</code><br><strong>说明</strong></p>
<ul>
<li>w:图片宽度</li>
<li>h:图片高度</li>
<li>g:g=1为转黑白色</li>
<li>x:裁剪的开始位置X坐标</li>
<li>y:裁剪的开始位置Y坐标</li>
<li>r:旋转角度</li>
<li>q:图片质量</li>
<li>f:输出格式</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://www.lvphp.com/2016/12/09/php-trie-filter/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="lvphpwb">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="LVPHP">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="LVPHP" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/09/php-trie-filter/" itemprop="url">
使用php扩展trie_filter,利用词库,过滤敏感词
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-09T15:14:39+08:00">
2016-12-09
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/09/php-trie-filter/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/09/php-trie-filter/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>trie_filter是一个php关键词过滤扩展,用于检查一段文本中是否出现敏感词,基于Double-Array Trie 树实现。</p>
<h4 id="安装步骤"><a href="#安装步骤" class="headerlink" title="安装步骤"></a>安装步骤</h4><h5 id="安装libiconv,这个是libdatrie的依赖项"><a href="#安装libiconv,这个是libdatrie的依赖项" class="headerlink" title="安装libiconv,这个是libdatrie的依赖项"></a>安装libiconv,这个是libdatrie的依赖项</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$ wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz</div><div class="line">$ tar zxvf libiconv-1.14.tar.gz</div><div class="line">$ <span class="built_in">cd</span> libiconv-1.14</div><div class="line">$ ./configure</div><div class="line">$ make</div><div class="line">$ make install</div></pre></td></tr></table></figure>
<h5 id="安装libdatrie依赖库"><a href="#安装libdatrie依赖库" class="headerlink" title="安装libdatrie依赖库"></a>安装libdatrie依赖库</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ tar zxvf libdatrie-0.2.4.tar.gz</div><div class="line">$ <span class="built_in">cd</span> libdatrie-0.2.4</div><div class="line">$ make clean $ ./configure --prefix=<span class="variable">$LIB_PATH</span></div><div class="line">$ make $ make install</div></pre></td></tr></table></figure>
<h5 id="安装trie-filter扩展"><a href="#安装trie-filter扩展" class="headerlink" title="安装trie_filter扩展"></a>安装trie_filter扩展</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ <span class="variable">$INSTALL_PHP_PATH</span>/bin/phpize</div><div class="line">$ ./configure --with-php-config=<span class="variable">$INSTALL_PHP_PATH</span>/bin/php-config --with-trie_filter=<span class="variable">$LIB_PATH</span></div><div class="line">$ make</div><div class="line">$ make install</div></pre></td></tr></table></figure>
<p>然后修改php.ini,增加一行:extension=trie_filter.so,然后重启PHP。</p>
<h5 id="生成用语检测的词典"><a href="#生成用语检测的词典" class="headerlink" title="生成用语检测的词典"></a>生成用语检测的词典</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$ tar zxf trie_filter-2011.03.21.tar.gz</div><div class="line">$ <span class="built_in">cd</span> trie_filter-2011.03.21</div><div class="line">$ gcc -o dpp dpp.c -ldatrie // 生成dpp命令用语编译词典</div><div class="line">$ ./dpp words.txt words.dic //将words.txt 编译成trie_filter使用的词典 words.txt中每个词占一行</div></pre></td></tr></table></figure>
<h4 id="PHP测试实例"><a href="#PHP测试实例" class="headerlink" title="PHP测试实例"></a>PHP测试实例</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="meta"><?php</span></div><div class="line">ini_set(<span class="string">'memory_limit'</span>, <span class="string">'512M'</span>);</div><div class="line"><span class="comment">// 载入词典,成功返回一个 Trie_Filter 资源句柄,失败返回 NULL </span></div><div class="line">$resTrie = trie_filter_load(<span class="string">'./words.dic'</span>);</div><div class="line">$str1 = <span class="string">'今天利用trie_filter做敏感词过滤示例'</span>; </div><div class="line">$str2 = <span class="string">'今天利用trie_filter做过滤示例'</span>; </div><div class="line"><span class="comment">// 检测文本中是否含有词典中定义的敏感词(假设敏感词设定为:‘敏感词’) </span></div><div class="line">$res1 = trie_filter_search_all($resTrie, $str1); <span class="comment">// 一次把所有的敏感词都检测出来</span></div><div class="line">$res2 = trie_filter_search($resTrie, $str2);<span class="comment">// 每次只检测一个敏感词</span></div><div class="line">var_dump($res1); </div><div class="line"><span class="keyword">echo</span> <span class="string">"<br/-->"</span>; </div><div class="line">var_dump($res2);</div><div class="line">trie_filter_free($resTrie); <span class="comment">//最后别忘记调用free</span></div></pre></td></tr></table></figure>
<p><strong>注意</strong></p>
<ul>
<li>一般情况下字典文件会比较大,如果每次都加载字典文件,性能会大打折扣,我们可以结合swool来做敏感词判断服务,避免每次加载字典;</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://www.lvphp.com/2016/12/06/generater-yield/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="lvphpwb">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="LVPHP">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="LVPHP" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/06/generater-yield/" itemprop="url">
JS中generater和yield的使用
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-06T15:00:23+08:00">
2016-12-06
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<a href="/2016/12/06/generater-yield/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/12/06/generater-yield/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h4><p>function* 声明(function关键字后跟一个星号)定义一个generator(生成器)函数,返回一个Generator对象。</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span>* <span class="title">name</span>(<span class="params">[param[, param[, ... param]]]</span>) </span>{ statements }</div></pre></td></tr></table></figure>
<p><strong>name</strong><br>函数名</p>
<p><strong>param</strong><br>传入函数的参数名,一个函数至多可有255个参数。</p>
<p><strong>statements</strong><br>函数的主体</p>
<h4 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h4><p>生成器是一种可以从中退出并在之后重新进入的函数。生成器的环境(绑定的变量)会在每次执行后被保存,下次进入时可继续使用。<br>调用一个生成器函数并不马上执行它的主体,而是返回一个这个生成器函数的迭代器(iterator)对象。当这个迭代器的next()方法被调用时,生成器函数的主体会被执行直至第一个yield表达式,该表达式定义了迭代器返回的值,或者,被 yield*委派至另一个生成器函数。next()方法返回一个对象,该对象有一个value属性,表示产出的值,和一个done属性,表示生成器是否已经产出了它最后的值。</p>
<h4 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span></div><div class="line"><span class="function"><span class="keyword">function</span>* <span class="title">idMaker</span>(<span class="params"></span>)</span>{</div><div class="line"> <span class="keyword">var</span> index = <span class="number">0</span>;</div><div class="line"> <span class="keyword">while</span>(index<<span class="number">3</span>)</div><div class="line"> <span class="keyword">yield</span> index++;</div><div class="line">}</div><div class="line"><span class="keyword">var</span> gen = idMaker();</div><div class="line"><span class="built_in">console</span>.log(gen.next().value); <span class="comment">// 0</span></div><div class="line"><span class="built_in">console</span>.log(gen.next().value); <span class="comment">// 1</span></div><div class="line"><span class="built_in">console</span>.log(gen.next().value); <span class="comment">// 2</span></div><div class="line"><span class="built_in">console</span>.log(gen.next().value); <span class="comment">// undefined</span></div></pre></td></tr></table></figure>
<h4 id="co的使用"><a href="#co的使用" class="headerlink" title="co的使用"></a>co的使用</h4><p>在开发中,为了方便的使用generator,我们可以使用TJ大神的co库,co其实是帮我们封装了不断调用next的过程<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span></div><div class="line"><span class="keyword">const</span> co = <span class="built_in">require</span>(<span class="string">"co"</span>);</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">sleep</span>(<span class="params">ms</span>)</span>{</div><div class="line"> <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">done</span>)</span>{</div><div class="line"> setTimeout(done, ms)</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line">co(<span class="function"><span class="keyword">function</span>* (<span class="params"></span>)</span>{</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">"sleep"</span>);</div><div class="line"> <span class="keyword">yield</span> sleep(<span class="number">1000</span>)</div><div class="line"> <span class="built_in">console</span>.log(<span class="string">"sleep"</span>);</div><div class="line">}).catch(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>{</div><div class="line"></div><div class="line">})</div></pre></td></tr></table></figure></p>
<h4 id="thunkify的使用"><a href="#thunkify的使用" class="headerlink" title="thunkify的使用"></a>thunkify的使用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span></div><div class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>),</div><div class="line"> co = <span class="built_in">require</span>(<span class="string">"co"</span>),</div><div class="line"> thunkify = <span class="built_in">require</span>(<span class="string">"thunkify"</span>),</div><div class="line"> readfile = thunkify(fs.readFile);</div><div class="line"></div><div class="line">co(<span class="function"><span class="keyword">function</span>* (<span class="params"></span>)</span>{</div><div class="line"> <span class="keyword">let</span> content = <span class="keyword">yield</span> <span class="function"><span class="keyword">function</span>(<span class="params">done</span>)</span>{</div><div class="line"> fs.readFile(<span class="string">"/tmp/test.txt"</span>, done)</div><div class="line"> }</div><div class="line"> <span class="keyword">let</span> text = <span class="keyword">yield</span> readfile(<span class="string">"/tmp/test.txt"</span>);</div><div class="line"> <span class="built_in">console</span>.log(content, text);</div><div class="line">}).catch(<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>{</div><div class="line"></div><div class="line">});</div></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://www.lvphp.com/2016/12/03/file-get-contents/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="lvphpwb">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="LVPHP">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="LVPHP" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/03/file-get-contents/" itemprop="url">
php file_get_contents
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-12-03T12:03:04+08:00">
2016-12-03
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>