jsonrpc4j 提供StreamServer类,以下是服务端和客户端基本示例
实现业务接口
package demo;
import com.googlecode.jsonrpc4j.JsonRpcMethod;
import com.googlecode.jsonrpc4j.JsonRpcParam;
/**
*
* @author Administrator
*
*/
public interface ExampleServerAPI {
@JsonRpcMethod("example.multiplier")
int multiplier(@JsonRpcParam(value = "a") int a, @JsonRpcParam(value = "b") int b);
}
实现业务类
package demo;
public class ExampleServerAPIImpl implements ExampleServerAPI {
public int multiplier(int a, int b) {
return a * b;
}
}
执行类
模拟 server 端 和 client 在main() 入口启动调用
package demo;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ServerSocketFactory;
import com.googlecode.jsonrpc4j.JsonRpcClient;
import com.googlecode.jsonrpc4j.JsonRpcMultiServer;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.googlecode.jsonrpc4j.ProxyUtil;
import com.googlecode.jsonrpc4j.StreamServer;
import server.BlogBean;
import server.BlogService;
import server.BlogServiceImpl;
import server.ExampleServerAPI;
import server.ExampleServerAPIImpl;
/**
* RPC 框架中很重要两个角色 Server端,和client端
*
* @author Administrator
*
*/
public class StreamServerDemo {
public static void main(String[] args) throws Throwable {
StreamServerDemo f = new StreamServerDemo();
f.server();
f.client();
}
/**
* Server端
*
* @throws IOException
*/
public void server() throws IOException {
// 定义server1:公共Server,每次注册一个Service
// JsonRpcBasicServer jsonRpcServer = new JsonRpcBasicServer(new ExampleServerAPIImpl(), ExampleServerAPI.class);
// 定义server2:MultiServer可以注册多个Service
// JsonRpcMultiServer jsonRpcServer = new JsonRpcMultiServer();
// jsonRpcServer.addService("example", new ExampleServerAPIImpl(), ExampleServerAPI.class);
// 定义server3:综合使用
ExampleServerAPIImpl exampleService = new ExampleServerAPIImpl();
BlogServiceImpl blogService = new BlogServiceImpl();
Object compositeService = ProxyUtil.createCompositeServiceProxy(
this.getClass().getClassLoader(),
new Object[] { exampleService, blogService},
new Class<?>[] { ExampleServerAPI.class, BlogService.class},
true);
JsonRpcServer jsonRpcServer = new JsonRpcServer(compositeService);
// 提供Socket通信器
InetAddress bindAddress = InetAddress.getByName("127.0.0.1");
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(8089, 0, bindAddress);
StreamServer streamServer = new StreamServer(jsonRpcServer, 5, serverSocket);
streamServer.start();
}
/**
* Client端
*
* @throws Throwable
*/
public void client() throws Throwable {
InetAddress bindAddress = InetAddress.getByName("127.0.0.1");
Socket socket = new Socket(bindAddress, 8089);
JsonRpcClient jsonRpcClient = new JsonRpcClient();
// client调用方法一;通过ProxyUtil 代理生成接口类客户端
// ExampleServerAPI exampleServerAPI =
// ProxyUtil.createClientProxy(getClass().getClassLoader(),ExampleServerAPI.class,
// jsonRpcClient, socket);
// int a = exampleServerAPI.multiplier(3, 4);
// client调用方法二:jsonRpcClient.invoke 直接反向调用目标
// 传参方法一:数据组传参结构
// Integer[] params = new Integer[2];
// str_array[0] = 3;
// str_array[1] = 4;
// 传参方法二:key-value传参结构,这个前提在接口参数形参标注jsonrpc-params
Map<String, Object> params = new HashMap<String, Object>();
params.put("a", 3);
params.put("b", 3);
Integer res = jsonRpcClient.invokeAndReadResponse("example.multiplier", params, Integer.class,
socket.getOutputStream(), socket.getInputStream());
System.out.println(res);
//传参方法三:通过ProxyUtil 客户端调用
BlogService blogService = ProxyUtil.createClientProxy(
getClass().getClassLoader(),
BlogService.class,
jsonRpcClient,socket);
List<BlogBean> list = blogService.list();
for (BlogBean blogBean : list) {
System.out.println(blogBean.getContent());
}
}
}
输出
2018-01-05 17:27:22||(JsonRpcBasicServer.java)||<init>||115||created server for interface interface demo.ExampleServerAPI with handler class demo.ExampleServerAPIImpl
2018-01-05 17:27:22||(StreamServer.java)||start||93||StreamServer starting /127.0.0.1:8089
2018-01-05 17:27:22||(StreamServer.java)||run||215||Client connected: 127.0.0.1:56918
2018-01-05 17:27:23||(JsonRpcClient.java)||internalWriteRequest||292||Request {"id":"533805557","jsonrpc":"2.0","method":"example.multiplier","params":{"a":3,"b":4}}
2018-01-05 17:27:23||(JsonRpcBasicServer.java)||handleObject||329||Request: {"id":"533805557","jsonrpc":"2.0","method":"example.multiplier","params":{"a":3,"b":4}}
2018-01-05 17:27:23||(JsonRpcBasicServer.java)||invoke||462||Invoking method: multiplier with args [3, 4]
2018-01-05 17:27:23||(JsonRpcBasicServer.java)||invoke||468||Invoked method: multiplier, result 12
2018-01-05 17:27:23||(JsonRpcBasicServer.java)||writeAndFlushValue||752||Response: {"jsonrpc":"2.0","id":"533805557","result":12}
2018-01-05 17:27:23||(JsonRpcClient.java)||readResponseNode||299||JSON-PRC Response: {"jsonrpc":"2.0","id":"533805557","result":12}
12