【分布式-2】自定义RPC框架
创始人
2024-06-02 22:05:46

RPC全称为remote procedure call,即远程过程调用。  实现对远程服务的调用,像在自己服务里调用方法一样。

如上图,RPC要做的就是把除了1,6外,其他步骤封装起来。 

在java中,已经有方式实现了远程调用。 就是java JMI。 它的实现依赖于Java虚拟机,因

此它仅支持从一个JVM到另一个JVM的调用。

一:java RMI

Java RMI,即远程方法调用(Remote Method Invocation),一种用于实现远程过程调用的Java API(RPCRemote procedure call), 能直接传输序列化后的Java对象。它的实现依赖于Java虚拟机,因此它仅支持从一个JVM到另一个JVM的调用。

代码实现一个查用户的方法:

import com.lagou.edu.service.impl.UserServiceImpl;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;/*** 服务端 */
public class RMIServer {public static void main(String[] args) {try {//1.注册Registry实例. 绑定端口Registry registry = LocateRegistry.createRegistry(9998);//2.创建远程对象IUserService userService = new UserServiceImpl();//3.将远程对象注册到RMI服务器上即(服务端注册表上)registry.rebind("userService", userService);System.out.println("---RMI服务端启动成功----");} catch (RemoteException e) {e.printStackTrace();}}
}
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.IUserService;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;/*** 客户端 */
public class RMIClient {public static void main(String[] args)throws RemoteException, NotBoundException {//1.获取Registry实例Registry registry = LocateRegistry.getRegistry("127.0.0.1", 9998);//2.通过Registry实例查找远程对象IUserService userService = (IUserService) registry.lookup("userService");User user = userService.getById(2);System.out.println(user.getId() + "----" + user.getName());}
}
import com.lagou.edu.pojo.User;
import java.rmi.Remote;
import java.rmi.RemoteException;public interface IUserService extends Remote {User getById(int id) throws RemoteException;
}
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.IUserService;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;public class UserServiceImpl extends UnicastRemoteObject implements IUserService {Map userMap = new HashMap();public UserServiceImpl() throws RemoteException {super();User user1 = new User();user1.setId(1);user1.setName("张三");User user2 = new User();user2.setId(2);user2.setName("李四");userMap.put(user1.getId(), user1);userMap.put(user2.getId(), user2);}@Overridepublic User getById(int id) throws RemoteException {return userMap.get(id);}
}

实际项目中不会用到RMI,因为不会真正去关心别人服务器的ip等信息是什么,也不会自己去维护。使用的还是如dubbo框架这种,基于Netty实现的通信。下面模拟自定义一个。 

需求: 客户端远程调用服务端提供根据ID查询user对象的方法.

1. 创建一个接口,定义抽象方法。用于消费者和提供者之间的约定, 2. 创建一个提供者,该类需要监听消费者的请求,并按照约定返回数据 3. 创建一个消费者,该类需要透明的调用自己不存在的方法,内部需要使用 Netty 进行数据通信 4. 提供者与消费者数据传输使用json字符串数据格式 5. 提供者使用netty集成spring boot 环境实现

1、自定义注解:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 对外暴露服务接口 */
@Target(ElementType.TYPE) // 用于接口和类上 
@Retention(RetentionPolicy.RUNTIME)// 在运行时可以获取到
public @interface RpcService {}

2、服务端实现类:

import com.lagou.edu.annotation.RpcService;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.IUserService;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;//该注解表示要注册到注册中心
@RpcService
@Service
public class UserServiceImpl implements IUserService {Map userMap = new HashMap();@Overridepublic User getById(int id) {if (userMap.size() == 0) {User user1 = new User();user1.setId(1);user1.setName("张三");User user2 = new User();user2.setId(2);user2.setName("李四");userMap.put(user1.getId(), user1);userMap.put(user2.getId(), user2);}return userMap.get(id);}}

二:Netty模拟dubbo框架实现

核心架构图:

api层:

定义接口,出参和入参

IUserService:

import com.lagou.rpc.pojo.User;/*** 用户服务*/
public interface IUserService {/*** 根据ID查询用户** @param id* @return*/User getById(int id);
}

实体:

import lombok.Data;@Data
public class User {private int id;private String name;
}

入参:

/*** 封装的请求对象*/
@Data
public class RpcRequest {/*** 请求对象的ID*/private String requestId;/*** 类名*/private String className;/*** 方法名*/private String methodName;/*** 参数类型*/private Class[] parameterTypes;/*** 入参*/private Object[] parameters;}

 出参:

/*** 封装的响应对象*/
@Data
public class RpcResponse {/*** 响应ID*/private String requestId;/*** 错误信息*/private String error;/*** 返回的结果*/private Object result;}

服务端:

注解RpcService:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 对外暴露服务接口*/
@Target(ElementType.TYPE) // 用于接口和类上
@Retention(RetentionPolicy.RUNTIME)// 在运行时可以获取到
public @interface RpcService {
}

接口实现类:

import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.pojo.User;
import com.lagou.rpc.provider.anno.RpcService;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;@RpcService
@Service
public class UserServiceImpl implements IUserService {Map userMap = new HashMap();@Overridepublic User getById(int id) {if (userMap.size() == 0) {User user1 = new User();user1.setId(1);user1.setName("张三");User user2 = new User();user2.setId(2);user2.setName("李四");userMap.put(user1.getId(), user1);userMap.put(user2.getId(), user2);}return userMap.get(id);}
}

 服务端Netty启动类

package com.lagou.rpc.provider.server;import com.lagou.rpc.provider.handler.RpcServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** 启动类*/
@Service
public class RpcServer implements DisposableBean {private NioEventLoopGroup bossGroup;private NioEventLoopGroup workerGroup;@AutowiredRpcServerHandler rpcServerHandler;public void startServer(String ip, int port) {try {//1. 创建线程组bossGroup = new NioEventLoopGroup(1);workerGroup = new NioEventLoopGroup();//2. 创建服务端启动助手ServerBootstrap serverBootstrap = new ServerBootstrap();//3. 设置参数serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer() {@Overrideprotected void initChannel(SocketChannel channel) throws Exception {ChannelPipeline pipeline = channel.pipeline();//添加String的编解码器pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());//业务处理类pipeline.addLast(rpcServerHandler);}});//4.绑定端口(要同步阻塞等待)ChannelFuture sync = serverBootstrap.bind(ip, port).sync();System.out.println("==========服务端启动成功==========");//channel也要同步阻塞等待sync.channel().closeFuture().sync();} catch (InterruptedException e) {e.printStackTrace();} finally {if (bossGroup != null) {bossGroup.shutdownGracefully();}if (workerGroup != null) {workerGroup.shutdownGracefully();}}}@Overridepublic void destroy() throws Exception {if (bossGroup != null) {bossGroup.shutdownGracefully();}if (workerGroup != null) {workerGroup.shutdownGracefully();}}
}

业务处理类RpcServerHandler:

该类的作用是:

1、将所有自定义注解的类缓存到map,key就是接口的名称

2、根据请求方参数的接口名找到map中具体实现类,再根据其他参数反射调用方法。

package com.lagou.rpc.provider.handler;import com.alibaba.fastjson.JSON;
import com.lagou.rpc.common.RpcRequest;
import com.lagou.rpc.common.RpcResponse;
import com.lagou.rpc.provider.anno.RpcService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.beans.BeansException;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;/*** 服务端业务处理类* 1.将标有@RpcService注解的bean缓存* 2.接收客户端请求* 3.根据传递过来的beanName从缓存中查找到对应的bean* 4.解析请求中的方法名称. 参数类型 参数信息* 5.反射调用bean的方法* 6.给客户端进行响应*/
@Component
@ChannelHandler.Sharable
public class RpcServerHandler extends SimpleChannelInboundHandler implements ApplicationContextAware {private static final Map SERVICE_INSTANCE_MAP = new ConcurrentHashMap();/*** 1.将标有@RpcService注解的bean缓存** @param applicationContext* @throws BeansException*/@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {Map serviceMap = applicationContext.getBeansWithAnnotation(RpcService.class);if (serviceMap != null && serviceMap.size() > 0) {Set> entries = serviceMap.entrySet();for (Map.Entry item : entries) {Object serviceBean = item.getValue();if (serviceBean.getClass().getInterfaces().length == 0) {throw new RuntimeException("服务必须实现接口");}//默认取第一个接口作为缓存bean的名称String name = serviceBean.getClass().getInterfaces()[0].getName();SERVICE_INSTANCE_MAP.put(name, serviceBean);}}}/*** 通道读取就绪事件** @param channelHandlerContext* @param msg* @throws Exception*/@Overrideprotected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {//1.接收客户端请求- 将msg转化RpcRequest对象RpcRequest rpcRequest = JSON.parseObject(msg, RpcRequest.class);RpcResponse rpcResponse = new RpcResponse();rpcResponse.setRequestId(rpcRequest.getRequestId());try {//业务处理rpcResponse.setResult(handler(rpcRequest));} catch (Exception exception) {exception.printStackTrace();rpcResponse.setError(exception.getMessage());}//6.给客户端进行响应channelHandlerContext.writeAndFlush(JSON.toJSONString(rpcResponse));}/*** 业务处理逻辑** @return*/public Object handler(RpcRequest rpcRequest) throws InvocationTargetException {// 3.根据传递过来的beanName从缓存中查找到对应的beanObject serviceBean = SERVICE_INSTANCE_MAP.get(rpcRequest.getClassName());if (serviceBean == null) {throw new RuntimeException("根据beanName找不到服务,beanName:" + rpcRequest.getClassName());}//4.解析请求中的方法名称. 参数类型 参数信息Class serviceBeanClass = serviceBean.getClass();String methodName = rpcRequest.getMethodName();Class[] parameterTypes = rpcRequest.getParameterTypes();Object[] parameters = rpcRequest.getParameters();//5.反射调用bean的方法- CGLIB反射调用FastClass fastClass = FastClass.create(serviceBeanClass);FastMethod method = fastClass.getMethod(methodName, parameterTypes);return method.invoke(serviceBean, parameters);}}

服务端启动类:

package com.lagou.rpc.provider;import com.lagou.rpc.provider.server.RpcServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {@AutowiredRpcServer rpcServer;public static void main(String[] args) {SpringApplication.run(ServerBootstrapApplication.class, args);}@Overridepublic void run(String... args) throws Exception {new Thread(new Runnable() {@Overridepublic void run() {rpcServer.startServer("127.0.01", 8899);}}).start();}
}

注意:CommandLineRunner是springboot提供的,在spring初始化完成后执行。具体使用可参照:https://www.cnblogs.com/yanxiaoguo/p/16167221.html

客户端: 

Netty启动类:

package com.lagou.rpc.consumer.client;import com.lagou.rpc.consumer.handler.RpcClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;/*** 客户端* 1.连接Netty服务端* 2.提供给调用者主动关闭资源的方法* 3.提供消息发送的方法*/
public class RpcClient {private EventLoopGroup group;private Channel channel;private String ip;private int port;private RpcClientHandler rpcClientHandler = new RpcClientHandler();private ExecutorService executorService = Executors.newCachedThreadPool();public RpcClient(String ip, int port) {this.ip = ip;this.port = port;initClient();}/*** 初始化方法-连接Netty服务端*/public void initClient() {try {//1.创建线程组group = new NioEventLoopGroup();//2.创建启动助手Bootstrap bootstrap = new Bootstrap();//3.设置参数bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000).handler(new ChannelInitializer() {@Overrideprotected void initChannel(SocketChannel channel) throws Exception {ChannelPipeline pipeline = channel.pipeline();//String类型编解码器pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());//添加客户端处理类pipeline.addLast(rpcClientHandler);}});//4.连接Netty服务端channel = bootstrap.connect(ip, port).sync().channel();} catch (Exception exception) {exception.printStackTrace();if (channel != null) {channel.close();}if (group != null) {group.shutdownGracefully();}}}/*** 提供给调用者主动关闭资源的方法*/public void close() {if (channel != null) {channel.close();}if (group != null) {group.shutdownGracefully();}}/*** 提供消息发送的方法*/public Object send(String msg) throws ExecutionException, InterruptedException {rpcClientHandler.setRequestMsg(msg);Future submit = executorService.submit(rpcClientHandler);return submit.get();}
}

 业务处理类RpcClientHandler:

package com.lagou.rpc.consumer.handler;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;import java.util.concurrent.Callable;/*** 客户端处理类* 1.发送消息* 2.接收消息*/
public class RpcClientHandler extends SimpleChannelInboundHandler implements Callable {ChannelHandlerContext context;//发送的消息String requestMsg;//服务端的消息String responseMsg;public void setRequestMsg(String requestMsg) {this.requestMsg = requestMsg;}/*** 通道连接就绪事件** @param ctx* @throws Exception*/@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {context = ctx;}/*** 通道读取就绪事件** @param channelHandlerContext* @param msg* @throws Exception*/@Overrideprotected synchronized void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {responseMsg = msg;//唤醒等待的线程notify();}/*** 发送消息到服务端** @return* @throws Exception*/@Overridepublic synchronized Object call() throws Exception {//消息发送context.writeAndFlush(requestMsg);//线程等待wait();return responseMsg;}
}

代理类:客户端真正调用接口时,每个客户端其实是用的接口的代理对象。

该代理类执行invoke方法时,才真正创建RpcClient对象去连接服务端进行通信。

package com.lagou.rpc.consumer.proxy;import com.alibaba.fastjson.JSON;
import com.lagou.rpc.common.RpcRequest;
import com.lagou.rpc.common.RpcResponse;
import com.lagou.rpc.consumer.client.RpcClient;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.UUID;/*** 客户端代理类-创建代理对象* 1.封装request请求对象* 2.创建RpcClient对象* 3.发送消息* 4.返回结果*/
public class RpcClientProxy {public static Object createProxy(Class serviceClass) {return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{serviceClass}, new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//1.封装request请求对象RpcRequest rpcRequest = new RpcRequest();rpcRequest.setRequestId(UUID.randomUUID().toString());rpcRequest.setClassName(method.getDeclaringClass().getName());rpcRequest.setMethodName(method.getName());rpcRequest.setParameterTypes(method.getParameterTypes());rpcRequest.setParameters(args);//2.创建RpcClient对象RpcClient rpcClient = new RpcClient("127.0.0.1", 8899);try {//3.发送消息Object responseMsg = rpcClient.send(JSON.toJSONString(rpcRequest));RpcResponse rpcResponse = JSON.parseObject(responseMsg.toString(), RpcResponse.class);if (rpcResponse.getError() != null) {throw new RuntimeException(rpcResponse.getError());}//4.返回结果Object result = rpcResponse.getResult();return JSON.parseObject(result.toString(), method.getReturnType());} catch (Exception e) {throw e;} finally {rpcClient.close();}}});}
}

测试类:

package com.lagou.rpc.consumer;import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;/*** 测试类*/
public class ClientBootStrap {public static void main(String[] args) {IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class);User user = userService.getById(1);System.out.println(user);}
}

客户端流程总结:

1、创建接口代理对象。

2、代理对象在调用invoke时,创建RpcClient对象。在RpcClient构造方法中,就会初始化Netty信息连接服务端。

3、调用RpcClient中的send方法,其实就是执行rpcClientHandler这个线程的call方法。

4、rpcClientHandler的call方法,就会发送和接受服务端消息。

上面就是自定义RPC的简单实现,Netty相关的东西,需要去回顾之前章节的学习。 

相关内容

热门资讯

苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...