RPC_简洁版
1、定义服务接口
public interface RpcService { int printHello(int msg); }
2、定义服务接口实现类
public class RpcInterfaceImpl implements RpcService{ @Override public int printHello(int msg) { return msg; } }
3、定义服务提供者
public class RpcProvider { public static void main(String[] args) throws Exception { RpcService service = new RpcInterfaceImpl();
//对外暴露服务接口 RpcSimple.exportServer(service, 12306); } }
4、定义服务消费者
public class RpcMainTest { public static void main(String[] args) throws Exception{
//远程调用服务 RpcService service = RpcSimple.refer(RpcService.class, "127.0.0.1", 12306); for(int i = 0; i < 10; i++){ System.out.println(service.printHello(i)); Thread.sleep(1000); } } }
5、服务提供中心类
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.net.ServerSocket; import java.net.Socket; /** * simple RPC servicer * @author Tim * */ public class RpcSimple { /** * export server */ public static void exportServer(final Object service, int port) throws Exception{ if(null == service){ throw new IllegalArgumentException("service is not null"); } if(port > 65535 || port < 0){ throw new IllegalArgumentException("port is not between 0 and 65535"); } System.out.println("export service" + service.getClass().getName() + " on port " + port); @SuppressWarnings("resource") ServerSocket serverSocket = new ServerSocket(port); for(;;){ try { final Socket socket = serverSocket.accept(); new Thread(new Runnable(){ @Override public void run(){ try { try { ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); try { /**get object from socket inputstream*/ String methodName = input.readUTF(); Class<?>[] parameterTypes = (Class<?>[]) input.readObject(); Object[] arguments = (Object[]) input.readObject(); ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream()); try { Method method = service.getClass().getMethod(methodName, parameterTypes); Object result = method.invoke(service, arguments); output.writeObject(result); } catch (Exception e) { output.writeObject(e); }finally { output.close(); } }finally { input.close(); } }finally{ socket.close(); } } catch (Exception e) { } } }).start(); } catch (Exception e) { e.printStackTrace(); } } } /** * refer server */ @SuppressWarnings("unchecked") public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception{ if(null == interfaceClass){ throw new IllegalArgumentException("interfaceClass is not null"); } if(!interfaceClass.isInterface()){ throw new IllegalArgumentException(interfaceClass.getName() + "must be interface class"); } if(null == host || host.length() == 0){ throw new IllegalArgumentException("host is null"); } if(port > 65535 || port < 0) { throw new IllegalArgumentException("the port must in 0 - 65535"); } System.out.println("get remote service" + interfaceClass.getName() + " from server" + host + ": " + port); return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable { Socket socket = new Socket(host, port); try { ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream()); try { output.writeUTF(method.getName()); output.writeObject(method.getParameterTypes()); output.writeObject(arguments); ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); try { Object result = input.readObject(); if(result instanceof Throwable){ throw (Throwable)result; } return result; } finally{ input.close(); } } finally{ output.close(); } } finally{ socket.close(); } } }); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?