background

In the current project, remote service invocations are all based on Dubbo, with some departments calling each other internally and some calling services from other departments. Because many services involve remote invocation, it is troublesome to manually write out input parameters, responses and exceptions when invoking each service.

This section is now optimized to automatically print inputs, responses, and exceptions to avoid having to manually write duplicate code for each service.

implementation

1. Implement filters

The new package

Xxx.solid. Filter // Ends with filter

New Class – Custom service consumer log interceptor

package XXX.solid.filter; import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.extension.Activate; import com.alibaba.dubbo.rpc.*; import com.alibaba.dubbo.rpc.service.GenericService; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; /** * service consumer log interceptor, @author gongzhihao */ @slf4j @activate (group = {Constants.CONSUMER}) public class LogDubboConsumerFilter implements Filter { @Override public Result invoke(Invoker<? > Invoker, Invocation) throws RpcException {// Print the log.info("Dubbo entry, InterfaceName={},MethodName={},Parameter={}", invocation.getInvoker().getInterface().getName(), invocation.getMethodName(), invocation.getArguments()); // startTime long startTime = system.currenttimemillis (); Invocation Result Result = Invocation. Invoke (Invocation); Elapsed = System.currentTimemillis () - startTime; // If an exception occurs, an exception log is printedif(result.hasException() && invoker.getInterface() ! = GenericService.class) { // log.error("Dubbo execution exception:", result.getException());
            log.error("Dubbo execution exception!!");
        } else {
            log.info("Dubbo response, InterfaceName={},MethodName={},Resposne={},SpendTime={} ms", invocation.getInvoker().getInterface().getName(), invocation.getMethodName(), JSON.toJSONString(new Object[]{result.getValue()}), elapsed); } // Return the result response datareturnresult; }}Copy the code

New Class – Custom service provider log interceptor

package XXX.solid.filter; import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.extension.Activate; import com.alibaba.dubbo.rpc.*; import com.alibaba.dubbo.rpc.service.GenericService; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; /** * service provider log interceptor, @author gongzhihao */ @slf4j @activate (group = {Constants.PROVIDER}) public class LogDubboProviderFilter implements Filter { @Override public Result invoke(Invoker<? > Invoker, Invocation) throws RpcException {// Print the log.info("Dubbo entry, InterfaceName={},MethodName={},Parameter={}", invocation.getInvoker().getInterface().getName(), invocation.getMethodName(), invocation.getArguments()); // startTime long startTime = system.currenttimemillis (); Invocation Result Result = Invocation. Invoke (Invocation); Elapsed = System.currentTimemillis () - startTime; // If an exception occurs, an exception log is printedif(result.hasException() && invoker.getInterface() ! = GenericService.class) { // log.error("Dubbo response, dubbo execution exception:", result.getException());
            log.error("Dubbo execution exception!!");
        } else {
            log.info("Dubbo Response, InterfaceName={},MethodName={},Response={},SpendTime={} ms", invocation.getInvoker().getInterface().getName(), invocation.getMethodName(), JSON.toJSONString(new Object[]{result.getValue()}), elapsed); } // Return the result response datareturnresult; }}Copy the code

2. The configuration

A, configuration,

New directories and files: meta-inf/dubbo/org. Apache. Dubbo. RPC. The Filter

Add configuration:

logDubboProviderFilter=XXX.solid.filter.LogDubboProviderFilter
logDubboConsumerFilter=XXX.solid.filter.LogDubboConsumerFilter
Copy the code

Second, to enable

There are two ways to enable filters

1. Based on annotations

@Activate(group = { Constants.PROVIDER })
Copy the code

2. Based on the configuration file

1. Springboot project-application. properties configuration file dubo.providerlogDubboProviderFilter
dubbo.consumer.filter=logDubboConsumerFilter 2. Spring project -xxx.xml configuration file <dubbo: Provider filter="logDubboProviderFilter"/> 

<dubbo:consumer retries="0" timeout="60000"  loadbalance="roundrobin" actives="0"
                validation="false" filter="logDubboConsumerFilter"/>
Copy the code

Disable dubbo’s log blocker

The built-in AccessLog configuration prints input parameters only, and only at the service provider.

If accessLog is configured, delete it to avoid duplicate logs.

abnormal

1. Dubbo has its own exception interceptor. Use the default exception interceptor and do not change it.

2. If you need to print service information in the exception log (for example, add the exception description or locate the abnormal order based on the order ID), you can also capture the exception log and throw the exception.

reference

Dubbo.apache.org/zh-cn/docs/…