深圳企业网站建设公司,上海中学官网电脑版,福州网站seo优化公司,国外flash网站1.JSON-RPC说明
JSON-RPC是一个无状态且轻量级的远程过程调用(RPC)协议。 它主要定义了一些数据结构及其相关的处理规则。 它运行时可以基于tcp(socket),http等不同的消息传输方式#xff0c; 即它不关心底层传输方式的细节。 它使用JSON#xff08;RFC 4627#xff09;作为…1.JSON-RPC说明
JSON-RPC是一个无状态且轻量级的远程过程调用(RPC)协议。 它主要定义了一些数据结构及其相关的处理规则。 它运行时可以基于tcp(socket),http等不同的消息传输方式 即它不关心底层传输方式的细节。 它使用JSONRFC 4627作为数据格式。 目前版本为JSON-RPC 2.0。
2.JSON-RPC over TCP说明
JSON-RPC over TCP是一种基于TCP协议的传输方式实现JSON-RPC 它的底层通过TCP套接字在客户端和服务器之间传输JSON格式的数据。
JSON-RPC over TCP的优势
双向通信TCP套接字允许客户端和服务器之间进行双向通信使得数据的传输更加灵活和高效。JSON格式使用JSON格式进行数据传输使得数据的解析和处理更加简单和方便。TCP协议基于TCP协议的实现保证了数据的可靠性和稳定性。适用场景适用于各种场景包括实时通信、远程控制、数据同步等。
3.jsonrpc4j开源库
jsonrpc4j是java编程语言实现的JSON-RPC开源工具库。 它使用Jackson库将java对象与json对象(以及与JSON-RPC相关的其他东西)进行转换。 它能提供如下多种特性
Streaming server (InputStream \ OutputStream)HTTP Server (HttpServletRequest \ HttpServletResponse)Portlet Server (ResourceRequest \ ResourceResponse)Socket Server (StreamServer)Integration with the Spring Framework (RemoteExporter)Streaming clientHTTP clientDynamic client proxiesAnnotations supportCustom error resolvingComposite services
Maven依赖
dependencygroupIdcom.github.briandilley.jsonrpc4j/groupIdartifactIdjsonrpc4j/artifactIdversion1.6/version
/dependency4.JSON-RPC over TCP Demo
下面基于jsonrpc4j开源库提供的 Socket Server (StreamServer)功能, 实现一个JSON-RPC over TCP Demo 服务端使用Socket Server提供JSON-RPC服务 客户端使用Socket Client访问JSON-RPC服务 客户端和服务端之间使用TCP协议通信。
5.JSON-RPC服务端
新建Maven工程添加pom.xml依赖 dependenciesdependencygroupIdcom.github.briandilley.jsonrpc4j/groupIdartifactIdjsonrpc4j/artifactIdversion1.6/version/dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-annotations/artifactIdversion2.16.0/version/dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-core/artifactIdversion2.16.0/version/dependencydependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.16.0/version/dependencydependencygroupIdorg.apache.tomcat.embed/groupIdartifactIdtomcat-embed-core/artifactIdversion9.0.36/version/dependencydependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter/artifactIdversion5.8.0/versionscopetest/scope/dependency/dependencies新建业务接口MathCalcService.java, 用于对外提供RPC服务
package com.ai.json.rpc.service;public interface MathCalcService {int add(int a, int b);int subtract(int a, int b);
}新建业务实现类MathCalcServiceImpl.java
package com.ai.json.rpc.service.impl;import com.ai.json.rpc.service.MathCalcService;public class MathCalcServiceImpl implements MathCalcService {Overridepublic int add(int a, int b) {return a b;}Overridepublic int subtract(int a, int b) {return a - b;}
}新建服务端启动类JsonRpcStreamServer.java
package com.ai.json.rpc;import java.net.ServerSocket;import javax.net.ServerSocketFactory;import com.ai.json.rpc.service.MathCalcService;
import com.ai.json.rpc.service.impl.MathCalcServiceImpl;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.googlecode.jsonrpc4j.StreamServer;/*** 启动JSON RPC服务端,基于Socket对外提供服务*/
public class JsonRpcStreamServer {public static void main(String[] args) throws Exception {// 在本地8080端口进行监听ServerSocket serverSocket ServerSocketFactory.getDefault().createServerSocket(8080);JsonRpcServer jsonRpcServer new JsonRpcServer(new MathCalcServiceImpl(), MathCalcService.class);// 服务端处理线程池中的最大线程为8个StreamServer server new StreamServer(jsonRpcServer, 8, serverSocket);server.start();}
}6.JSON-RPC客户端
新建客户端测试类JsonRpcServerTest.java:
package com.ai.json.rpc;import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;import org.junit.jupiter.api.Test;import com.googlecode.jsonrpc4j.JsonRpcClient;public class JsonRpcServerTest {/*** 基于Socket的客户端调用,直接使用JsonRpcClient对应服务端JsonRpcStreamServer*/Testpublic void testClient2ServerBySocket() throws Throwable {// 打开socket客户端连接String serverIP localhost;int serverPort 8080;Socket socket new Socket(serverIP, serverPort);// 打开JsonRpc客户端JsonRpcClient client new JsonRpcClient();OutputStream output socket.getOutputStream();InputStream input socket.getInputStream();// 调用服务端的add方法此时不需要目的URL,只需要知道方法名Integer result1 client.invokeAndReadResponse(add, new Object[] { 3, 5 }, Integer.class, output, input);// 输出8System.out.println(result1);// 调用服务端的subtract方法Integer result2 client.invokeAndReadResponse(subtract, new Object[] { 10, 5 }, Integer.class, output,input);// 输出5System.out.println(result2);// 关闭socket.close();}}7.运行测试
首先启动服务端 然后运行客户端 输出如下结果 表示JSON RPC调用成功。
服务端输出:
16:18:26.864 [main] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.init(JsonRpcBasicServer.java:109)] - created server for interface interface com.ai.json.rpc.service.MathCalcService with handler class com.ai.json.rpc.service.impl.MathCalcServiceImpl
16:18:26.869 [main] DEBUG [com.googlecode.jsonrpc4j.StreamServer.start(StreamServer.java:93)] - StreamServer starting 0.0.0.0/0.0.0.0:8080
16:18:44.960 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.StreamServer$Server.run(StreamServer.java:215)] - Client connected: 127.0.0.1:7924
16:18:46.335 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.handleObject(JsonRpcBasicServer.java:420)] - Request: {id:1627298179,jsonrpc:2.0,method:add,params:[3,5]}
16:18:46.378 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.invoke(JsonRpcBasicServer.java:572)] - Invoking method: add with args [3, 5]
16:18:46.383 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.invoke(JsonRpcBasicServer.java:589)] - Invoked method: add, result 8
16:18:46.386 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.writeAndFlushValue(JsonRpcBasicServer.java:974)] - Response: {jsonrpc:2.0,id:1627298179,result:8}
16:18:46.396 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.handleObject(JsonRpcBasicServer.java:420)] - Request: {id:994082771,jsonrpc:2.0,method:subtract,params:[10,5]}
16:18:46.397 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.invoke(JsonRpcBasicServer.java:572)] - Invoking method: subtract with args [10, 5]
16:18:46.397 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.invoke(JsonRpcBasicServer.java:589)] - Invoked method: subtract, result 5
16:18:46.397 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.writeAndFlushValue(JsonRpcBasicServer.java:974)] - Response: {jsonrpc:2.0,id:994082771,result:5}
16:18:46.420 [pool-2-thread-1] DEBUG [com.googlecode.jsonrpc4j.StreamServer$Server.run(StreamServer.java:250)] - Client disconnected: 127.0.0.1:7924客户端输出
16:54:35.778 [main] DEBUG [com.googlecode.jsonrpc4j.JsonRpcClient.internalWriteRequest(JsonRpcClient.java:306)] - Request {id:677962822,jsonrpc:2.0,method:add,params:[3,5]}
16:54:35.801 [main] DEBUG [com.googlecode.jsonrpc4j.JsonRpcClient.readResponseNode(JsonRpcClient.java:313)] - JSON-RPC Response: {jsonrpc:2.0,id:677962822,result:8}
8
16:54:35.806 [main] DEBUG [com.googlecode.jsonrpc4j.JsonRpcClient.internalWriteRequest(JsonRpcClient.java:306)] - Request {id:464162296,jsonrpc:2.0,method:subtract,params:[10,5]}
16:54:35.807 [main] DEBUG [com.googlecode.jsonrpc4j.JsonRpcClient.readResponseNode(JsonRpcClient.java:313)] - JSON-RPC Response: {jsonrpc:2.0,id:464162296,result:5}
5