欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于TCP协议实现RPC

程序员文章站 2022-07-10 16:19:54
...
RPC的全称:Remote Process Call,即远程过程调用,拥有RMI、webService等诸多成熟方案。
如图:
基于TCP协议实现RPC
            
    
    博客分类: java socketjava、Serializable 
实现代码如下:
User代码:
package com.test.xulihua;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User:
 * Date: 2018/3/19
 * Time: 11:16
 * To change this template use File | Settings | File Templates.
 */
public class User implements Serializable{
    private String name;
    private int age;
    private boolean sex;

    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 isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }
}


consumer.java代码
package com.test.xulihua;

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

/**
 * Created with IntelliJ IDEA.
 * User: 
 * Date: 2018/3/19
 * Time: 10:40
 * To change this template use File | Settings | File Templates.
 */
public class Consume {
    public static void main(String[] args) throws IOException, NoSuchMethodException, ClassNotFoundException {
        Socket socket = new Socket("127.0.0.1",8888);
        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
        String interfaceName = HelloSayService.class.getName();
        Method method = HelloSayService.class.getMethod("helloSay",User.class);
        User user =new User();
        user.setName("王五");
        Object[] params = {user};
        outputStream.writeUTF(interfaceName);    //接口名称
        outputStream.writeUTF(method.getName());  //方法名称
        outputStream.writeObject(method.getParameterTypes());
        outputStream.writeObject(params);
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        User object = (User) inputStream.readObject();
        System.out.println(object.getName()+":"+object.getAge()+":");
    }
}

Provider代码:
package com.test.xulihua;

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

/**
 * Created with IntelliJ IDEA.
 * User: 
 * Date: 2018/3/19
 * Time: 10:38
 * To change this template use File | Settings | File Templates.
 */
public class ServerProvide {
    private static Map<String,Object> services= new HashMap<String, Object>();
    static{
        services.put(HelloSayService.class.getName(),new HelloSayServiceImpl());
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true){
            Thread.sleep(1000L);
            Socket socket = serverSocket.accept();
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            String interfaceName = inputStream.readUTF();
            String methodName = inputStream.readUTF();
            Class<?>[] paramterType = (Class<?>[]) inputStream.readObject();
            Object[] paramter = (Object[]) inputStream.readObject();
            Class serviceClass = Class.forName(interfaceName);
            Object service =   services.get(interfaceName);
            Method method = serviceClass.getMethod(methodName,paramterType);
            Object result = method.invoke(service,paramter);
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(result);

        }
    }
}


接口HelloSayService.java
package com.test.xulihua;

/**
 * Created with IntelliJ IDEA.
 * User: 
 * Date: 2018/3/19
 * Time: 10:35
 * To change this template use File | Settings | File Templates.
 */
public interface HelloSayService {

    public User helloSay(User user);
}


接口service实现代码:
package com.test.xulihua;

/**
 * Created with IntelliJ IDEA.
 * User: 
 * Date: 2018/3/19
 * Time: 10:37
 * To change this template use File | Settings | File Templates.
 */
public class HelloSayServiceImpl implements HelloSayService {
    public User helloSay(User user) {
        user.setAge(123);
        user.setSex(false);
        return user ;
    }
}

  • 基于TCP协议实现RPC
            
    
    博客分类: java socketjava、Serializable 
  • 大小: 35.9 KB