Thrift-based node. JS RPC Service

Source: Internet
Author: User

1. Create the Node_modules file under the node. JS Service, NPM install thrift Download Thrift to the file.

2. Write the IDL file. User.thrift content is as follows:

struct user{

1:string uid,

2:string uname,

3:bool Usex,

4:i16 Uage,

}

Service userservice{

void Add (1:user u),

User get (1:string uid),

}

3. Generate code: Run: Thrift  --gen Js:node User.thrift will generate a Gen-nodejs folder in the current directory, which is the JS code we need to copy the JS code into our project. 4. Create a node. JS service segment and Client service side:  var thrift = require (' thrift ');  var UserService = require ('./gen-nodejs/ Userservice.js '),    ttypes = require ('./gen-nodejs/user_types ');   var users = {}; var server = Thrift.createserver (UserService, {   add:function (user, callback) {    Console.log ("Server Stored: ", User.uname);    Users[user.uid] = user;    callback (); }});  server.listen ( 7911); Console.log (' Server start ');  client: var thrift = require (' thrift ');  var userstorage = require ('./ Gen-nodejs/userstorage.js '),    ttypes = require ('./gen-nodejs/user_types ');  var connection = Thrift.createconnection (' localhost ', 9090),    client = thrift.createclient (userstorage, connection);  var user = new ttypes. UserProfile ({uid:1,                  &NBsp                name: "Mark Slee",                                   BLURB: "I'll find something to put."});  connection.on (' Error ', function (err) {  console.error (err);});  client.store (user, function (err, response) {  if (err) {    Console.error (err); } else {    Console.log ("Client stored:", User.uid);    Client.retrieve (user.uid, function (err, responseuser) {& nbsp     if (err) {        Console.error (err);     } else {      & nbsp Console.log ("Client retrieved:", Responseuser.uid);        connection.end ();     }& nbsp  ); });  can now be tested.

Thrift-based node. JS RPC Service

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.