网络编程 -- 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的更多相关文章

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

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——RPC -- 本地方法调用:不通过网络 入门 1. RPCObjectProxy rpcObjectProxy = new RPCObjec ...

  2. 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V2 -- 对象传输

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- 使用序列化和反序列化在网络上传输对象:需要实现 java.io.Serializable 接口 只能传输( ByteBuf ...

  3. 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——Netty入门应用 Class : NIOServerBootStrap package lime.pri.limeNio.netty.ne ...

  4. 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V3 -- 编码解码

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- pipeline.addLast(io.netty.handler.codec.MessageToMessageCodec ...

  5. 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V4 -- 粘包拆包

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- new LengthFieldPrepender(2) : 设置数据包 2 字节的特征码 new LengthFieldB ...

  6. 网络编程 -- RPC实现原理 -- NIO多线程 -- 迭代版本V2

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——增加WriteQueue队列,存放selectionKey.addWriteEventToQueue()添加selectionKey并唤醒阻 ...

  7. 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V3 -- 远程方法调用 整合 Spring

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V3——RPC -- 远程方法调用 及 null的传输 + Spring 服务提供商: 1. 配置 rpc03_server.xml 注入 服务提供 ...

  8. 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V4 -- 远程方法调用 整合 Spring 自动注册

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V4——RPC -- 远程方法调用 + Spring 自动注册 服务提供商: 1. 配置 rpc04_server.xml 注入 服务提供商 rpc ...

  9. Java网络编程UDP通信原理

    前言 继续今天我们的Java网络编程--TCP和UDP通信 一.TCP和UDP概述 传输层通常以TCP和UDP协议来控制端点与端点的通信   TCP UDP 协议名称 传输控制协议 用户数据包协议 是 ...

随机推荐

  1. 为什么用svg放弃了iconfont?

    svg替代iconfont的好处(无论是基于Vue.Jquery),都推荐svg http://www.woshipm.com/pd/463866.html svg图标库,svg图标在线制作 http ...

  2. springboot mybatis pagehelper 分页问题

    1:添加依赖 compile group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2 ...

  3. MySql之修改操作与进阶

    一:更新特定行 UPDATE tableName SET 列名 = 值,列名 = 值... WHERE 条件; 二:使用子查询更新数据 UPDATE tableName SET 列名 = SELECT ...

  4. 使用Guava的ComparisonChain实现自定义的排序

    可以看到使用比较器前,先要写一个实体类,还要实现comparable接口,实现compareTo方法.这个方法一般会返回-1 0 1三个int类型数字,分别表示,对象和传入的对象比较,排序应该在传入的 ...

  5. ASP.NET MVC ViewBag/ViewData/TempData区别

    ViewBag/ViewData public dynamic ViewBag { get; } public ViewDataDictionary ViewData { get; set; } Vi ...

  6. android 制作9.png图

    9.png图片与.png图片的具体不同之处.9.png图片的四周与普通的png图片相比多了一个像素位的白色区域,该区域只有在图片被还原和制造的时候才能看到,当打包后无法看见,并且图片的总像素会缩小2个 ...

  7. 【strpos】php的strpos的坑,记一次

    php > var_dump(strpos('开始23测试ceshi', '测试')); int(8) php > var_dump(mb_strpos('开始23测试ceshi', '测 ...

  8. tmux入门

    按键 man tmux和C+b?两个操作可以获得一切. C-b ? 显示快捷键帮助 C-b C-o 调换窗口位置,类似与vim 里的C-w C-b 空格键 采用下一个内置布局 C-b ! 把当前窗口变 ...

  9. Python多进程库multiprocessing中进程池Pool类的使用[转]

    from:http://blog.csdn.net/jinping_shi/article/details/52433867 Python多进程库multiprocessing中进程池Pool类的使用 ...

  10. C++ 匿名namespace的作用以及与static的区别

    匿名namespace的作用以及它与static的区别 一.匿名namespace的作用 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做 为函数名或者全局变量名 ...