It is a technology that requests services from remote computer programs through the network without understanding the underlying network
Complete RPC network call process
- Service consumer: call the client stub in the local call mode;
- What is a client stub? Even if the remote method is in the local simulation object, there are also method names and method parameters. After receiving the call, the client stub is responsible for packing the parameters of the method name and method, and sending the packed information to the server through the network.
- After the server receives the message, it hands it to the proxy stub and decodes it to the actual method name and parameter after the server part
- The server stub calls the local actual service on the server according to the decoding result.
- Local service execution, return the result to server stub
- server stub packages the returned result into a message and sends it to the consumer
- client stub receives the message and decodes it
- The consumer gets the final result.
Writing a similar framework needs to solve several unusual problems
- proxy pattern
- Serialization problem uses java's related mechanism, namely Serializable
- Communication issues BIO
- Reflection mechanism of service strength of registration
- At runtime, judge the class of any object
- The object of a class is constructed at runtime
- At runtime, judge the methods and member variables of any class
- At runtime, call the method of any object
- Generate dynamic agent
The following code simply explains the above problems
1. Agent mode
//Implementation of static agent method /** * Interface class */ public interface IGetServer { void choice(String desc); } /** * Implementation class of interface */ public class Receptionist implements IGetServer { public void choice(String desc) { System.out.println(desc); } } /** *proxy class */ public class ProxyServer implements InvocationHandler { private Object receptionist;//Instance of implementation class public ProxyServer(Object receptionist) { this.receptionist = receptionist; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Method executed before executing corresponding method"); //Practical approach Object result = method.invoke(receptionist,args); System.out.println("Methods executed after execution"); return result; } } //----------------------------------- /** *Calling class */ public class Proxyclient { //Call proxy method public static void main(String[] args) { IGetServer ig = (IGetServer) Proxy.newProxyInstance(IGetServer.class.getClassLoader(),new Class[]{IGetServer.class},new ProxyServer(new Receptionist())); ig.choice("Here are the parameters I want to pass in"); } }
2. Serialization
bean implements Se rierizable at the same time
public class SerialBean implements Serializable { private final String name; private final String age; public SerialBean(String name, String age) { this.name = name; this.age = age; } public String getName() { return name; } public String getAge() { return age; } }
It mainly uses how to call serialization and deserialization
public class TestSerial { public static void main(String[] args) { setSer();//serialize getSer(); } //serialize private static void setSer() { SerialBean bean = new SerialBean("Mark","123"); List<String> list= new ArrayList<String>(); list.add("my name"); list.add( "is " ); list.add("mark"); ObjectOutputStream os = null; try { os = new ObjectOutputStream(new FileOutputStream("/Users/weblogic/study/Serial.txt")); os.writeObject(bean); os.writeObject(list); os.close(); } catch (IOException e) { e.printStackTrace(); } } public static void getSer(){ try { ObjectInputStream is = new ObjectInputStream( new FileInputStream("/Users/weblogic/study/Serial.txt")); SerialBean bean = (SerialBean) is.readObject(); System.out.println(bean.getAge()); System.out.println(bean.getName()); List tempList = (List) is.readObject(); for(Iterator iterable = tempList.iterator();iterable.hasNext();){ System.out.println(iterable.next()); } is.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
3. Reflection
public class RefleDemo { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { //Standard usage of instanced objects Servant servant = new Servant(); servant.service("hello|"); // The class object of the full name Class servantClazz = Class.forName("cn.baip.netty.rpc.refle.Servant"); //Get the instance of the class through the class object Servant servant1 = (Servant) servantClass.newInstance(); //Get all the method names Method[] methods = servantClazz.getMethods(); for (Method method :methods){ System.out.println(method.getName());//Name of the method if(method.getName().equals("toString")){ try { System.out.println(method.invoke(servant1,null)); System.out.println("Execution"+method.invoke(servantClazz.newInstance(),null)); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } }