Skip to content

Commit 59a9875

Browse files
committed
1、完善基础例子
1 parent 670da81 commit 59a9875

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

examples/2016.11.30-cluster/req.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
for((i=1;i<=4;i++)); do
4+
curl http://127.0.0.1:3000
5+
echo ""
6+
done

examples/2016.11.30-cluster/server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ if(cluster.isMaster){
88
}
99
}else{
1010
http.createServer(function(req, res){
11-
res.end('ok');
11+
res.end(`response from worker ${process.pid}`);
1212
}).listen(3000);
13+
14+
console.log(`Worker ${process.pid} started`);
1315
}

模块/cluster.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,34 @@ if(cluster.isMaster){
3333
}
3434
}else{
3535
http.createServer(function(req, res){
36-
res.end('ok');
36+
res.end(`response from worker ${process.pid}`);
3737
}).listen(3000);
38+
39+
console.log(`Worker ${process.pid} started`);
3840
}
3941
```
4042

43+
客户端发送请求:`./req.sh`
44+
45+
```bash
46+
#!/bin/bash
47+
48+
# req.sh
49+
for((i=1;i<=4;i++)); do
50+
curl http://127.0.0.1:3000
51+
echo ""
52+
done
53+
```
54+
55+
输出如下:
56+
57+
```bash
58+
response from worker 23735
59+
response from worker 23731
60+
response from worker 23729
61+
response from worker 23730
62+
```
63+
4164
## 实现原理
4265

4366
主要搞清楚三个问题:

0 commit comments

Comments
 (0)