群集后,它无法按预期工作,并一次又一次地询问我的名字。
我错过了什么吗?
这是我的微软机器人框架代码。
server.js
var worker = require('./worker');
var cluster = require('cluster');
var spawnWorker = function() {
worker.createServer();
};
var createCluster = function() {
if(cluster.isMaster) {
var cpus = require('os').cpus().length;
for(var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('online', function(wkr) {
console.log('Worker started', wkr.process.pid);
});
cluster.on('exit', function(wkr) {
console.log('Worker died, respawing', wkr.process.pid);
cluster.fork();
});
} else {
spawnWorker();
}
};
createCluster();
worker.js
exports.createServer = function(){
var builder = require('botbuilder');
var connector = new builder.ConsoleConnector().listen();
var bot = new builder.UniversalBot(connector);
bot.dialog('/', [
function (session) {
builder.Prompts.text(session, process.pid+': Hi! What is your name?');
},
function (session, results) {
session.send(process.pid+':Hello %s!', results.response);
}
]);
}
输出
$ node server.js
Worker started 8540
Worker started 8068
Worker started 6020
Worker started 4244
8540: Hi! What is your name?
abc
8068: Hi! What is your name?
prq
6020: Hi! What is your name?
lmn
4244: Hi! What is your name?
xyz
更多信息here
答案 0 :(得分:6)
您正在使用Worker.js文件中的ConsoleConnector,该文件将所有机器人对话跟踪状态存储在内存中。这意味着每个节点都有一个单独的会话状态副本,它不会在多节点架构中工作。您需要使用使用BotFramework的ChatConnector将对话状态存储在机器人的一个中心位置,否则您必须将自定义IBotStorage实现传递给UniversalBot。该实现可以使用redis缓存或其他东西来集中所有内容。