[dubbo] Custom filter printing interface request log

Target

When the application calls the dubbo interface or the provided dubbo interface is called, the input parameter information, service name, method name and return value of the interface request are printed through the custom filter.

Implement filter

1. Custom filter implementation class

@Slf4j
@Activate(group = {
    
    "provider", "consumer"})
public class DubboProviderLogFilter implements Filter {
    
    

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    
    
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("service", invocation.getTargetServiceUniqueName());
        map.put("method", invocation.getMethodName());
        map.put("params", getParams(invocation.getArguments(), invocation.getParameterTypes()));

        Result result = null;
        try {
    
    
            result = invoker.invoke(invocation);
            map.put("result", result.getValue());
            log.info("dubbo_request_log={}", JSONObject.toJSONString(map));
            return result;
        } catch (Exception e) {
    
    
            log.error("dubbo_request_catch_exception={}, e", JSONObject.toJSONString(map));
            throw new RuntimeException(e);
        }
    }

    private Map<String, Argument> getParams(Object[] argvs, Class<?>[] argts) {
    
    
        Map<String, Argument> map = new LinkedHashMap<>();
        int size = argvs.length;
        try {
    
    
            for (int i = 0; i < size; i++) {
    
    
                map.put("param" + i, new Argument(JSONObject.toJSONString(argts[i]).replaceAll("\"", ""), argvs[i]));
            }
        } catch (Exception e) {
    
    
            log.error("getParams_catch_exception=", e);
        }
        return map;
    }

    @Data
    @AllArgsConstructor
    private static class Argument {
    
    
        private String type;
        private Object value;
    }
}

The group parameter in the annotation @Activateis used to specify the effective scope of the filter. Provider and consumer are both added here. Then the application will print logs when calling the dubbo interface and the dubbo interface provided by itself.

2. Configure filter

Create a file in the resource directory META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter
and write the following content:
dubboRequestLog=com.yourapp.path.DubboProviderLogFilter
Insert image description here

3. Make the filter effective

There are three ways

  1. In the custom filter implementation class, @Activatespecifying whether the service provider or consumer takes effect through the annotated group parameter, or you can choose to take effect at the same time.
  2. Specified in the filter attribute of the service provider
  3. In the xml configuration file, set the filter uniformly
    <dubbo:provider filter="dubboRequestLog"/>or <dubbo:consumer filter="dubboRequestLog"/>configure the filter to be used through filter. This is for all service providers or service consumers.

Guess you like

Origin blog.csdn.net/u011308433/article/details/132271868