首页 > 代码库 > 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V1 -- 本地方法调用

网络编程 -- RPC实现原理 -- RPC -- 迭代版本V1 -- 本地方法调用

 网络编程 -- RPC实现原理 -- 目录

  啦啦啦

V2——RPC -- 本地方法调用:不通过网络 入门

  1. RPCObjectProxy rpcObjectProxy = new RPCObjectProxy(new LocalRPCClient()); : 绑定目标对象

  2. IUserService userService = (IUserService) rpcObjectProxy.create(IUserService.class); :返回代理类

  3. List<User> users = userService.queryAll(10, 4); : 调用目标对象的  Object invokeMethod(MethodStaics methodStaics);  方法,通过反射返回指定接口实现方法的返回值。

 

  Class : RPCObjectProxy

package lime.pri.limeNio.netty.rpc01.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

import lime.pri.limeNio.netty.rpc01.entity.User;
import lime.pri.limeNio.netty.rpc01.service.IUserService;

public class RPCObjectProxy implements InvocationHandler {

    private RPCClient rpcClient;

    public RPCObjectProxy() {
        super();
    }

    public RPCObjectProxy(RPCClient rpcClient) {
        super();
        this.rpcClient = rpcClient;
    }

    private Class<?> targetInterface;

    public Object create(Class<?> targetInterface) {
        this.targetInterface = targetInterface;
        return Proxy.newProxyInstance(RPCObjectProxy.class.getClassLoader(), new Class[] { targetInterface }, this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return rpcClient
                .invokeMethod(new MethodStaics(targetInterface, method.getName(), args, method.getParameterTypes()));
    }

    public static void main(String[] args) {

        RPCObjectProxy rpcObjectProxy = new RPCObjectProxy(new LocalRPCClient());
        IUserService userService = (IUserService) rpcObjectProxy.create(IUserService.class);
        System.out.println("---queryAll");
        List<User> users = userService.queryAll(10, 4);
        for (User user : users) {
            System.out.println(user);
        }
        System.out.println("---queryById");
        User user = userService.queryById(20);
        System.out.println(user);
    }
}

  Class : RPCClient

package lime.pri.limeNio.netty.rpc01.proxy;

public interface RPCClient {

    Object invokeMethod(MethodStaics methodStaics);

}

  Class : LocalRPCClient

package lime.pri.limeNio.netty.rpc01.proxy;

import java.lang.reflect.Method;

import lime.pri.limeNio.netty.rpc01.service.IUserService;

public class LocalRPCClient implements RPCClient {

    public Object invokeMethod(MethodStaics methodStaics) {
        try {
            IUserService object = (IUserService) BeanFactory.get(methodStaics.getTargetInterface().getSimpleName());
            Method method = object.getClass().getDeclaredMethod(methodStaics.getMethod(),
                    methodStaics.getParameterTypes());
            return method.invoke(object, methodStaics.getArgs());
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

}

  Class : BeanFactory

package lime.pri.limeNio.netty.rpc01.proxy;

import java.util.HashMap;
import java.util.Map;

import lime.pri.limeNio.netty.rpc01.service.IUserService;
import lime.pri.limeNio.netty.rpc01.service.impl.UserService;

public class BeanFactory {

    private static Map<String,Object> beanFactory;
    static{
        beanFactory = new HashMap<String, Object>();
        beanFactory.put(IUserService.class.getSimpleName(), new UserService());
    }
    public static Object get(String targetInterface){
        return beanFactory.get(targetInterface);
    }
}

  Class : IUserService

package lime.pri.limeNio.netty.rpc01.service;

import java.util.List;

import lime.pri.limeNio.netty.rpc01.entity.User;

public interface IUserService {

    User queryById(Integer id);
    
    List<User> queryAll(Integer pageSize,Integer pageNum);
}

  Class : UserService

package lime.pri.limeNio.netty.rpc01.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lime.pri.limeNio.netty.rpc01.entity.User;
import lime.pri.limeNio.netty.rpc01.service.IUserService;

public class UserService implements IUserService {

    private static Map<Integer, User> userMap = new HashMap<Integer, User>();

    static {
        for (int i = 1; i <= 100; i++) {
            userMap.put(i, new User(i, "lime_" + i, new Date()));
        }
    }

    public User queryById(Integer id) {
        return userMap.get(id);
    }

    public List<User> queryAll(Integer pageSize, Integer pageNum) {
        int stNum = (pageNum - 1) * pageSize + 1;
        int enNum = pageNum * pageSize;
        List<User> result = new ArrayList<User>();
        for (int i = stNum; i <= enNum; i++) {
            result.add(userMap.get(i));
        }
        return result;
    }
}

  Class : 

package lime.pri.limeNio.netty.rpc01.proxy;

import java.io.Serializable;
import java.util.Arrays;

/**
 * @author lime
 *
 */
public class MethodStaics implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Class<?> targetInterface;
    private String method;
    private Object[] args;
    private Class[] parameterTypes;
    public MethodStaics() {
        super();
        // TODO Auto-generated constructor stub
    }
    public MethodStaics(Class<?> targetInterface, String method, Object[] args, Class[] parameterTypes) {
        super();
        this.targetInterface = targetInterface;
        this.method = method;
        this.args = args;
        this.parameterTypes = parameterTypes;
    }
    @Override
    public String toString() {
        return "MethodStaics [targetInterface=" + targetInterface + ", method=" + method + ", args="
                + Arrays.toString(args) + ", parameterTypes=" + Arrays.toString(parameterTypes) + "]";
    }
    public Class<?> getTargetInterface() {
        return targetInterface;
    }
    public void setTargetInterface(Class<?> targetInterface) {
        this.targetInterface = targetInterface;
    }
    public String getMethod() {
        return method;
    }
    public void setMethod(String method) {
        this.method = method;
    }
    public Object[] getArgs() {
        return args;
    }
    public void setArgs(Object[] args) {
        this.args = args;
    }
    public Class[] getParameterTypes() {
        return parameterTypes;
    }
    public void setParameterTypes(Class[] parameterTypes) {
        this.parameterTypes = parameterTypes;
    }

}

  Class : User

package lime.pri.limeNio.netty.rpc01.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int id;
    private String name;
    private Date birth;

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(int id, String name, Date birth) {
        super();
        this.id = id;
        this.name = name;
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

}

啦啦啦

网络编程 -- RPC实现原理 -- RPC -- 迭代版本V1 -- 本地方法调用