上一篇文章中实现了用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文件夹下有各种语言的例子。

相关文章:

  • 2021-04-19
  • 2021-05-02
  • 2021-06-24
  • 2021-11-13
  • 2021-08-13
  • 2021-07-10
  • 2021-07-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-09-03
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案