UNPKG

dubbotest

Version:

Please run follow command in Windows GitBash or Linux. Can't use windows Cmd or PowerShell. ```shell npm install -g dubbotest dubbotest ```

145 lines (133 loc) 4.38 kB
const { Telnet } = require('telnet-client') const express = require('express') const process = require('process') const app = express() app.use(express.json()) process.on('uncaughtException', (error, source) => { // fs.writeSync(process.stderr.fd, error, source); console.log('Source: ', source) console.log(error) }); class TelnetTool { constructor(body) { if (!body.host) { throw new Error("host can't be empty") } if (!body.port) { throw new Error("port can't be empty") } if (!body.service && !body.provider) { throw new Error("service or provider can't be empty") } if (!body.method) { throw new Error("method can't be empty") } this.host = body.host this.port = body.port this.service = body.service || body.provider this.method = body.method this.param = body.param } getTelnetConfig() { return { host: this.host, port: this.port, negotiationMandatory: false, timeout: 3000, encoding: 'utf-8' } } getServicePath() { const _this = this const telnet = new Telnet() const config = this.getTelnetConfig() const command = `ls ${this.service}` return new Promise((resolve, reject) => { telnet.connect(config) .then(_ => { telnet .send(command) .then(r => { const providerKey = ' (as provider)' if (!r) { reject(`Run dubbo command fail, command: ${command}`) } if (r.indexOf(providerKey) < 0) { reject(`Service not found, service: ${_this.service}, info: ${r}`) } telnet.end() resolve(r.split(providerKey)[0]) }) }) }) } getDubboInvokeParam() { const param = this.param if (!param) { return '' } switch (typeof param) { case 'string': { return param.split(',').map(v => '\"' + v + '\"').join(',') } case 'object': { return JSON.stringify(param) } } } invokeDubbo() { const telnet = new Telnet() const connectConfig = this.getTelnetConfig() const param = this.getDubboInvokeParam() return new Promise((resolve, reject) => { this .getServicePath() .then(provider => { let command = `invoke ${provider}.${this.method}(${param})` console.log(command) telnet.connect(connectConfig) .then(_ => { console.log(command) telnet.send(command) .then(r => { resolve(r) }) }) }) .catch(r => { reject(r) }) }) } main() { const body = { "host": "192.168.27.4", "port": "20882", "service": "CardValidateFacade", "method": "findAllCustValidate", "param": { "class": "com.smy.pcs.dto.CustCardValidateRequest", "custNo": "168880612898" } } console.log(this.invokeDubbo(body)) } } (() => { app.get('/api', async (req, resp) => { try { let form = req.body new TelnetTool(form).invokeDubbo() .then(r => { resp.send(r) }) .catch(r => { resp.send(r) }) } catch (err) { resp.send(err.stack) } }) })() console.log("程序开始运行,端口3000:") app.listen(3000)