简易RPC
暴露服务:
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的更多相关文章
- 简易RPC框架-心跳与重连机制
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 简易RPC框架-客户端限流配置
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 简易RPC框架-SPI
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 自行实现一个简易RPC框架
10分钟写一个RPC框架 1.RpcFramework package com.alibaba.study.rpc.framework; import java.io.ObjectInputStrea ...
- 简易RPC框架-学习使用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 简易RPC框架-私有协议栈
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 简易RPC框架-过滤器机制
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 简易RPC框架-上下文
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 简易RPC框架-代理
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 简易RPC框架-熔断降级机制
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
随机推荐
- FreeMarker js 获取后台设置的request、session
使用Request里的Attribute值最简单的方法就是直接${AttributeName}或者安全一点:${AttributeName!"default Value"} 1.取 ...
- java_方法引用
什么是方法引用? 个人简述方法引用: 方法引用主要是针对已经有的方法来让目前的编程更加简洁 当我们想要使用一个接口的子类的时候,子类需要重写这个接口中的抽象方法, 被重写的这个方法参数列表固定,返回值 ...
- oracle学习笔记(六) JDBC使用
JDBC使用 1. 导包 直接使用IDEA导入依赖包即可 新建一个lib,把jar包放在这里 2. 加载驱动 Class.forName("oracle.jdbc.driver.Oracle ...
- java类与对象(属性,方法)的使用
---恢复内容开始--- 类和对象是java编程中很重要的应该面向对象的一课,实际上可以将类看作对象的载体,它定义了对象所具有的功能.Java是面向对象的语言,因此掌握类与对象是学习Java语言的基础 ...
- Python二级-----------程序冲刺4
1. 根据输入正整数 n,作为财务数据,输出一个宽度为 20 字符,n 右对齐显示,带千位分隔符的效果,使用减号字符“-”填充.如果输入正整数超过 20 位,则按照真实长度输出.提示代码如下: ...
- java自定义连接池
1.java自定义连接池 1.1连接池的概念: 实际开发中"获取连接"或“释放资源”是非常消耗系统资源的两个过程,为了姐姐此类性能问题,通常情况我们采用连接池技术来贡献连接Conn ...
- div在另一个div居中对齐
position:fixed; top:0; right:0; left:0; bottom:0; margin:auto;
- 微信小程序(基本知识点)
创建页面的两种方式: 1.通过创建文件夹的方式创建(.wxml/.wxss/.json/.js/) 2.在app.json的"pages": []中添加路径"pa ...
- intellij idea 2017和Jprofiler 10的集成 报错问题
本来想用Jprofiler来分析一下自己写的Java项目,以提高代码执行效率和自己的编码能力.结果,官网和网上很多帖子都写了点出session->IDE integrations->选择i ...
- rocketmq广播消息
发布与模式实现.广播就是向一个主题的所有订阅者发送同一条消息. 在发送消息的时候和普通的消息并与不同之处,只是在消费端做一些配置即可. Consumer消息消费 public class Broadc ...