暴露服务:

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. 使用curl制作简易百度搜索

    这几天研究了一下php中的curl类库,做了一个简单的百度搜索,先上代码 <div style="width:200px;height:100px;"> <div ...

  2. web服务器负载均衡与集群基本概念二

    前面已经说过负载均衡的作用是在多个节点之间按照一定的策略(算法)分发网络或计算处理负载.负载均衡可以采用软件和硬件来实现.一般的框架结构可以参考下图.    后台的多个Web节点上面有相同的Web应用 ...

  3. vue input输入框长度限制

    今天在开发登录页时,需要设置登录输入框的长度,输入类型为number <input type="number" maxlength="11" placeh ...

  4. 【工作分解法】IT人,你的工作“轻松”么?

    一.前言 假如读者是一个老板,下面有两位员工,工作难度一样,完成量一样,人品和责任心也一样.一位每天加班加点,废寝忘食的工作:而另外一位每天在座位上喝着咖啡,非常的轻松自如的工作.您会更器重哪一位? ...

  5. MyDAL - .QueryOneAsync() 使用

    索引: 目录索引 一.API 列表 .QueryOneAsync() .QueryOneAsync<M>() 如: .QueryOneAsync<Agent>() , 用于 单 ...

  6. 一:SqlServer中的 CEILING函数和 FLOOR函数以及ROUND()

    例如 1.ROUND() 格式为ROUND(y1,y2,y3) y1:要被四舍五入的数字y2:保留的小数位数 y3:为0,可以不写,y1进行四舍五入,不为0则y1不进入四舍五入,如果y1有值就直接根据 ...

  7. SQL Server 增加链接服务器

    exec sp_addlinkedserver '名称' , '' , 'SQLOLEDB' , '10.102.29.xxx' exec sp_addlinkedsrvlogin '名称' , 'f ...

  8. time-时间模块

    # time模块import time# 时间戳res = time.time() # ***print(res, type(res)) # time.sleep(1) # ***# print(12 ...

  9. 一个简单的以太坊合约让imtoken支持多签

    熟悉比特币和以太坊的人应该都知道,在比特币中有2种类型的地址,1开头的是P2PKH,就是个人地址,3开头的是P2SH,一般是一个多签地址.所以在原生上比特币就支持多签.多签的一个优势就是可以多方对一笔 ...

  10. 云计算openstack共享组件(2)——Memcache 缓存系统

    一.缓存系统 在大型海量并发访问网站及openstack等集群中,对于关系型数据库,尤其是大型关系型数据库,如果对其进行每秒上万次的并发访问,并且每次访问都在一个有上亿条记录的数据表中查询某条记录时, ...