groovy实现JSONRPC

◇◆丶佛笑我妖孽 提交于 2019-12-07 17:45:50

介绍

groovy-jsonrpc JSONRPC的JAVA和GROOVY实现,能够将java类(包括groovy脚本)的公有静态方法通过jsonrpc2.0暴露出来。地址:https://github.com/wonder365/groovy-jsonrpc 。
JSONRPC2.0规范(中文版本):https://github.com/wonder365/groovy-jsonrpc/blob/master/gjsonrpc2/doc/www.jsonrpc.org/specification_cn.html

功能

  • 完全实现JSONRPC2.0(参看测试用例)
  • 支持动态加载groovy脚本
  • 提供工具类:支持jsonrpc的groovyservlet(groovy.jsongrpc.tools.RpcServlet);提供基于netty的nio jsonrpc网络服务(支持java类和groovy脚本)
  • 支持不需要groovy.jar的纯java版本,暴露java类方法给jsonrpc
  • 支持params到标准函数参数的转换(转换 "params":[1,2] 供 方法“ int add(int a, int b)” 调用,非jsonrpc规范)

groovy.jsongrpc.handler.UrlHandler

该类支持从指定groovy文件调用方法(所以调用时必须传入url)。样例:
UrlHandler handler = new UrlHandler();
String url = "test/test.groovy";
String req = "{"jsonrpc":"2.0","id":null,"method":"add","params":[1,2]}";
String rsp = handler.call(url, req);
// rsp : {"jsonrpc":"2.0","id":null,"result":3}
// or
byte[] rsp = handler.call(url, byte[]data)

RPC commands

支持调用如下rpc工具命令:
>{"jsonrpc":"2.0","id":null,"method":"rpc.ls"}
<{"jsonrpc":"2.0","id":null,"result":["subtract","getDate","nullf","donotify","notify_sum","sum","adds","fun1arg","add","echo"]}

>{"jsonrpc":"2.0","id":null,"method":"rpc.ll"}
<{"jsonrpc":"2.0","id":null,"result":{"add":'public static int jsonrpc.dynamic.test.add(int,int)',"adds":'public static int jsonrpc.dynamic.test.adds(java.util.List)',"donotify":'public static java.lang.Object jsonrpc.dynamic.test.donotify()',"echo":'public static java.lang.Object jsonrpc.dynamic.test.echo(java.lang.Object)',"fun1arg":'public static java.lang.Object jsonrpc.dynamic.test.fun1arg(int)',"getDate":'public static java.lang.Object jsonrpc.dynamic.test.getDate()',"notify_sum":'public static java.lang.Object jsonrpc.dynamic.test.notify_sum(java.util.List)',"nullf":'public static java.lang.Object jsonrpc.dynamic.test.nullf()',"subtract":'public static int jsonrpc.dynamic.test.subtract(int,int)',"sum":'public static java.lang.Object jsonrpc.dynamic.test.sum(java.util.List)'}}

>{"jsonrpc":"2.0","id":null,"method":"rpc.all"}
<{"jsonrpc":"2.0","id":null,"result":[{"url":"test/test.groovy","classname":"jsonrpc.dynamic.test","isok":true,"compiletime":1363253990140,"failmsg":""}]}

>{"jsonrpc":"2.0","id":null,"method":"rpc.recompile"}
<{"jsonrpc":"2.0","id":null,"result":true}

groovy.jsongrpc.handler.GMapedHandler


与UrlHandler相类似,但是不需要传入文件路径(url),但是需要注册java类或groovy文件,调用时method也要传入相应的前缀。举例: register "java.lang.Math" to "my.math.",可以通过 "my.math.bas" 调用Math.abs by method "my.math.bas"; register "test/test.groovy" to "my.testg.", 可以通过 "my.testg.methodname" 调用 test.groovy 中的方法。

样例

GMapedHandler handler = new GMapedHandler();
// register a groovy script
handler.register("groovyclass.", "test/test.groovy", true);
// register a java class
handler.register("javaclass.", "test.class", false);
// call groovy
String req = "{"jsonrpc":"2.0","id":null,"method":"groovyclass.add","params":[1,2]}";
String rsp = handler.call(req);
// call java
String req = "{"jsonrpc":"2.0","id":null,"method":"javaclass.add","params":[1,2]}";
String rsp = handler.call(req);

RPC commands

>{"jsonrpc":"2.0","id":null,"method":"rpc.register","params":["groovyclass.2","test/test.groovy",true]}
<{"jsonrpc":"2.0","id":null,"result":true}

groovy.jsongrpc.tools.RpcServlet


支持 JSONRPC 的 GroovyServlet, 当 requst.ContentType 包含 "json"时将调用 JSONRPC。(当前只支持UTF-8 encoding,前边的类也是一样)

groovy.jsongrpc.tools.RpcServerG


支持暴露javaclass和groovyclass的NIO服务器,基于netty



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!