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文件夾下有各種語言的例子。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM