• Nodejs通过thrift访问Java服务


    上一篇文章中实现了用Java作为thrift客户端和服务端。接下来我们用nodejs作为客户端访问一下。Nodejs的安装可以查看http://www.cnblogs.com/xucheng/p/3988835.htmlnodejs的介绍。

    1、进入thrift.exe所在目录执行thrift-0.9.2.exe   –gen  js:node hello.thrift编译hello.thrift生成nodejs的实现文件。

    2、在cmd窗口进入生成的gen-nodejs目录,使用npm  install  thrift安装nodejs的thrift模块,安装完多了一个node_modules目录。

    3、新建一个js文件作为thrift的客户端。内容如下:

    //引入thrift模块

    var thrift = require('thrift');

    //引入hello服务定义文件在同一路径下也要加 ./

    var Hello = require('./Hello.js'),

       ttypes = require('./hello_types');

    //创建连接和客户端

    var connection = thrift.createConnection('localhost', 19090),

      client = thrift.createClient(Hello, connection);

    //连接

    connection.on('error', function(err) {

      console.error(err);

    });

    //调用helloString函数

    console.log(client.helloString('tomdog').toString());

    4、启动上一篇文章中Java server程序,用node指令运行nodejsclient.js,看到控制台输出:[object Promise]。在这里js把Java返回的string当成object。

    5、当然在thrift的lib文件夹下有各种语言的例子。

    本文章属于本人和博客园所有,如有转载请注明出自http://www.cnblogs.com/xucheng
  • 相关阅读:
    Vasya and Endless Credits CodeForces
    Dreamoon and Strings CodeForces
    Online Meeting CodeForces
    数塔取数 基础dp
    1001 数组中和等于K的数对 1090 3个数和为0
    1091 线段的重叠
    51nod 最小周长
    走格子 51nod
    1289 大鱼吃小鱼
    POJ 1979 Red and Black
  • 原文地址:https://www.cnblogs.com/xucheng/p/4160680.html
Copyright © 2020-2023  润新知