基于jsonrpc4j实现JSON-RPC over TCP

1.JSON-RPC说明

JSON-RPC是一个无状态且轻量级的远程过程调用(RPC)协议。
它主要定义了一些数据结构及其相关的处理规则。
它运行时可以基于tcp(socket),http等不同的消息传输方式,
即它不关心底层传输方式的细节。
它使用JSON(RFC 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 client
  • HTTP client
  • Dynamic client proxies
  • Annotations support
  • Custom error resolving
  • Composite services

Maven依赖:

<dependency>
	<groupId>com.github.briandilley.jsonrpc4jgroupId>
	<artifactId>jsonrpc4jartifactId>
	<version>1.6version>
dependency>

4.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依赖:

    <dependencies>
        <dependency>
            <groupId>com.github.briandilley.jsonrpc4jgroupId>
            <artifactId>jsonrpc4jartifactId>
            <version>1.6version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>2.16.0version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.16.0version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.16.0version>
        dependency>

        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-coreartifactId>
            <version>9.0.36version>
        dependency>

        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiterartifactId>
            <version>5.8.0version>
            <scope>testscope>
        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 {
    @Override
    public int add(int a, int b) {
        return a + b;
    }

    @Override
    public 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
     */
    @Test
    public 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);
        // 输出:8
        System.out.println(result1);

        // 调用服务端的subtract方法
        Integer result2 = client.invokeAndReadResponse("subtract", new Object[] { 10, 5 }, Integer.class, output,
                input);
        // 输出:5
        System.out.println(result2);

        // 关闭
        socket.close();
    }

}

7.运行测试

首先启动服务端,
然后运行客户端,
输出如下结果,
表示JSON RPC调用成功。

服务端输出:

16:18:26.864 [main] DEBUG [com.googlecode.jsonrpc4j.JsonRpcBasicServer.(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

你可能感兴趣的:(json,rpc,tcp/ip)