Implement simple RPC calls between systems based on Tcp protocol with java

1. The first step: create a simple demo with idea

Insert picture description here
Insert picture description here
Insert picture description here

2. Step 2: Create the corresponding package and class

Insert picture description here

(1) Server class implementation:

package com.ctgu_zyj.rpc;

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;
import java.util.Map;

public class Server {
    
    
    public static void main(String[] args) throws Exception {
    
    
        ServerSocket serverSocket = new ServerSocket(1234);
        Map<Object,Object> services = new HashMap<Object,Object>();
        services.put(SayHelloService.class, new SayHelloServiceImpl());
        while(true){
    
    
            System.out.println("服务提供者启动,等待客户端调用…………");
            Socket socket = serverSocket.accept();

            //收到消息后进行解码
            ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            String interfaceName = objectInputStream.readUTF();
            String methodName = objectInputStream.readUTF();
            Class<?>[] paramterTypes = (Class<?>[])objectInputStream.readObject();
            Object[] argments = (Object[])objectInputStream.readObject();
            System.out.println("客户端调用服务端接口"+interfaceName+"的"+ methodName+"方法");

            //根据解码结果调用本地的服务
            Class serviceClass = Class.forName(interfaceName);
            Object serivce = services.get(serviceClass);
            Method method = serviceClass.getMethod(methodName, paramterTypes);
            Object result = method.invoke(serivce, argments);

            //服务提供者发送result给服务调用者
            ObjectOutputStream stream = new ObjectOutputStream(socket.getOutputStream());
            stream.writeObject(result);
            System.out.println("服务端返回结果为"+result);
        }
    }
}

(2) The interface class SayHelloService of the service class of the Server class:

package com.ctgu_zyj.rpc;

public interface SayHelloService {
    
    
    public String sayHello(String msg);
}

(3) Interface implementation class SayHelloServiceImpl:

package com.ctgu_zyj.rpc;

public class SayHelloServiceImpl implements SayHelloService {
    
    

    @Override
    public String sayHello(String msg) {
    
    
        if("hello".equals(msg)){
    
    
            return "hello client";
        }else{
    
    
            return "bye bye";
        }
    }
}

(4) Create a user class Consumer:

package com.ctgu_zyj.rpc;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.Socket;

public class Consumer {
    
    
    public static void main(String[] args) throws Exception {
    
    

        //构造需要调用的方法
        String interfaceName = SayHelloService.class.getName();
        Method method = SayHelloService.class.getMethod("sayHello",
                java.lang.String.class);
        Object[] argments = {
    
    "hello"};

        //发送调用信息到服务器端,调用相应的服务
        Socket socket = new Socket("127.0.0.1",1234);
        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
        outputStream.writeUTF(interfaceName);
        outputStream.writeUTF(method.getName());
        outputStream.writeObject(method.getParameterTypes());
        outputStream.writeObject(argments);
        System.out.println("发送信息到服务端,发送的信息为:"+argments[0]);

        //服务返回的结果
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        Object object = inputStream.readObject();
        System.out.println("服务返回的结果为"+object);
    }
}

3. Step 3: Prepare to run the project

Insert picture description here
Insert picture description here

4. Step 4: Summary

Simply put, the whole process is that the Server service provider runs, instantiates the service and places it in the Map of Services, and then continuously receives incoming requests through a while loop, gets the received parameters, and then obtains them through Java reflection. The service interface needs to call the method, and then return the result to the service consumer after execution.

Guess you like

Origin blog.csdn.net/Zheng_lan/article/details/108934248