-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
372 lines (270 loc) · 126 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>LVPHP</title>
<link href="/atom.xml" rel="self"/>
<link href="http://www.lvphp.com/"/>
<updated>2017-02-21T02:20:39.504Z</updated>
<id>http://www.lvphp.com/</id>
<author>
<name>lvphpwb</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>试试zookeeper</title>
<link href="http://www.lvphp.com/2017/02/20/zookeeper/"/>
<id>http://www.lvphp.com/2017/02/20/zookeeper/</id>
<published>2017-02-20T10:51:31.000Z</published>
<updated>2017-02-21T02:20:39.504Z</updated>
<content type="html"><![CDATA[<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>
]]></content>
<summary type="html">
<h4 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h4><p>Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:
</summary>
<category term="zookeeper,node,配置" scheme="http://www.lvphp.com/tags/zookeeper-node-%E9%85%8D%E7%BD%AE/"/>
</entry>
<entry>
<title>初试 php-swool</title>
<link href="http://www.lvphp.com/2016/12/18/php-swool/"/>
<id>http://www.lvphp.com/2016/12/18/php-swool/</id>
<published>2016-12-18T15:27:23.000Z</published>
<updated>2016-12-20T09:23:23.044Z</updated>
<content type="html"><![CDATA[<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>
]]></content>
<summary type="html">
<h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>swool官网网站:<a href="http://www.swoole.com/" target="_blank" rel="extern
</summary>
<category term="php" scheme="http://www.lvphp.com/tags/php/"/>
<category term="swool" scheme="http://www.lvphp.com/tags/swool/"/>
</entry>
<entry>
<title>zimg imageserver</title>
<link href="http://www.lvphp.com/2016/12/09/zimg-imgserver/"/>
<id>http://www.lvphp.com/2016/12/09/zimg-imgserver/</id>
<published>2016-12-09T09:15:21.000Z</published>
<updated>2016-12-09T09:36:58.779Z</updated>
<content type="html"><![CDATA[<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>
]]></content>
<summary type="html">
<h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>zimg是<a href="http://blog.buaa.us/" target="_blank" rel="external">@招牌
</summary>
<category term="zimg" scheme="http://www.lvphp.com/tags/zimg/"/>
<category term="图片存储" scheme="http://www.lvphp.com/tags/%E5%9B%BE%E7%89%87%E5%AD%98%E5%82%A8/"/>
</entry>
<entry>
<title>使用php扩展trie_filter,利用词库,过滤敏感词</title>
<link href="http://www.lvphp.com/2016/12/09/php-trie-filter/"/>
<id>http://www.lvphp.com/2016/12/09/php-trie-filter/</id>
<published>2016-12-09T07:14:39.000Z</published>
<updated>2016-12-09T07:40:18.020Z</updated>
<content type="html"><![CDATA[<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>
]]></content>
<summary type="html">
<h4 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h4><p>trie_filter是一个php关键词过滤扩展,用于检查一段文本中是否出现敏感词,基于Double-Array Trie 树实现。</p>
</summary>
<category term="php" scheme="http://www.lvphp.com/tags/php/"/>
<category term="扩展" scheme="http://www.lvphp.com/tags/%E6%89%A9%E5%B1%95/"/>
<category term="trie_filter" scheme="http://www.lvphp.com/tags/trie-filter/"/>
<category term="敏感词" scheme="http://www.lvphp.com/tags/%E6%95%8F%E6%84%9F%E8%AF%8D/"/>
</entry>
<entry>
<title>JS中generater和yield的使用</title>
<link href="http://www.lvphp.com/2016/12/06/generater-yield/"/>
<id>http://www.lvphp.com/2016/12/06/generater-yield/</id>
<published>2016-12-06T07:00:23.000Z</published>
<updated>2016-12-07T06:10:22.118Z</updated>
<content type="html"><![CDATA[<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>
]]></content>
<summary type="html">
<h4 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h4><p>function* 声明(function关键字后跟一个星号)定义一个generator(生成器)函数,返回一个Generator对象。</
</summary>
<category term="js" scheme="http://www.lvphp.com/tags/js/"/>
<category term="ES6" scheme="http://www.lvphp.com/tags/ES6/"/>
<category term="generater" scheme="http://www.lvphp.com/tags/generater/"/>
<category term="yield" scheme="http://www.lvphp.com/tags/yield/"/>
<category term="co" scheme="http://www.lvphp.com/tags/co/"/>
<category term="thunkify" scheme="http://www.lvphp.com/tags/thunkify/"/>
</entry>
<entry>
<title>php file_get_contents</title>
<link href="http://www.lvphp.com/2016/12/03/file-get-contents/"/>
<id>http://www.lvphp.com/2016/12/03/file-get-contents/</id>
<published>2016-12-03T04:03:04.000Z</published>
<updated>2016-12-03T04:05:28.217Z</updated>
<content type="html"><![CDATA[<h4 id="使用file-get-contents发送post请求"><a href="#使用file-get-contents发送post请求" class="headerlink" title="使用file_get_contents发送post请求"></a>使用file_get_contents发送post请求</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></pre></td><td class="code"><pre><div class="line">$data = [<span class="string">'eat'</span>=><span class="string">'2kg'</span>,<span class="string">'run'</span>=><span class="string">'10km'</span>];</div><div class="line">$url = <span class="string">'http://localhost'</span>;</div><div class="line">$context = stream_context_create(<span class="keyword">array</span>(</div><div class="line"> <span class="string">'http'</span> => <span class="keyword">array</span>( </div><div class="line"> <span class="string">'method'</span> => <span class="string">'POST'</span>, </div><div class="line"> <span class="string">'header'</span> => <span class="string">'Content-type:application/x-www-form-urlencoded'</span>, </div><div class="line"> <span class="string">'content'</span> => html_build_query($info), </div><div class="line"> <span class="string">'timeout'</span> => <span class="number">3</span></div><div class="line"> )</div><div class="line">));</div><div class="line">$result = file_get_contents($url, <span class="keyword">false</span>, $context);</div></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<h4 id="使用file-get-contents发送post请求"><a href="#使用file-get-contents发送post请求" class="headerlink" title="使用file_get_contents发送post请求"></a>使用fil
</summary>
<category term="php" scheme="http://www.lvphp.com/tags/php/"/>
<category term="file_get_contents" scheme="http://www.lvphp.com/tags/file-get-contents/"/>
</entry>
<entry>
<title>微信JS SDK</title>
<link href="http://www.lvphp.com/2016/12/03/weixin-sdk/"/>
<id>http://www.lvphp.com/2016/12/03/weixin-sdk/</id>
<published>2016-12-03T03:17:56.000Z</published>
<updated>2016-12-03T03:46:48.366Z</updated>
<content type="html"><![CDATA[<h4 id="引入微信JS文件"><a href="#引入微信JS文件" class="headerlink" title="引入微信JS文件"></a>引入微信JS文件</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"><script type=<span class="string">"text/javascript"</span> src=<span class="string">"https://res.wx.qq.com/open/js/jweixin-1.1.0.js"</span>></script></div></pre></td></tr></table></figure>
<h4 id="获取access-token"><a href="#获取access-token" class="headerlink" title="获取access_token"></a>获取access_token</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$access_info = file_get_contents(<span class="string">"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"</span>);</div></pre></td></tr></table></figure>
<p>正常情况下,微信会返回下述JSON数据包给公众号:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">{"access_token":"ACCESS_TOKEN","expires_in":7200}</div></pre></td></tr></table></figure></p>
<p>错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">{"errcode":40013,"errmsg":"invalid appid"}</div></pre></td></tr></table></figure></p>
<p><strong>注意</strong><br>获取access_token有频次限制,获取的access_token需要在做缓存 </p>
<h4 id="获取jsapi-ticket"><a href="#获取jsapi-ticket" class="headerlink" title="获取jsapi_ticket"></a>获取jsapi_ticket</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ticket_info = file_get_contents(<span class="string">"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi"</span>);</div></pre></td></tr></table></figure>
<p>成功返回如下JSON:<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></pre></td><td class="code"><pre><div class="line">{</div><div class="line"><span class="string">"errcode"</span>:<span class="number">0</span>,</div><div class="line"><span class="string">"errmsg"</span>:<span class="string">"ok"</span>,</div><div class="line"><span class="string">"ticket"</span>:<span class="string">"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA"</span>,</div><div class="line"><span class="string">"expires_in"</span>:<span class="number">7200</span></div><div class="line">}</div></pre></td></tr></table></figure></p>
<p><strong>注意</strong><br>获取jsapi_ticket有频次限制,获取的jsapi_ticket需要在做缓存</p>
<h4 id="计算签名"><a href="#计算签名" class="headerlink" title="计算签名"></a>计算签名</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$signature = sha1(<span class="string">"jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=http://mp.weixin.qq.com?params=value"</span>)</div></pre></td></tr></table></figure>
<ul>
<li>jsapi_ticket: 获取的jsapi_ticket</li>
<li>noncestr: 随机串</li>
<li>timestamp: 时间戳</li>
<li>url: 当前网页的URL,不包含#及其后面部分</li>
</ul>
<h4 id="调用SDK"><a href="#调用SDK" class="headerlink" title="调用SDK"></a>调用SDK</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></pre></td><td class="code"><pre><div class="line">wx.config({</div><div class="line"> <span class="attr">debug</span>: <span class="literal">true</span>, <span class="comment">// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。</span></div><div class="line"> appId: <span class="string">'wx1a1bba241ca4cce5'</span>, <span class="comment">// 必填,公众号的唯一标识</span></div><div class="line"> timestamp: <span class="number">1428980719</span>, <span class="comment">// 必填,生成签名的时间戳</span></div><div class="line"> nonceStr: <span class="string">'Wm3WZYTPz0wzccnW'</span>, <span class="comment">// 必填,生成签名的随机串</span></div><div class="line"> signature: <span class="string">'2c3b0863b61fe7cc37961d7ce3ce71dc1d7360a8'</span>,<span class="comment">// 必填,签名,见附录1</span></div><div class="line"> jsApiList: [<span class="string">"previewImage"</span>] <span class="comment">// 必填,需要使用的JS接口列表,所有JS接口列表见附录2</span></div><div class="line">});</div><div class="line">wx.ready(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line"> $(<span class="string">".img"</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line"> wx.previewImage({</div><div class="line"> <span class="attr">current</span>: <span class="string">'http://apps.sinaapp.com/app/screen.jpeg'</span>, <span class="comment">// 当前显示的图片链接</span></div><div class="line"> urls: [<span class="string">'http://apps.sinaapp.com/app/link.png'</span>, <span class="string">'http://apps.sinaapp.com/app/search.png'</span>, <span class="string">'http://apps.sinaapp.com/app/searchn.png'</span>] <span class="comment">// 需要预览的图片链接列表</span></div><div class="line"> });</div><div class="line"> });</div><div class="line">});</div></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<h4 id="引入微信JS文件"><a href="#引入微信JS文件" class="headerlink" title="引入微信JS文件"></a>引入微信JS文件</h4><figure class="highlight js"><table><tr><td class
</summary>
<category term="js" scheme="http://www.lvphp.com/tags/js/"/>
<category term="微信" scheme="http://www.lvphp.com/tags/%E5%BE%AE%E4%BF%A1/"/>
<category term="weixin" scheme="http://www.lvphp.com/tags/weixin/"/>
<category term="sdk" scheme="http://www.lvphp.com/tags/sdk/"/>
</entry>
<entry>
<title>ES6-info</title>
<link href="http://www.lvphp.com/2016/12/02/es6-info/"/>
<id>http://www.lvphp.com/2016/12/02/es6-info/</id>
<published>2016-12-02T10:15:38.000Z</published>
<updated>2016-12-02T14:54:39.681Z</updated>
<content type="html"><![CDATA[<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></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span></div><div class="line"><span class="keyword">const</span> items = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</div><div class="line"><span class="keyword">const</span> itemscopy = [...items]</div><div class="line"><span class="built_in">console</span>.log(itemscopy); <span class="comment">//note out: [ 1, 2, 3, 4 ]</span></div><div class="line">itemscopy[<span class="number">0</span>] = <span class="number">6</span>;</div><div class="line"><span class="built_in">console</span>.log(items, itemscopy); <span class="comment">//note out: [ 1, 2, 3, 4 ] [ 6, 2, 3, 4 ]</span></div></pre></td></tr></table></figure>
<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></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">getFullName</span>(<span class="params">obj</span>) </span>{</div><div class="line"> <span class="keyword">const</span> { firstName, lastName } = obj;</div><div class="line"> <span class="keyword">return</span> <span class="string">`<span class="subst">${firstName}</span> <span class="subst">${lastName}</span>`</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">let</span> str = getFullName({<span class="attr">firstName</span>:<span class="string">"wangbo"</span>, <span class="attr">lastName</span>:<span class="string">"bo"</span>, <span class="attr">age</span>:<span class="number">30</span>});</div><div class="line"><span class="built_in">console</span>.log(str); <span class="comment">//note out: wangbo bo</span></div></pre></td></tr></table></figure>
<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></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span></div><div class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</div><div class="line"><span class="keyword">const</span> [first, second] = arr;</div><div class="line"><span class="built_in">console</span>.log(first, second); <span class="comment">//note out: 1 2</span></div></pre></td></tr></table></figure>
<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></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">processInput</span>(<span class="params"></span>) </span>{</div><div class="line"> <span class="keyword">return</span> {<span class="attr">left</span>: <span class="number">10</span>, <span class="attr">right</span>: <span class="number">20</span>, <span class="attr">top</span>: <span class="number">30</span>, <span class="attr">bottom</span>: <span class="number">40</span>};</div><div class="line">}</div><div class="line"><span class="keyword">const</span> { top, left } = processInput();</div><div class="line"><span class="built_in">console</span>.log(top, left); <span class="comment">//note out: 30 10</span></div></pre></td></tr></table></figure>
<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></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">sayHi</span>(<span class="params">name</span>) </span>{</div><div class="line"> <span class="keyword">return</span> <span class="string">`How are you, <span class="subst">${name}</span>?`</span>;</div><div class="line">}</div><div class="line"><span class="keyword">const</span> str = sayHi(<span class="string">"wangbo"</span>);</div><div class="line"><span class="built_in">console</span>.log(str); <span class="comment">//note out: How are you, wangbo?</span></div></pre></td></tr></table></figure>
<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></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">handleThings</span>(<span class="params">opts = {}</span>) </span>{</div><div class="line"></div><div class="line">}</div></pre></td></tr></table></figure>
<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></pre></td><td class="code"><pre><div class="line"><span class="meta">"use strict"</span></div><div class="line"><span class="keyword">const</span> items = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</div><div class="line"><span class="keyword">let</span> tmp = items.map(<span class="function">(<span class="params">x</span>)=></span>{<span class="keyword">return</span> x*x;});</div><div class="line"><span class="built_in">console</span>.log(tmp); <span class="comment">//note out: [ 1, 4, 9, 16 ]</span></div><div class="line"><span class="keyword">let</span> arr = items.map(<span class="function"><span class="params">x</span> =></span> x * x);</div><div class="line"><span class="built_in">console</span>.log(arr); <span class="comment">//note out: [ 1, 4, 9, 16 ]</span></div></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<h4 id="使用拓展运算符-…-复制数组"><a href="#使用拓展运算符-…-复制数组" class="headerlink" title="使用拓展运算符 … 复制数组"></a>使用拓展运算符 … 复制数组</h4><figure class="highlight
</summary>
<category term="js" scheme="http://www.lvphp.com/tags/js/"/>
<category term="es6" scheme="http://www.lvphp.com/tags/es6/"/>
</entry>
<entry>
<title>git-stash</title>
<link href="http://www.lvphp.com/2016/12/02/git-stash/"/>
<id>http://www.lvphp.com/2016/12/02/git-stash/</id>
<published>2016-12-02T10:02:38.000Z</published>
<updated>2016-12-02T10:08:48.372Z</updated>
<content type="html"><![CDATA[<h4 id="应用场合"><a href="#应用场合" class="headerlink" title="应用场合"></a>应用场合</h4><p>branch_A分支的工作区内容已被修改,但是并未完成。这时需要立即切换到另外一个分支branch_B上去,可是又不想提交目前的修改,因为修改没有完成。但是,不提交的话,又没有办法checkout到前面的分支。<br>此时用Git Stash就相当于备份工作区了。然后再Checkout到branch_B去修改,就能够达到保存当前工作区被修改过的内容,并及时恢复的作用。</p>
<h4 id="用法"><a href="#用法" class="headerlink" title="用法"></a>用法</h4><ul>
<li>git stash 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中;</li>
<li>git stash apply 把栈里面记录的内容运用到当前的版本上,这时候栈里面的记录还保存着;</li>
<li>git stash pop 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复, pop后栈中不再存在该记录;</li>
<li>git stash list 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复;</li>
<li>git stash clear 清空Git栈,此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了;</li>
</ul>
<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h4><p>在不同分支执行git stash 命令会将修改保存在同一个栈中,<br><strong>例如</strong></p>
<ol>
<li>在copy_master分支执行 git stash, 这时候stash 栈中有一个stash@{0} </li>
<li>在sonar分支执行 git stash, 这时候stash 栈中会再放进一个stash@{1} </li>
<li>执行git stash list命令,输出:<br>stash@{0}: WIP on copy_master: 2923b6d Merge branch ‘master’ into copy_master<br>stash@{1}: WIP on sonar: c2c677b Merge branch ‘master’ into sonar </li>
</ol>
]]></content>
<summary type="html">
<h4 id="应用场合"><a href="#应用场合" class="headerlink" title="应用场合"></a>应用场合</h4><p>branch_A分支的工作区内容已被修改,但是并未完成。这时需要立即切换到另外一个分支branch_B上去,可是又不想提交目
</summary>
<category term="git" scheme="http://www.lvphp.com/tags/git/"/>
<category term="git-stash" scheme="http://www.lvphp.com/tags/git-stash/"/>
</entry>
<entry>
<title>初试Hexo</title>
<link href="http://www.lvphp.com/2016/11/24/hello-world/"/>
<id>http://www.lvphp.com/2016/11/24/hello-world/</id>
<published>2016-11-24T08:48:21.879Z</published>
<updated>2016-12-02T09:58:50.683Z</updated>
<content type="html"><![CDATA[<p>Hexo官网网站:<a href="https://hexo.io/" target="_blank" rel="external">Hexo</a>,文档:<a href="https://hexo.io/docs/" target="_blank" rel="external">documentation</a></p>
<h2 id="使用命令"><a href="#使用命令" class="headerlink" title="使用命令"></a>使用命令</h2><h3 id="创建新文章"><a href="#创建新文章" class="headerlink" title="创建新文章"></a>创建新文章</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo new name</div></pre></td></tr></table></figure>
<h3 id="本地预览"><a href="#本地预览" class="headerlink" title="本地预览"></a>本地预览</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo server</div></pre></td></tr></table></figure>
<h3 id="生成静态文件"><a href="#生成静态文件" class="headerlink" title="生成静态文件"></a>生成静态文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo generate</div></pre></td></tr></table></figure>
<h3 id="发布"><a href="#发布" class="headerlink" title="发布"></a>发布</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo deploy</div></pre></td></tr></table></figure>
]]></content>
<summary type="html">
<p>Hexo官网网站:<a href="https://hexo.io/" target="_blank" rel="external">Hexo</a>,文档:<a href="https://hexo.io/docs/" target="_blank" rel="exter
</summary>
<category term="hexo" scheme="http://www.lvphp.com/tags/hexo/"/>
</entry>
</feed>