Why to monitor
Service interface is to provide service, interface correctness, stability is the most important, in ensuring that the correct and the need to maximize the interface response time.
Some teams have specialized tools to monitor system response time and throughput, but if the team does not have this "treatment" it will need to do some tools to service its code.
DIY, clothed
AOP + Annotation Simple implementation, can achieve the purpose
AOP: Use of wrapping to intercept interface, recording time before and after the interception interface last calculated
Annotation: Custom Annotations Set time-out (timeout) andtime-outs to send mail options on the interface (emailiftimeout)
By comparing the actual execution time of the interface to the time-out of the configuration, the system can calculate whether the interface timed out, at which point the log (or other way to notify the developer) can be used to record which interface, what parameters, and the execution time
Annotations can provide more options for their own interface services, such as support annotations to the class, the bulk for the interface set the default time-out, the support log display of the processing method name and so on ...
Code implementation
interface annotation Definition
/** * Interface Custom Properties * * @author Tianshu on 16/8/30 4:55. */@Target (value = {Elementtype.method}) @Retention (retentionpolicy.runtime) public @interface Interfaceproperty { /** * Interface timeout, in milliseconds. Default value 100 milliseconds * @return set timeout time * /int timeout () default; /** * Whether the message is sent when the interface response times out. Default Send * @return return ture need to send message * /boolean emailiftimeout () default true;}
AOP implementations
/** * @author Tianshu on 16/1/28 10:35. */@Component @aspectpublic class Systemrequestaspect {/** log */private static final Logger log = Loggerfactory.getl Ogger (Systemrequestaspect.class); /** Interface Timeout log */private static final Logger Interface_timeout_log = Loggerfactory.getlogger ("Interface_timeout_log"); @Around (value = "Execution (* com.xx.xx.xx.xx): * * (..)) ", argnames=" PJP ") Public Object Validator (Proceedingjoinpoint pjp) throws Throwable {object[] args = P Jp.getargs (); /** Intercept Method Name */String MethodName = Pjp.gettarget (). GetClass (). Getsimplename () + "." + pjp.getsignature (). GetName (); try {Long start = System.currenttimemillis (); Object obj = pjp.proceed (args); Long finish = System.currenttimemillis (); Long usetime = Finish-start;
/** interface Response Time monitoring */Interfaceusetimemonitor (Pjp.gettarget (). GetClass (), Pjp.getsignature (). GetName (), a RGS, Usetime); return obj; } catch (Throwable e) {//handle your exception} finally {//Handle other}}/** * Interface response Time monitoring * * @param targetclass The interface implements class * @param MethodName interface method * @param the Args interface such as parameter * @param usetime call Interface Actual usage time */private void inte Rfaceusetimemonitor (Class targetclass, String methodName, object[] args, long usetime) {/** Compared with interface annotations, eligible to send mail */try {class[] Classarray = new Class[args.length]; for (int i = 0; i < args.length; ++i) {Classarray[i] = Args[i].getclass (); } method = Targetclass.getmethod (MethodName, Classarray); if (Method.isannotationpresent (Interfaceproperty.class)) {Interfaceproperty Interfaceproperty = Method.getA Nnotation (Interfaceproperty.class); if (Usetime >= inTerfaceproperty.timeout ()) {if (interface_timeout_log.isinfoenabled ()) {Interfa Ce_timeout_log.info ("Interface timeout, interface:[{}].usetime:[{}].settingusetime:[{}].traceid:[{}]", NE W object[]{targetclass.getsimplename () + "." + MethodName, Usetime, Interfaceproper Ty.timeout (), Traceutils.gettrace ()}); }}}} catch (Throwable e) {/** monitor logic processing error do nothing */}}}
Java interface response time-out monitoring