JSON-RPC Lightweight Remote Call protocol introduction and use

Source: Internet
Author: User

JSON-RPC Lightweight Remote Call protocol introduction and use

Folder

Technology Brief Introduction 1

I. JSON-RPC description of the Agreement 1

Second, Json-rpc call simple Demo Sample 1

2.1. Server-side Java call Demo Sample 1

2.2. Javaclient Call Demo Sample 2

2.3. Phpclient Call Demo Sample 2

2.3. Javascriptclient Call Demo Sample 2

2.4. Call the direct GET request 2

Iii. Summary of Json-rpc 3

Documentation 3

Technology Brief Introduction

JSON-RPC is a JSON-based, cross-language remote invocation protocol. Smaller than XML-RPC, WebService and other text-based protocol data transfer, the Hessian, JAVA-RPC and other binary protocols are easy to debug, implement and extend, which is a very good remote call 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. It can also be seamlessly assembled with spring and is suitable for spring-based project development.

I. Descriptive narrative of JSON-RPC agreement

The JSON-RPC protocol is easy to initiate a remote call to the service-side data transfer format such as the following:

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

Description of the parameters:

Method: The name of the call

Params: The number of parameters passed in the method. If no argument 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     
 } 

Description of the parameters:

Result: The method returns a value. If no return value is returned. Then NULL 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 easy, small. Easy to implement in all languages.

Second, Json-rpc simple demonstration Sample2.1. Server-side Java call Demo sample

Jsonrpc4jserver-Side Java Demo sample:

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. Javaclient Call Demo Sample

JSONRPC4J's Javaclient call Demo sample:

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. Phpclient Call Demo Sample

Demo sample of Phpclient 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. Javascriptclient Call Demo Sample

Demo sample of Javascriptclient invocation based on JSONRPCJS:

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 matter what the client. Just manually stitching the parameters for remote invocation, request URLs such as the following:

METHOD=GETSYSTEMPROPERTIES&ID=3325235235235&PARAMS=JTVIJTVK ">http://127.0.0.1:8080/index.json? METHOD=GETSYSTEMPROPERTIES&ID=3325235235235&PARAMS=JTVIJTVK

Description of the parameters:

Method: Methods Name

Params: Call parameter. JSON array format [], the parameters are URL-encoded before base64 encoding

ID: Call identifier, random value.

iii. Summary of Json-rpc

JSON-RPC is a very lightweight, cross-language remote invocation protocol. Simple to implement and use.

With just dozens of lines of code, you can implement a remote call to the client. facilitates the implementation of the language extension client.

Server side has PHP, Java, Python, Ruby,. NET and other languages implemented, is a very good and lightweight remote call protocol.

documentation for references

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

JSON-RPC Lightweight Remote Call protocol introduction and use

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.