标签:intern NPU throws 对象 getc exce date loader res
package com.wu; public interface RpcProtocol { void export(int port); Object refer(Class inrerfaceClass, String host, int port); }
package com.wu; import java.io.IOException; import java.io.ObjectInput; 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.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import static com.sun.jmx.remote.internal.IIOPHelper.connect; /** * @ClassName RpcCore * @Description TODO * @Author Administrator * @Date 2019/5/18 16:39 * @Version 1.0 **/ public class RpcCore implements RpcProtocol { private Socket socket; private ObjectOutputStream objectOutputStream; private ObjectInputStream objectInputStream; private ServerSocket serverSocket; private Map<String, List<Object>> services = new ConcurrentHashMap<>(); private Map<String, Map<String, Object>> interfaceAtrributes = new ConcurrentHashMap<>(); @Override public void export(int port) { start(port); } private void start(int port) { try { serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("localhost", port)); init(); } catch (Exception e) { e.printStackTrace(); } while(true) { System.out.println("server has started success port is ->" + port); Socket socket = null; try { socket = serverSocket.accept(); new Thread(new Processsor(socket, services)).start(); } catch (Exception e) { e.printStackTrace(); } } } private void init() { RpcDemo rpcDemo = new RpcDemoImplProvider(); String group = "rpcDemo"; String version = "1.0.0"; String fullName = RpcDemo.class.getName()+"&"+group+"&"+version; List<Object> rpcDemoInstances = services.get(fullName); if (rpcDemoInstances==null){ rpcDemoInstances = new ArrayList(); rpcDemoInstances.add(rpcDemo); } services.put(fullName,rpcDemoInstances); } @Override public Object refer(final Class inrerfaceClass, String host, int port) { connect(host, port); return Proxy.newProxyInstance(inrerfaceClass.getClassLoader(), new Class[]{inrerfaceClass}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String interfaceName = inrerfaceClass.getName(); String fullName = (String)interfaceAtrributes.get(interfaceName).get("fullName"); return get(fullName, method, args); } }); } /** * connect * @param host * @param port */ public void connect(String host, int port) { try { storeInterface(); socket = new Socket(); socket.connect(new InetSocketAddress(host, port)); objectOutputStream=new ObjectOutputStream(socket.getOutputStream()); objectInputStream=new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { e.printStackTrace(); } } private void storeInterface(){ String group="rpcDemo"; String version="1.0.0"; String fullName=RpcDemo.class.getName()+"&"+group+"&"+version; Map<String,Object> attributes=interfaceAtrributes.get(fullName); if (attributes == null){ attributes=new ConcurrentHashMap(100); attributes.put("group",group); attributes.put("version",version); attributes.put("fullName",fullName); } interfaceAtrributes.put(RpcDemo.class.getName(),attributes); } /** * get Object * @param interfaceFullName * @param method * @param parames * @return */ private Object get(String interfaceFullName, Method method, Object[] parames) { Object result = null; try { objectOutputStream.writeUTF(interfaceFullName); objectOutputStream.writeUTF(method.getName()); objectOutputStream.writeObject(method.getParameterTypes()); objectOutputStream.writeObject(parames); objectOutputStream.flush(); result = objectInputStream.readObject(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(objectInputStream != null) { objectInputStream = null; } if(objectOutputStream != null) { objectOutputStream = null; } } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 内部类 */ private class Processsor implements Runnable { private Socket socket; private ObjectInputStream objectInputStream; private ObjectOutputStream objectOutputStream; private Map<String, List<Object>> services; private Processsor(Socket socket, Map<String, List<Object>> services) { this.socket = socket; this.services = services; } @Override public void run() { try { objectInputStream = new ObjectInputStream(socket.getInputStream()); objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); String interfaceFullName = objectInputStream.readUTF(); String methodName = objectInputStream.readUTF(); Class[] parameTypes = (Class[])objectInputStream.readObject(); Object[] objects = (Object[])objectInputStream.readObject(); String interfaceName=interfaceFullName.split("&")[0]; // 反射创建对象 Class service = Class.forName(interfaceName); Method method = service.getMethod(methodName, parameTypes); Object instances = services.get(interfaceFullName).get(0); Object result = method.invoke(instances, objects); objectOutputStream.writeObject(result); objectOutputStream.flush(); objectOutputStream.close(); objectInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } }
package com.wu; import java.io.Serializable; /** * @ClassName RpcDemo * @Description TODO * @Author Administrator * @Date 2019/5/18 17:08 * @Version 1.0 **/ public interface RpcDemo { Student getStudent(Integer id, String name); class Student implements Serializable { public long id; public String name; public int age; public boolean man; public Student(long id, String name, int age, boolean man) { this.id = id; this.name = name; this.age = age; this.man = man; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isMan() { return man; } public void setMan(boolean man) { this.man = man; } @Override public String toString() { return "Student{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", age=" + age + ", man=" + man + ‘}‘; } } }
package com.wu; /** * @ClassName RpcDemoImplProvider * @Description TODO * @Author Administrator * @Date 2019/5/18 17:10 * @Version 1.0 **/ public class RpcDemoImplProvider implements RpcDemo { @Override public Student getStudent(Integer id, String name) { return new Student(1234,"zhangsan",20,true); } public static void main(String[] args) { int port = 8087; RpcCore rpcCore = new RpcCore(); rpcCore.export(port); } }
package com.wu; /** * @ClassName RpcDemoConsumer * @Description TODO * @Author Administrator * @Date 2019/5/18 17:12 * @Version 1.0 **/ public class RpcDemoConsumer { public static void main(String[] args) { RpcCore rpcCore = new RpcCore(); RpcDemo rpcDemo = (RpcDemo)rpcCore.refer(RpcDemo.class, "127.0.0.1", 8087); System.out.println("远程调用成功"); System.out.println("返回的结果是---->"+rpcDemo.getStudent(111,"zhangsan")); // 先启动RpcDemoImplProvider // 启动RpcDemoConsumer } }
标签:intern NPU throws 对象 getc exce date loader res
原文地址:https://www.cnblogs.com/wylwyl/p/10886446.html