1 | 分布式服务框架
|
2 | ============
|
3 |
|
4 | + 基于Redis来传递消息
|
5 | + 无管理主机,由节点自主维护
|
6 | + 服务器任意加入
|
7 | + 简单出错处理机制:调用超时自动重新调用,超过一定次数时返回调用失败错误
|
8 | + 目前没有做任何性能测试
|
9 | + 可以随意启动多个节点进程,相同的服务也可以派发到多个不同节点来处理
|
10 |
|
11 |
|
12 | ## 安装
|
13 |
|
14 | ```bash
|
15 | $ npm install clouds --save
|
16 | ```
|
17 |
|
18 |
|
19 | ## 服务器端(Server)
|
20 |
|
21 | ```javascript
|
22 | var clouds = require('clouds');
|
23 |
|
24 | // 创建服务器
|
25 | var server = new clouds.Server({
|
26 | // redis连接配置
|
27 | redis: {
|
28 | host: '127.0.0.1',
|
29 | port: 6379,
|
30 | db: 3
|
31 | },
|
32 | // 心跳周期,如果服务器端异常下线,超过指定时间将自动从服务器端删除,单位:秒
|
33 | heartbeat: 2
|
34 | });
|
35 |
|
36 | // 注册服务处理程序
|
37 | server.register('test.hello', function (name, msg, callback) {
|
38 | // 函数的最后一个参数表示回调函数,客户端在调用的时候必须保证参数数量是一致的
|
39 | // 回调函数第一个参数表示是否出错,第二个参数起表示返回的结果
|
40 | callback(null, 'Hello ' + name + ', ' + msg);
|
41 | });
|
42 | ```
|
43 |
|
44 | ## 客户端(Client)
|
45 |
|
46 | ```javascript
|
47 | var clouds = require('clouds');
|
48 |
|
49 | var client = new clouds.Client({
|
50 | // redis连接配置
|
51 | redis: {
|
52 | host: '127.0.0.1',
|
53 | port: 6379,
|
54 | db: 3
|
55 | },
|
56 | // 调用超时时间,如果服务器超过指定时间没有响应结果,则认为调用失败,单位:秒
|
57 | timeout: 2
|
58 | });
|
59 |
|
60 | // 返回一个函数,用于直接调用远程服务
|
61 | var testHello = client.bind('test.hello');
|
62 |
|
63 | // 调用远程服务,跟使用本地普通函数差不多
|
64 | testHello('Glen', 'timestamp is ' + Date.now(), function (err, ret) {
|
65 | console.log(err, ret);
|
66 | });
|
67 |
|
68 | // 也可以这样直接调用,第一个参数是服务名,第二个参数是调用参数数组,第三个参数是回调函数
|
69 | client.call('test.hello', ['Glen', 'timestamp is ' + Date.now()], function (err, ret) {
|
70 | console.log(err, ret);
|
71 | });
|
72 | ```
|
73 |
|
74 | ## 监视器(Monitor)
|
75 |
|
76 | ```javascript
|
77 | var clouds = require('clouds');
|
78 |
|
79 | // 创建服务器
|
80 | var monitor = new clouds.Monitor({
|
81 | // redis连接配置
|
82 | redis: {
|
83 | host: '127.0.0.1',
|
84 | port: 6379,
|
85 | db: 3
|
86 | }
|
87 | });
|
88 |
|
89 | // 获取状态
|
90 | monitor.status(function (err, info) {
|
91 | console.log(err, info);
|
92 | // info.methods服务名对应的服务器ID列表
|
93 | // info.servers服务器对应的服务名列表
|
94 | });
|
95 | ```
|
96 |
|
97 | ## 出错处理
|
98 |
|
99 | 客户端在初始化时可设置一个超时时间,如果调用的服务超过该时间没有返回结果,将返回一个服务超时的错误。
|
100 |
|
101 | 在`Client.bind()`时可以指定自动重试的次数(仅当重试次数超过指定值时才放弃,并执行回调函数),比如:
|
102 |
|
103 | ```javascript
|
104 | // 返回一个函数,用于直接调用远程服务,第一个参数是服务名,第二个参数最大重试次数
|
105 | var testHello = client.bind('test.hello', 5);
|
106 |
|
107 | // 调用远程服务,跟使用本地普通函数差不多
|
108 | testHello('Glen', 'timestamp is ' + Date.now(), function (err, ret) {
|
109 | console.log(err, ret);
|
110 | });
|
111 | ```
|
112 |
|
113 | ## 客户端之间消息通信
|
114 |
|
115 | 客户端和服务端均可互相发送消息:
|
116 |
|
117 | ```javascript
|
118 | // 接收消息
|
119 | client.on('message', function (sender, msg) {
|
120 | // sender表示消息发送者的ID
|
121 | // msg为消息内容,可以为对象、字符串、数值等任何可以转换成JSON字符串的内容
|
122 | });
|
123 |
|
124 | // 发送消息
|
125 | client.send('receiver', 'msg');
|
126 | ```
|
127 |
|
128 |
|
129 |
|
130 | License
|
131 | =======
|
132 |
|
133 | ```
|
134 | Copyright (c) 2012-2015 Zongmin Lei (雷宗民) <leizongmin@gmail.com>
|
135 | http://ucdok.com
|
136 |
|
137 | The MIT License
|
138 |
|
139 | Permission is hereby granted, free of charge, to any person obtaining
|
140 | a copy of this software and associated documentation files (the
|
141 | "Software"), to deal in the Software without restriction, including
|
142 | without limitation the rights to use, copy, modify, merge, publish,
|
143 | distribute, sublicense, and/or sell copies of the Software, and to
|
144 | permit persons to whom the Software is furnished to do so, subject to
|
145 | the following conditions:
|
146 |
|
147 | The above copyright notice and this permission notice shall be
|
148 | included in all copies or substantial portions of the Software.
|
149 |
|
150 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
151 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
152 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
153 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
154 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
155 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
156 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
157 | ```
|