美文网首页
hsf笔记-ClientFilter

hsf笔记-ClientFilter

作者: 兴浩 | 来源:发表于2018-11-23 22:38 被阅读32次

1.ClientFilter

Consumer端过滤器

1.1 CommonClientFilter

在requestProps中设置AppName和TargetGroup

public class CommonClientFilter implements ClientFilter {
    public CommonClientFilter() {
    }

    public ListenableFuture<RPCResult> invoke(InvocationHandler invocationHandler, Invocation invocation) throws Throwable {
        ConsumerMethodModel consumerMethodModel = invocation.getClientInvocationContext().getMethodModel();
        ServiceMetadata serviceMetadata = consumerMethodModel.getMetadata();
        ApplicationModel applicationModel = serviceMetadata.getApplicationModel();
        invocation.setRequestProps("Consumer-AppName", applicationModel.getName());
        invocation.setRequestProps("target_group", serviceMetadata.getGroup());
        return invocationHandler.invoke(invocation);
    }

    public void onResponse(Invocation invocation, RPCResult rpcResult) {
    }
}

1.2 MonitorLogClientFilter

在调用前后添加监控日志

    public ListenableFuture<RPCResult> invoke(InvocationHandler nextHandler, Invocation invocation) throws Throwable {
        ConsumerMethodModel methodModel = invocation.getClientInvocationContext().getMethodModel();
        String serviceName = methodModel.getUniqueName();
        String methodName = invocation.getMethodName();
        this.monitorService.add("HSF-Consumer-ActiveThread", serviceName, methodName, 1L, 1L);

        ListenableFuture var6;
        try {
            var6 = nextHandler.invoke(invocation);
        } finally {
            this.monitorService.add("HSF-Consumer-ActiveThread", serviceName, methodName, -1L, -1L);
        }

        return var6;
    }

1.3 InvocationStatsClientFilter

统计consumer端调用次数

public class InvocationStatsClientFilter implements ClientFilter {
    private InvocationStats<ConsumerMethodModel, ConsumerInvokerStats> clientInvocationStats = (InvocationStats)HSFServiceContainer.getInstance(InvocationStats.class, "consumer");
    private RemotingRuntimeInfoHolder remotingRuntimeInfoHolder = RemotingRuntimeInfoHolder.getInstance();

    public InvocationStatsClientFilter() {
    }

    public ListenableFuture<RPCResult> invoke(InvocationHandler nextHandler, Invocation invocation) throws Throwable {
        ConsumerMethodModel consumerMethodModel = invocation.getClientInvocationContext().getMethodModel();
        ConsumerInvokerStats consumerInvokerStats = null;

        ListenableFuture var5;
        try {
            consumerInvokerStats = (ConsumerInvokerStats)this.clientInvocationStats.getStats(consumerMethodModel);
            if (consumerInvokerStats != null) {
                consumerInvokerStats.addThreadCount(1);
                consumerInvokerStats.addInvokeCount(1L);
            }

            var5 = nextHandler.invoke(invocation);
        } catch (Throwable var10) {
            RPCResult rpcResult = new RPCResult();
            rpcResult.setHsfResponse(new HSFResponse());
            rpcResult.setAppResponse(var10);
            rpcResult.setErrorType(ResponseStatus.UNKNOWN_ERROR.name());
            this.onResponse(invocation, rpcResult);
            throw var10;
        } finally {
            if (consumerInvokerStats != null) {
                consumerInvokerStats.addThreadCount(-1);
            }

        }

        return var5;
    }
}

1.4 SpasClientFilter

添加签名请求信息

public class SpasClientFilter implements ClientFilter, ApplicationModelAware, ServiceMetadataAware {
    private static final Logger LOGGER;
    private ApplicationModel applicationModel;
    private ServiceMetadata serviceMetadata;
    private static final Boolean clientNeedAuth;

    public SpasClientFilter() {
    }

    public ListenableFuture<RPCResult> invoke(InvocationHandler invocationHandler, Invocation invocation) throws Throwable {
        if (this.serviceMetadata.getAttributeMap().get(SpasApplicationComponent.NEED_AUTH_ATTRIBUTE_KEY) != Boolean.TRUE) {
            return invocationHandler.invoke(invocation);
        } else {
            String accessKey;
            try {
                String secretKey = RequestCtxUtil.getSecreteKey();
                accessKey = RequestCtxUtil.getAccessKey();
                if (StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(accessKey)) {
                    Credentials credential = SpasSdkClientFacade.getCredential(this.applicationModel.getName());
                    secretKey = credential == null ? null : credential.getSecretKey();
                    accessKey = credential == null ? null : credential.getAccessKey();
                }

                if (StringUtils.isNotEmpty(secretKey)) {
                    HSFRequest request = invocation.getHsfRequest();
                    String spasSignatureString = request.getTargetServiceUniqueName() + "#" + request.getMethodName();
                    String signature = SpasSigner.sign(spasSignatureString, secretKey);
                    request.setRequestProps("Spas-Signature", signature);
                    if (StringUtils.isNotEmpty(accessKey)) {
                        request.setRequestProps("Access-Key", accessKey);
                    }

                    String version = SpasSdkClientFacade.getVersion();
                    if (StringUtils.isNotEmpty(version)) {
                        request.setRequestProps("Spas-Version", version);
                    }
                }
            } catch (Exception var9) {
                accessKey = LoggerHelper.getErrorCodeStr("HSF", "HSF-0081", "HSF", "spas credential error.");
                LOGGER.error("HSF-0081", accessKey, var9);
            }

            return invocationHandler.invoke(invocation);
        }
    }
}

1.5 InvocationValidationFilter

调用方法参数长度检查

public class InvocationValidationFilter implements ClientFilter {

    public ListenableFuture<RPCResult> invoke(InvocationHandler invocationHandler, Invocation invocation) throws Throwable {
        Object[] args = invocation.getMethodArgs();
        String[] signature = invocation.getMethodArgSigs();
        if (args != null && args.length != signature.length) {
            throw new HSFException("invalid invocation: args.length != argTypes.length.");
        } else {
            return invocationHandler.invoke(invocation);
        }
    }
}

1.6 GenericInvocationClientFilter

标准泛化方法执行

    public static boolean isGenericMethod(String methodName, String[] sig) {
        return methodName.equals("$invoke") && sig != null && sig.length == 3;
    }
    public ListenableFuture<RPCResult> invoke(InvocationHandler invocationHandler, Invocation invocation) throws Throwable {
        HSFRequest hsfRequest = invocation.getHsfRequest();
        if (PojoUtils.isGenericMethod(hsfRequest.getMethodName(), hsfRequest.getMethodArgSigs())) {
            Object[] methodArgs = hsfRequest.getMethodArgs();
            String[] realSignatures = (String[])((String[])methodArgs[1]);
            Object[] realArgs = (Object[])((Object[])methodArgs[2]);
            if (realSignatures == null) {
                realSignatures = new String[0];
                methodArgs[1] = realSignatures;
            }

            if (realArgs == null) {
                realArgs = new Object[0];
                methodArgs[2] = realArgs;
            }

            if (realSignatures.length != realArgs.length) {
                throw new HSFException("invalid generic invocation: args.length != argTypes.length.");
            }

            if (isServerRemoveClass) {
                hsfRequest.setRequestProps("REMOVE_CLASS", true);
            }
        }

        return invocationHandler.invoke(invocation);
    }

1.7 DubboRPCContextClientFilter

设置Consumer端Dubbo RPCContext参数

    public ListenableFuture<RPCResult> invoke(InvocationHandler nextHandler, Invocation invocation) throws Throwable {
        ListenableFuture var6;
        try {
            RpcContext rpcContext = RpcContext.getContext();
            if (rpcContext.getAttachments() != null) {
                rpcContext.getAttachments().remove("interface");
            }

            if (rpcContext.getAttachments() != null && !rpcContext.getAttachments().isEmpty()) {
                invocation.getHsfRequest().setRequestProps("rc", new HashMap(RpcContext.getContext().getAttachments()));
            }

            ListenableFuture<RPCResult> result = nextHandler.invoke(invocation);
            ServiceURL serviceURL = invocation.getInvokerContext().getUrl();

            try {
                if (serviceURL == null) {
                    rpcContext.setUrl(new URL("localhost", "127.0.0.1", 12200));
                    rpcContext.setLocalAddress(new InetSocketAddress(0));
                    rpcContext.setRemoteAddress(new InetSocketAddress(0));
                } else {
                    rpcContext.setUrl(URL.valueOf(serviceURL.getUrl()));
                    rpcContext.setLocalAddress(new InetSocketAddress(0));
                    rpcContext.setRemoteAddress(serviceURL.getHost(), serviceURL.getPort());
                }

                rpcContext.setMethodName(invocation.getHsfRequest().getMethodName());
                rpcContext.setParameterTypes(invocation.getHsfRequest().getParameterClasses());
                rpcContext.setArguments(invocation.getHsfRequest().getMethodArgs());
            } catch (Throwable var10) {
                ;
            }

            var6 = result;
        } finally {
            RpcContext.getContext().clearAttachments();
        }

        return var6;
    }

1.8 RPCContextClientFilter

设置HSF中RPCContext的Consumer端属性

    public ListenableFuture<RPCResult> invoke(InvocationHandler nextHandler, Invocation invocation) throws Throwable {
        if (RPCContext.clientContextUsed) {
            Map attachments = RPCContext.getClientContext().getAttachments();
            if (attachments != null) {
                invocation.getHsfRequest().setRequestProps("_hsf_rpc_context_key", attachments);
            }

            ListenableFuture<RPCResult> result = nextHandler.invoke(invocation);
            this.linkClientContext(invocation);
            return result;
        } else {
            return nextHandler.invoke(invocation);
        }
    }

相关文章

网友评论

      本文标题:hsf笔记-ClientFilter

      本文链接:https://www.haomeiwen.com/subject/oajgbftx.html