fingerprint-container-node-sdk

0.1.11 • Public • Published

FPC NODE SDK

Run Unit Test

npm install && npm test

Server API

    var FPC = require('fingerprint-container-node-sdk');
    var server = new FPC.Server('127.0.0.1','9090');
    
    // 增加一个rpc handler
    server.addHandler('ServiceName',function(res){
        //回复调用的client
        res.send({
            words: "hello world!"
        });
 
        server.destroy();
    });
    
    // 监听connect事件
    server.on('connect',function(){
        // connect成功后进行服务注册
        server.register();
    });
       
    // 注册成功回调
    server.on('register',function(){
        console.log('register success');
    });

Client API

    var FPC = require('fingerprint-container-node-sdk');
    var client = new FPC.Client('127.0.0.1','9090');
    
    // 监听connect事件
    client.on('connect',function(){
        // connect成功后进行服务注册
        client.register();
    });
    
    // 注册成功回调
    client.on('register',function(){
        // 发送一个event事件给容器
        client.eventRequest('ServiceName', {
            name: 'event'
        });
        
        // 发送一个rpc给容器,timeout=2s, 等待server回复
        client.rpcRequest('ServiceName',{
            name: 'rpc'
        },function(body){
            console.log(body);
            client.destroy(); // 销毁client
        }, function(){
            console.log("timeout");
        },2000);
    });

ClientPool API

    var FPC = require('fingerprint-container-node-sdk');
    var pool = new FPC.ClientPool({
        host: '127.0.0.1',
        port: 8080,
        num: 5, // 启动多少client,默认为5
        name: 'mis', // Server名
        heartbeat: 30*1000 // 心跳检测轮询时间,用来剔除失活的链接
    });
 
    pool.acquire(function(client){
        client.rpcRequest('ServiceName',{
            name: 'rpc'
        },function(body){
            pool.release(client);
            console.log(body);
        }, function(){
            pool.release(client);
            console.log("timeout");
        },2000);
    });

Readme

Keywords

Package Sidebar

Install

npm i fingerprint-container-node-sdk

Weekly Downloads

0

Version

0.1.11

License

MIT

Last publish

Collaborators

  • tcstory