SOA (Service Oriented Architecture) ,即面向服务的架构,是一种分布式计算的软件设计方法,软件的部分组件(调用者),可以通过网络上的通用协议调用另一个应用软件组件执行、运作,让调用者获得服务。这里的服务应视为软件功能的一个独立单元,可以远程访问并独立执行与更新,比如说需要查询的指定状态信息或者要执行的某项操作。换句话说,SOA 集成了独立部署和维护的软件组件,并允许它们相互通信和协同工作,以构建一个跨系统的软件应用。
/**
* server.publish(url[, payload])
* @param url {string}
* @param payload {object}
*/
server.publish('/a/b/c',{ param:{ hello:'hello'}})
server.publish('/a/b',{ param:{ hello:'hello'}, data:new Buffer([1,2,3])})
/**
* server.js
*/
server.publish('/a/b/c',{ param:{ hello:'hello'}})
server.publish('/a/b',{ param:{ hello:'hello'}, data:new Buffer([1,2,3])})
/**
* client.js
*/
client.subscribe('/a', function (error) {
if (error) {
console.error('Subscribe error', error)
}
})
client.on('message', function (url, payload) {
console.log('Message:', url, JSON.stringify(payload))
})
/**
* client.js
*/
client.datagram('/foo', { "param": { 'client send time': new Date().toLocaleString() } })
client.on('datagram', (url, payload) => {
console.log('client receive Datagram:', url, JSON.stringify(payload))
})
/**
* server.js
*/
server.ondata = function (cli, url, payload) {
console.log('server receive datagram', url, JSON.stringify(payload.param))
/*
* do something or reply a message
*/
cli.datagram('/bar', { param: `${new Date().toLocaleString()} ` })
}
/**
* server.js
*/
server.on('/a/b/c', function (cli, request, payload) {
/*
* do someting and reply
*/
console.log(JSON.stringify(payload))
cli.reply(0, request.seqno, {param:'completed'});
});
/**
* client.js
*/
client.call('/a/b/c', { method: vsoa.method.SET }, {
param: { hello: 'hello' }
}, function (error, payload) {
if (error) {
console.error('RPC call error:', error);
if (error.status) {
console.error('Error status code:', error.status);
}
} else {
console.log('RPC call OK:', JSON.stringify(payload));
}
})
/**
* server.js
*/
server.on('/a/b/c', function (cli, request, payload) {
var stream = server.createStream();
cli.reply(0, request.seqno, stream.tunid);
stream.on('connect', () => {
stream.write(Buffer([0, 1, 2, 3, 4, 5, 6]));
stream.close();
});
stream.on('timeout', () => {
console.log('No client connect stream!');
});
});
/**
* client.js
*/
client.fetch('/a/b/c').then((payload, tunid) => {
var stream = client.createStream(tunid);
// Duplex Stream
stream.on('data', chunk => {
console.log('Stream rece:', chunk.byteLength);
});
}).catch(error => console.error(error));
const vsoa = require('vsoa');
const socket = require('socket');
const saddr = { domain: socket.AF_INET, addr: '127.0.0.1', port: 3200 };
const position = new vsoa.Position([
{ name: 's1', domain: 2, addr: '127.0.0.1', port: 2050, security: false }
], saddr);
position.start();
vsoa.Position.server(saddr)
position.onquery = function (name, domain, callback) {
console.log(`cannot find ${name} ,call this function`);
if (name === 's2') {
callback({ name: 's2', domain: 2, addr: '192.168.128.100', port: 2049, security: false });
} else {
callback({ name: 'default', domain: 2, addr: '192.168.100.100', port: 3000, security: false });
}
}
vsoa.lookup('s1', function (error, saddr) {
if (error) {
console.log(`lookup s1 error: ${error}`);
} else {
console.log('the ret of lookup s1:', JSON.stringify(saddr));
}
});
vsoa.lookup('s2', function (error, saddr) {
if (error) {
console.log(`lookup error: ${error}`);
} else {
console.log('the ret of lookup s2:', JSON.stringify(saddr));
}
});