RPC based on TCP protocol

Keywords: Java socket Eclipse JDK

Preface:

Environmental Science:
    windown 10
    Eclipse
   JDK 1.8
 Concept of RPC:
    RPC is a remote procedure call and the basis of distributed web site.

Experiment

SayHelloService.java interface class for specification
SayHelloServiceImpl.java is the implementation class of SayHelloService
Provider.java is the service providing class
Consumer.java is the consumption class of a service

SayHelloService.java

package cn.szxy;

/**
 * Interface
 *
 */
public interface SayHelloService {
    /**
     * Returns a string
     * @param str
     * @return
     */
    public String sayHello(String str);
}

SayHelloServiceImpl.java

package cn.szxy;

/**
 * Implementation class
 *
 */
public class SayHelloServiceImpl implements SayHelloService{

    @Override
    public String sayHello(String str) {
        if(str.equals("Hello")){
            return "Hello Hello";
        }else{
            return "byebye!";
        }
    }
    
}

Service provider class Provider.java

package cn.szxy;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

/**
 *  
 * Service provider based on TCP protocol
 * 
 */
public class Provider{
    
    public static void main(String[] args) throws Exception {
        HashMap<Object, Object> map = new HashMap<>();
        map.put(SayHelloService.class.getName(), new SayHelloServiceImpl());
        
        
        //Create socket object
        ServerSocket server = new ServerSocket(5555);
        System.out.println("Server started....");
        while(true){
            
            //Listen to user requests
            Socket client = server.accept();
            //Get input stream
            ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
            
            String inferName = ois.readUTF();
            String methodName = ois.readUTF();
            Class<?>[] parameterTypes = (Class<?>[]) ois.readObject();
            Object[] arguments = (Object[])ois.readObject();
            
            //Execution call 
            Class<?> serviceClass = Class.forName(inferName);
            Object service = map.get(inferName);
            Method method = serviceClass.getMethod(methodName, parameterTypes);
            //Execute the method and return the result
            Object result = method.invoke(service, arguments);
            
            System.out.println("Results to be sent for processing: "+result);
            
            
            //Get output stream
            ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
            oos.writeObject(result);
            oos.writeUTF("End of call");
            oos.flush();
            ;
        }
    }
}

Service Consumer

package cn.szxy;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Scanner;
/**
 * 
 *   TCP based server consumer
 *   
 *  
 */
public class Consumer{
    
    public static void main(String[] args) throws Exception {
            
            //Get what you need to send
            String interName = SayHelloService.class.getName();
            Method method = SayHelloService.class.getMethod("sayHello", String.class);
            Object[]  argurements = {"Hello"};
            
            //Create socket object
            Socket client = new Socket("127.0.0.1",5555);
            //Get output stream
            ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
            oos.writeUTF(interName);
            oos.writeUTF(method.getName());
            oos.writeObject(method.getParameterTypes());
            oos.writeObject(argurements);
            
            //Refresh the buffer, otherwise there will be a problem that "connection reset connection reset cannot find the resource"
            oos.flush();
            
            //Get input stream
            ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
            Object result = ois.readObject();
            String str = ois.readUTF();
            System.out.println("RPC Remote call results: ");
            System.out.println(result);
            
        
            //Close connection
            client.close();
    }
}

summary

 RPC is based on the socket of Java. It uses socket and IO flow to send the classes, methods in the classes and parameters of the methods that the service consumer needs to call to the service provider
 The service provider accepts the request from the service consumer, and uses the reflection technology to execute the corresponding method and return the result to the service consumer.

Posted by rg_22uk on Mon, 02 Dec 2019 09:34:18 -0800