RPC简单的demo

编写rpc服务端代码如下:

package com.bootdo.testDemo.rpc;

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

public class SimpleRpcFramework {

    public static void export(Object serviceImpl ,int port){
        try(ServerSocket serverSocket=new ServerSocket(port)){
            Socket socket=serverSocket.accept();
            while(!Thread.currentThread().isInterrupted()){
                 new Thread(()->{
                     try(ObjectInputStream objectInputStream=new ObjectInputStream(socket.getInputStream())){
                         //利用放射获取方法,后面是方法的参数类型。
                         Method method=serviceImpl.getClass().getMethod(objectInputStream.readUTF(),(Class[]) objectInputStream.readObject());
                         Object result=method.invoke(serviceImpl,(Object[]) objectInputStream.readObject());
                         try(MyObjectOutputStream outputStream=new MyObjectOutputStream(socket.getOutputStream())){
                             outputStream.writeObject(result);
                         }
                         System.out.println("service sccess:result="+result+" from "+socket.getRemoteSocketAddress());
                     }catch (Exception e){
                         e.printStackTrace();
                     }
                 }).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public static  T use(Class serviceImpl,String host,int port){
        System.out.println("use:"+serviceImpl.getSimpleName()+";host:");
        return (T)Proxy.newProxyInstance(serviceImpl.getClassLoader(),new Class[]{serviceImpl},(proxy,method,args)->{
            try(Socket socket=new Socket(host,port)){
                try(MyObjectOutputStream objectOutputStream=new MyObjectOutputStream(socket.getOutputStream())) {
                    objectOutputStream.writeUTF(method.getName());
                    objectOutputStream.writeObject(method.getParameterTypes());
                    objectOutputStream.writeObject(args);
                    try(ObjectInputStream objectInputStream=new ObjectInputStream(socket.getInputStream())){
                        return objectInputStream.readObject();
                    }
                }
            }
        });
    }
}

测试客户端代码如下:

package com.bootdo.testDemo.rpc;


import com.bootdo.common.utils.TimeUtils;

import java.util.concurrent.TimeUnit;

import static com.bootdo.testDemo.rpc.SimpleRpcFramework.*;
public class RpcTest {
    private static final String HOST="localhost";
    private static final int PORT=8899;

    static interface Compute{
        int sum(int a,int b);
    }


    static class ComputeServiceImpl implements Compute{
        @Override
        public int sum(int a, int b) {
            return a+b;
        }
    }

    static class Provider implements Runnable{
        @Override
        public void run() {
            export(new ComputeServiceImpl(),PORT);
        }
    }

    static class Comsumer implements Runnable {
        @Override
        public void run() {
            Compute compute = use(Compute.class, HOST, PORT);
            for (int i = 0; i < 10; i++) {
                int sum = compute.sum(i, i + 1);
                System.out.println(i + "+" + (i + 1) + "=" + sum);
              /*  try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }*/

            }
        }
    }


    public static void main(String[] args) {
        new Thread(new Provider()).start();
        new Thread(new Comsumer()).start();
        //new Thread(new Comsumer()).start();
    }
}

 

你可能感兴趣的:(java)