暴露服务:

package com.saiarea;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket; public class RpcService {
public static void export(int port) throws Exception {
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Export service on port " + port);
ServerSocket server = new ServerSocket(port);
for(;;) {
try {
final Socket socket = server.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
try {
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
String className = input.readUTF();
String methodName = input.readUTF();
Class<?>[] parameterTypes = (Class<?>[])input.readObject();
Object[] arguments = (Object[])input.readObject();
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
Object service = Class.forName(className+"Impl").newInstance();
Method method = service.getClass().getMethod(methodName, parameterTypes);
Object result = method.invoke(service, arguments);
output.writeObject(result);
} catch (Throwable t) {
output.writeObject(t);
} finally {
output.close();
}
} finally {
input.close();
}
} finally {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
} public static void main(String[] args) throws Exception{
Test test = new TestImpl();
RpcService.export(8081);
}
}

引用服务

 package com.saiarea;

 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.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class RpcRequestService {
static String host = "";
static int port = 0; public static <T> T proxyFactory(final Class<T> myClass) throws Exception {
List<Class> interfacesList = new ArrayList(myClass.getInterfaces().length + 1);
interfacesList.addAll(Arrays.asList(myClass.getInterfaces()));
interfacesList.add(myClass);
Class[] classes = new Class[myClass.getInterfaces().length + 1];
interfacesList.toArray(classes);
Object proxy = Proxy.newProxyInstance(RpcRequestService.class.getClassLoader(), classes, new InvocationHandler() {
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(myClass.getName());
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();
}
}
});
System.out.println("test:" + proxy);
return (T) proxy;
} public static void main(String[] args) throws Exception {
host = "127.0.0.1";
port = 8081;
Test service = proxyFactory(Test.class);
Long hello = service.testRpc("测试动态加载代理");
System.out.println("我收到了," + hello);
}
}

简易RPC的更多相关文章

  1. 简易RPC框架-心跳与重连机制

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. 简易RPC框架-客户端限流配置

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  3. 简易RPC框架-SPI

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. 自行实现一个简易RPC框架

    10分钟写一个RPC框架 1.RpcFramework package com.alibaba.study.rpc.framework; import java.io.ObjectInputStrea ...

  5. 简易RPC框架-学习使用

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. 简易RPC框架-私有协议栈

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. 简易RPC框架-过滤器机制

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. 简易RPC框架-上下文

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  9. 简易RPC框架-代理

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  10. 简易RPC框架-熔断降级机制

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

随机推荐

  1. vue 使用定时器setInterval

    来自:https://www.jianshu.com/p/180957762852 侵删 beforeMount() { //车辆进出设置定时器,每3秒刷新一次 var self = this; cl ...

  2. C# 文件绝对路径与相对路径的转换

    class Program { const string CONFIG_PATH = @"C:\SoftWare\Config.xml"; const string IMAGE_P ...

  3. 项目实战工具类(二):ZipUtils(压缩/解压缩文件相关)

    import android.content.Context; import android.util.Log; import java.io.File; import java.io.FileInp ...

  4. vue的表单编辑删除,保存取消功能

    过年回来第一篇博客,可能说的不是很清楚,而且心情可能也不是特别的high,虽然今天是元宵,我还在办公室11.30在加班,但就是想把写过的代码记下来,怕以后可能真的忘了.(心将塞未塞,欲塞未满) VUE ...

  5. RowKey设计之单调递增行键/时序数据

    ​在一个集群中,一个导入数据的进程锁住不动,所有的client都在等待一个region (因而也就是一个单个节点),过了一会后,变成了下一个region…​如果使用了单调递增 或者时序的key便会造成 ...

  6. 神经网络MPLClassifier分类

    代码: # -*- coding: utf-8 -*- """ Created on Fri Aug 24 14:38:56 2018 @author: zhen &qu ...

  7. MySQL:select command denied to user for table 'proc'案例

    使用EMS MySQL Manager Pro(3.4.0.1)连接MySQL 5.6.20时,报错:"SELECT command denied to user xxx@xxx.xxx.x ...

  8. linux 系统shell运行程序不退出

    如果通过ssh远程连接到linux系统终端,在shell下执行程序.假如程序名称为app,且程序本身会一直执行不退出,程序执行需要参数文件paramfile. 当我们用 ./app paramfile ...

  9. Mysql原理与优化

    原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...

  10. Spring Boot自定义Banner

    在2016年的最后一天,借用Spring Boot的Banner向各位程序猿同仁们问候一声:Happy New Year. 接下来我们就来介绍一下这个轻松愉快的自定义banner功能.实现的方式非常简 ...