Developing Dubbo filter under the Spring boot framework

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.