RPC - 入门

本文的示例代码参考rpc-hprose

目录

  • HTTP

    • Server

    • Client

    • Package

  • TCP

    • Server

    • Client

    • Package

HTTP

Server

vim server-http.js
var hprose = require("hprose");
function hello(name) {
    return "Hello " + name + "!";
}
var server = hprose.Server.create("http://0.0.0.0:8080");
server.addFunction(hello);
server.start();

Client

vim client-http.js
var hprose = require("hprose");
var client = hprose.Client.create("http://127.0.0.1:8080/");
var proxy = client.useService();
proxy.hello("world", function (result) {
    console.log(result);
});
  • Test
cnpm i --save hprose

node server-http.js

node client-http.js # Hello world!

关于跨语言RPC框架hprose详细介绍 可以参考hprose

Package

基于Wireshark抓包如下

rcp-basic-01.png
rcp-basic-02.png

关于抓包工具Wireshark详细介绍 可以参考Wireshark

TCP

Server

vim server-tcp.js
var hprose = require("hprose");
function hello(name) {
    return "Hello " + name + "!";
}
var server = hprose.Server.create("tcp://0.0.0.0:8080");
server.addFunction(hello);
server.start();

Client

vim client-tcp.js
var hprose = require("hprose");
var client = hprose.Client.create("tcp://127.0.0.1:8080/");
var proxy = client.useService();
proxy.hello("world", function (result) {
    console.log(result);
});
  • Test
cnpm i --save hprose

node server-http.js

node client-http.js # Hello world!

Package

基于Wireshark抓包如下

rcp-basic-03.png
rcp-basic-04.png

你可能感兴趣的:(RPC - 入门)