JSON-RPC Lightweight Remote Call protocol introduction and use

Source: Internet
Author: User

JSON-RPC Lightweight Remote Call protocol introduction and use

Directory

Technical Introduction 1

I. JSON-RPC protocol description 1

Second, Json-rpc call simple Example 1

2.1. Server-side Java invocation Example 1

2.2. Java Client Invocation Example 2

2.3. PHP Client Invocation Example 2

2.3. JavaScript Client Invocation Example 2

2.4. Call the direct GET request 2

Iii. Summary of Json-rpc 3

Reference Document 3

Technical Introduction

Json-rpc is a cross-language remote call protocol based on JSON, which is smaller than XML-RPC, WebService and other text-based protocols, and is very good for debugging, implementing and extending binary protocols such as Hessian and Java-rpc, and is a very excellent remote calling protocol. At present, the mainstream language has JSON-RPC implementation framework, the Java language better JSON-RPC implementation Framework has jsonrpc4j, Jpoxy, Json-rpc. Among the three, jsonrpc4j can be used independently and seamlessly with spring, and is more suitable for spring-based project development.

I. Description of JSON-RPC Agreement

The JSON-RPC protocol is very simple and transmits the data format to the server when initiating a remote invocation as follows:

{"method":"SayHello","params":["Hello json-rpc"],"id" :1}

Parameter description:

Method: The name of the call

Params: Method passed in parameter, if no parameter is passed in []

ID: Call identifier used to mark a remote call procedure

The server receives the call request, processes the method call, and effects the method utility result to the caller; Returns the data format:

 {   
    "result": "Hello JSON-RPC",  
    "error" :   null  ,   
    "id" Span style= "COLOR: #339933" >: 1     
 } 

Parameter description:

Result: The method returns a value that returns NULL if no return value is returned. If an error is called, NULL is returned.

Error: Wrong call, NULL returned without error.

ID: The invocation identifier, consistent with the caller's incoming identifier.

The above is the JSON-RPC protocol specification, very simple, small, easy to implement in various languages.

Second, Json-rpc simple example2.1. Server-side Java invocation example

JSONRPC4J server-side Java example:

Public class helloworldservletextends httpservlet {

      private static Final long serialversionuid = 3638336826344504848L

Private jsonrpcserverrpcservice = null;

@Override

Public void init (servletconfig config)throws servletexception {

Super. Init (config);

rpcservice =new jsonrpcserver (new helloworldservice (), HelloWorldService . class);

}

@Override

protectedvoid Service (httpservletrequest req, HttpServletResponse resp)

throws servletexception, IOException {

Rpcservice. Handle (req, resp);

}

}

2.2. Java Client Invocation Example

Example of jsonrpc4j Java client invocation:

Jsonrpchttpclient client =new jsonrpchttpclient (

New URL ("Http://127.0.0.1:8080/index.json"));

map<string,string>Headers = new hashmap<string,string> ();

headers. Put ("name"," Sword white ");

Client.setheaders (headers);

String properties = Client.invoke ("Getsystemproperties",null, string. class );

System. out. println (properties);

2.3. PHP Client Invocation Example

Example of PHP client invocation based on json-rpc-php:

<?php include ( dirname ( __file__ ). "/lib/client/jsonrpcclient.php" Span style= "Font-family:courier New; Color: #444444 ">);

$client = new jsonrpcclient ( "Http://10.13.49.234:8080/index.json" );

$response=getsystemproperties $client ();

echo$response,result;

?>

2.3. JavaScript Client Invocation Example

Example of Jsonrpcjs-based JavaScript client invocation:

var RPC =new jsonrpc. Jsonrpc (' Http://127.0.0.1:8080/index.json ');

Rpc.call (' getsystemproperties ',function(result) {

alert (result);

});

2.4, direct GET request to call

No need for any client, just manually stitching parameters for remote invocation, the request URL is as follows:

Http://127.0.0.1:8080/index.json?method=getSystemProperties&id=3325235235235&params=JTViJTVk

Parameter description:

Method: Methods Name

Params: Call parameters, JSON array format [], the parameters need to be URL-encoded before base64 encoding

ID: Call identifier, any value.

iii. Summary of Json-rpc

JSON-RPC is a very lightweight, cross-language, remote invocation protocol that is simple to implement and use. With just dozens of lines of code, you can implement a remote calling client that facilitates the implementation of the language extension client. The server side has PHP, Java, Python, Ruby,. NET and other languages implemented, is a very good and lightweight remote call protocol.

Reference Documents

http://code.google.com/p/jsonrpc4j/

Http://json-rpc.org/wiki/implementations

Http://en.wikipedia.org/wiki/JSON-RPC

Https://github.com/gimmi/jsonrpcjs

Http://bitbucket.org/jbg/php-json-rpc

https://github.com/Pozo/json-rpc-php

https://github.com/subutux/json-rpc2php

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.