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

网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring

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

  啦啦啦

V2——RPC -- 本地方法调用 + Spring

  1. 配置applicationContext.xml文件 注入 bean 及 管理 bean 之间的依赖关系

  2. RPCObjectProxy 类 实现 FactoryBean<Object> 接口,通过 public Object getObject() throws Exception 返回代理类

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

 

  XML : 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="   
          http://www.springframework.org/schema/beans 
          classpath:/org/springframework/beans/factory/xml/spring-beans-4.1.xsd 
          http://www.springframework.org/schema/context 
          classpath:/org/springframework/context/config/spring-context-4.1.xsd 
          http://mybatis.org/schema/mybatis-spring 
          http://mybatis.org/schema/mybatis-spring.xsd 
          http://www.springframework.org/schema/aop 
          classpath:org/springframework/aop/config/spring-aop-4.1.xsd"
    default-lazy-init="false">

    <bean id="userServiceProxy" class="lime.pri.limeNio.netty.rpc02.core.RPCObjectProxy">
        <constructor-arg index="0" ref="rpcClient" />
        <constructor-arg index="1" value="lime.pri.limeNio.netty.rpc02.service.IUserService" />
    </bean>


    <bean id="rpcClient" class="lime.pri.limeNio.netty.rpc02.core.impl.LocalRPCClient">
        <property name="beanFactory">
            <map>
                <entry key="IUserService" value-ref="userService"></entry>
            </map>
        </property>
    </bean>
    
    <bean id="userService" class="lime.pri.limeNio.netty.rpc02.service.impl.UserService"></bean>


</beans>

  Class :RPCObjectProxy

package lime.pri.limeNio.netty.rpc02.core;

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

import org.springframework.beans.factory.FactoryBean;

/**
 * 通过接口动态创建代理对象
 * 
 * @author lime
 * @param <T>
 *
 *            实现FactoryBean接口,与Spring整合
 * 
 */
public class RPCObjectProxy implements InvocationHandler, FactoryBean<Object> {

    private RPCClient rpcClient;
    private Class<?> targetInterface;

    public RPCObjectProxy() {
        super();
    }

    public RPCObjectProxy(RPCClient rpcClient, Class<?> targetInterface) {
        super();
        this.rpcClient = rpcClient;
        this.targetInterface = targetInterface;
    }

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

    // 产生代理对象
    public Object getObject() throws Exception {
        return Proxy.newProxyInstance(RPCObjectProxy.class.getClassLoader(), new Class[] { targetInterface }, this);

    }

    public Class<?> getObjectType() {
        return targetInterface;
    }

    // 返回单例对象
    public boolean isSingleton() {
        return true;
    }
}

  Class : RPCClient

package lime.pri.limeNio.netty.rpc02.core;

/**
 * 通过RPCClient实现对远程方法的调用
 * 
 * @author lime
 *
 */
public interface RPCClient {

    Object invokeMethod(MethodStaics methodStaics);
}

  Class : LocalRPCClient

package lime.pri.limeNio.netty.rpc02.core.impl;

import java.lang.reflect.Method;
import java.util.Map;

import lime.pri.limeNio.netty.rpc02.core.MethodStaics;
import lime.pri.limeNio.netty.rpc02.core.RPCClient;
import lime.pri.limeNio.netty.rpc02.service.IUserService;

public class LocalRPCClient implements RPCClient {

    private Map<String,Object> beanFactory;
    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;
    }
    public Map<String,Object> getBeanFactory() {
        return beanFactory;
    }
    public void setBeanFactory(Map<String,Object> beanFactory) {
        this.beanFactory = beanFactory;
    }

}

  Class : IUserService

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

import java.util.List;

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

public interface IUserService {

    User queryById(Integer id);

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

  Class : UserService

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

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

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

public class UserService implements IUserService {

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

    static {
        Calendar calendar = Calendar.getInstance();
        for (int i = 1; i <= 100; i++) {
            calendar.set(Calendar.YEAR, i + 1900);
            userMap.put(i, new User(i, "lime_" + i, calendar.getTime()));
        }
    }

    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;
    }

    public static void main(String[] args) {
        UserService userService = new UserService();
        for (User user : userService.queryAll(10, 2)) {
            System.out.println(user);
        }
        System.out.println(userService.queryById(100));
    }

}

  Class : User

package lime.pri.limeNio.netty.rpc02.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;
    }

}

  Class : MethodStaics

package lime.pri.limeNio.netty.rpc02.core;

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 : Tt

package lime.pri.limeNio.netty.rpc02.tT;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class Tt {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/rpc02.xml");
        IUserService userService = (IUserService) ctx.getBean("userServiceProxy");
        List<User> users = userService.queryAll(10, 3);
        for(User user : users){
            System.out.println(user);
        }
        System.out.println("-- -- ");
        User user = userService.queryById(23);
        System.out.println(user);
    }
}

  Console : 

六月 25, 2017 12:07:23 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17f052a3: startup date [Sun Jun 25 12:07:23 CST 2017]; root of context hierarchy
六月 25, 2017 12:07:23 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/rpc02.xml]
User [id=21, name=lime_21, birth=Sat Jun 25 12:07:23 CST 1921]
User [id=22, name=lime_22, birth=Sun Jun 25 12:07:23 CST 1922]
User [id=23, name=lime_23, birth=Mon Jun 25 12:07:23 CST 1923]
User [id=24, name=lime_24, birth=Wed Jun 25 12:07:23 CST 1924]
User [id=25, name=lime_25, birth=Thu Jun 25 12:07:23 CST 1925]
User [id=26, name=lime_26, birth=Fri Jun 25 12:07:23 CST 1926]
User [id=27, name=lime_27, birth=Sat Jun 25 12:07:23 CST 1927]
User [id=28, name=lime_28, birth=Mon Jun 25 12:07:23 CST 1928]
User [id=29, name=lime_29, birth=Tue Jun 25 12:07:23 CST 1929]
User [id=30, name=lime_30, birth=Wed Jun 25 12:07:23 CST 1930]
-- -- 
User [id=23, name=lime_23, birth=Mon Jun 25 12:07:23 CST 1923]

 

  啦啦啦

网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring