1. To implement a filter, note that you want to write the annotations.
Importjava.util.Date;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importcom.alibaba.dubbo.common.extension.Activate;ImportCom.alibaba.dubbo.rpc.Filter;Importcom.alibaba.dubbo.rpc.Invocation;ImportCom.alibaba.dubbo.rpc.Invoker;ImportCom.alibaba.dubbo.rpc.Result;Importcom.alibaba.dubbo.rpc.RpcException;ImportCom.alibaba.fastjson.JSON;Importcom.google.common.base.Throwables; @Activate Public classDubbologfilterImplementsFilter {protected Final StaticLogger Logger = Loggerfactory.getlogger (dubbologfilter.class); @Override PublicResult Invoke (invoker<?> Invoker, invocation invocation)throwsrpcexception {invokelog Invokelog=NewInvokelog (); //Set Request TimeInvokelog.setstarttime (NewDate ()); //set up Request messagessetrequest (invocation, invokelog); //interface Call start timeResult result =NULL; Try{result=Invoker.invoke (invocation); if(Result.hasexception ()) {//If there is an exceptionInvokelog.setflag ("Failure"); Invokelog.setresponsedata (Throwables.getstacktraceasstring (Result.getexception ())); } Else { //If successful if(Result.getvalue ()! =NULL) {String returnstr=json.tojsonstring (Result.getvalue ()); Invokelog.setresponsedata (RETURNSTR); } //TODO Dubbo Interface If no return value is temporarily unhandled } } Catch(RuntimeException e) {Invokelog.setflag ("Failure"); Invokelog.setresponsedata (Throwables.getstacktraceasstring (Result.getexception ())); } Catch(Exception e) {Invokelog.setflag ("Failure"); Invokelog.setresponsedata (Throwables.getstacktraceasstring (Result.getexception ())); } finally { //Interface Call durationInvokelog.setendtime (NewDate ()); Invokelog.setresponsetime (Invokelog.getendtime (). GetTime ()-invokelog.getstarttime (). GetTime ()); //Springhelper.getbean ("Invokelogservice", Invokelogservice.class). Insert (Invokelog);} System.out.println (Json.tojsonstring (Invokelog)); returnresult; } /*** SET interface invocation parameters **/ protected voidsetrequest (invocation invocation, Invokelog invokelog) {Try{object[] arguments=invocation.getarguments (); if(Arguments! =NULL&& arguments.length > 0) {String requeststr=json.tojsonstring (Invocation.getarguments ()); Invokelog.setrequestdata (REQUESTSTR); } } Catch(Exception e) {//cannot affect Dubbo interface OperationInvokelog.setrequestdata (throwables.getstacktraceasstring (e)); } }}
2.resources folder under the new directory Meta-inf\dubbo, in this directory to increase the file Com.alibaba.dubbo.rpc.Filter, the file content is:
Dubbologfilter=com.example.dubbotest.base.dubbologfilter
3. Add configuration under the Application.properties folder Spring.dubbo.provider.filter=dubbologfilter
4. After the above configuration, the filter of the Dubbo will take effect, and when a consumer invokes the service, the filter will be triggered
Dubbo's filter has many applications in microservices, such as logging, tracking, and black-and-white lists.
Developing Dubbo filter under the Spring boot framework