RPC使用JDK动态代理

介绍

在 Java 动态代理机制中 InvocationHandler 接口和 Proxy 类是核心。
Proxy类中使用频率最高的方法是:newProxyInstance() ,这个方法主要用来生成一个代理对象。

public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h)
    throws IllegalArgumentException{
    .....
}

这个方法一共有 3 个参数:
1.loader :类加载器,用于加载代理对象。
2.interfaces:被代理类实现的一些接口。
3.h:实现了 InvocationHandler 接口的对象。


要实现动态代理的话,还必须实现 InvocationHandler 来自定义处理逻辑。当我们的动态代理对象调用一个方法时候,这个方法的调用就会被转发到实现 InvocationHandler 接口类的 invoke 方法来调用。
 

JDK动态代理类使用步骤

1.定义一个接口及其实现类。
2.自定义 InvocationHandler 并重写 invoke 方法,在 invoke 方法中我们会调用原生方法(被代理类的方法) 并自定义一些处理逻辑。
3 .通过 Proxy.newProxyInstance(ClassLoader loader,Class<?>[] interfaces, InvocationHandler h) 方法创建代理对象。
 

代码示例

1.定义发送短信的接口

public interface SmsService {
    String send(string message);
}

2.实现发送短信的接口

public class SmsServiceImpl implements SmsService {
    public String send(String message) {
        System.out.println("send message:"+ message);
        return message;
    }
}

3.定义一个JDK动态代理类

public class DebugInvocationHandler implements InvocationHandler {
    //代理类中的真实对象
    private final Object target;
 
    public DebugInvocationHandler(Object target) {
        this.target = target;
    }
 
    public 0bject invoke(Object proxy, Method method, 0bject[] args) throws InvocationTargetException, IllegalAccessException{
        //调用方法之前,我们可以添加自己的操作
        System.out.println("before method " + method.getName());
        Obiect result = method.invoke(target, args);
        //调用方法之后,我们同样可以添加自己的操作
        System.out.println("after method " + method.getName());
        return result;
    }
}

invoke() 方法: 当我们的动态代理对象调用原生方法的时候,最终实际上调用到的是 invoke()方法,然后 invoke() 方法代替我们去调用了被代理对象的原生方法。

4.获取代理对象的工厂类

public class JdkProxyFactory{
    public static Object getProxy(Object target) {
        return Proxy.newProxyInstance(
            //目标类的类加载
            target.getclass().getclassLoader();
            //代理需要实现的接口,可指定多个
            target.getClass().getInterfaces()
            //代理对象对应的自定义
            InvocationHandlernew DebugInvocationHandler(target)
        );
    }
}

5.实际使用

SmsService smsService = (SmsService) JdkProxyFactory.getProxy(new SmsServiceImpl());
smsService.send("java")

在RPC中的使用

RpcClientProxy相当于代理对象的工厂类,同时它还实现了InvocationHandler接口,所以它自己也是JDK动态代理类。

这里的getProxy对应工厂类的getProxy方法。

public <T> T getProxy(Class<T> clazz) {
    // this代表this.invoke
    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz}, this);
}

第一个<T>表示该方法是泛型方法,第二个T是返回值类型,Class<T> clazz是一个类型为<T>的类。this就代表了它自己,因为自己实现了InvocationHandler接口。

这里的invoke对应动态代理类的invoke方法。

   public Object invoke(Object proxy, Method method, Object[] args) {
        log.info("invoked method: [{}]", method.getName());
        // 根据被调用的方法、方法参数等信息构建一个 RpcRequest 对象,表示一个远程过程调用请求
        RpcRequest rpcRequest = RpcRequest.builder().methodName(method.getName())
                .parameters(args)
                .interfaceName(method.getDeclaringClass().getName())
                .paramTypes(method.getParameterTypes())
                .requestId(UUID.randomUUID().toString())
                .group(rpcServiceConfig.getGroup())
                .version(rpcServiceConfig.getVersion())
                .build();
        RpcResponse<Object> rpcResponse = null;
        // 判断rpcResponse的类型
        if (rpcRequestTransport instanceof NettyRpcClient) {
            //这里的rpcRequestTransport.sendRpcRequest对应了NettyRpcClient的sendRpcRequest
            CompletableFuture<RpcResponse<Object>> completableFuture = (CompletableFuture<RpcResponse<Object>>) rpcRequestTransport.sendRpcRequest(rpcRequest);
            rpcResponse = completableFuture.get();
        }
        if (rpcRequestTransport instanceof SocketRpcClient) {
            rpcResponse = (RpcResponse<Object>) rpcRequestTransport.sendRpcRequest(rpcRequest);
        }
        this.check(rpcResponse, rpcRequest);
        return rpcResponse.getData();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值