-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathinter-ns.js
135 lines (118 loc) · 2.94 KB
/
inter-ns.js
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
"use strict";
const { ServiceBroker } = require("../");
const { isString } = require("../src/utils");
// --- INTER-NAMESPACE MIDDLEWARE ---
const InterNamespaceMiddleware = function (opts) {
if (!Array.isArray(opts)) throw new Error("Must be an Array");
let thisBroker;
const brokers = {};
return {
created(broker) {
thisBroker = broker;
opts.forEach(nsOpts => {
if (isString(nsOpts)) {
nsOpts = {
namespace: nsOpts
};
}
const ns = nsOpts.namespace;
this.logger.info(`Create internamespace broker for '${ns} namespace...'`);
const brokerOpts = _.defaultsDeep(
{},
nsOpts,
{ nodeID: null, middlewares: null },
broker.options
);
brokers[ns] = new ServiceBroker(brokerOpts);
});
},
started() {
return Promise.all(Object.values(brokers).map(b => b.start()));
},
stopped() {
return Promise.all(Object.values(brokers).map(b => b.stop()));
},
call(next) {
return function (actionName, params, opts = {}) {
if (isString(actionName) && actionName.includes("@")) {
const [action, namespace] = actionName.split("@");
if (brokers[namespace]) {
return brokers[namespace].call(action, params, opts);
} else if (namespace === thisBroker.namespace) {
return next(action, params, opts);
} else {
throw new Error("Unknow namespace: " + namespace);
}
}
return next(actionName, params, opts);
};
}
};
};
// --- NAMESPACE: ns-mars ---
const broker1 = new ServiceBroker({
namespace: "ns-mars",
nodeID: "node-1",
transporter: "NATS"
});
broker1.createService({
name: "greeter",
actions: {
hello(ctx) {
return `Hello from '${this.broker.namespace}' namespace!`;
}
}
});
// --- NAMESPACE: ns-venus ---
const broker2 = new ServiceBroker({
namespace: "ns-venus",
nodeID: "node-1",
transporter: "NATS"
});
broker2.createService({
name: "greeter",
actions: {
hello(ctx) {
return `Hello from '${this.broker.namespace}' namespace!`;
}
}
});
// --- LOCAL NAMESPACE ---
const broker = new ServiceBroker({
namespace: "local",
nodeID: "node-1",
transporter: "NATS",
middlewares: [InterNamespaceMiddleware(["ns-mars", "ns-venus"])]
});
broker.createService({
name: "greeter",
actions: {
hello(ctx) {
return `Hello from '${this.broker.namespace}' namespace!`;
}
}
});
Promise.all([broker1.start(), broker2.start(), broker.start()])
.delay(2000)
.then(() => {
broker.repl();
})
.then(() =>
broker.call("greeter.hello").then(res => broker.logger.info("Call 'greeter.hello':", res))
)
.then(() =>
broker
.call("greeter.hello@local")
.then(res => broker.logger.info("Call 'greeter.hello@local':", res))
)
.then(() =>
broker
.call("greeter.hello@ns-venus")
.then(res => broker.logger.info("Call 'greeter.hello@ns-venus':", res))
)
.then(() =>
broker
.call("greeter.hello@ns-mars")
.then(res => broker.logger.info("Call 'greeter.hello@ns-mars':", res))
)
.catch(err => broker.logger.error(err));