Nodejs accesses the Java service through thrift, and nodejsthrift

Source: Internet
Author: User

Nodejs accesses the Java service through thrift, and nodejsthrift

In the previous article, Java is used as the thrift client and server. Next, we will use nodejs as the client. For details about how to install Nodejs, visit http://www.cnblogs.com/xucheng/p/3988835.htmlnodejs.

1. Import thrift.exe into the Directory and execute thrift-0.9.2.exe-gen js: node hello. thrift compilation hello. thrift to generate the nodejs implementation file.

2. Enter the generated gen-nodejs directory in the cmd window, use npm install thrift to install the thrift module of nodejs, and add a node_modules directory after installation.

3. Create a js file as the thrift client. The content is as follows:

// Introduce the thrift Module

Var thrift = require ('thrift ');

// Add the hello service definition file in the same path ./

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

Ttypes = require ('./hello_types ');

// Create a connection and a client

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

Client = thrift. createClient (Hello, connection );

// Connection

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

Console. error (err );

});

// Call the helloString Function

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

4. Start the Java server program in the previous article and run nodejsclient. js with the node command. The console output is [object Promise]. Here, js regards the string returned by Java as an object.

5. Of course, there are examples of various languages in the lib folder of thrift.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.