利用xfire框架攔截器(Handler)實現介面訪問日誌列印
阿新 • • 發佈:2018-12-11
前景:
因專案中釋出了80+個介面,需要給每個介面加上請求和響應的日誌資訊。(PS:沒日誌很蛋疼老是背鍋。)給這些介面加上日誌資訊的同時還必須滿足介面功能不受影響。
方案:
根據研究發現可以使用xfire的handler攔截器實現,原理見如下請求示意圖。
具體實現:
1.編寫JAVA類 繼承 org.codehaus.xfire.handler.AbstractHandler 重寫 invoke方法。
inHandler
import com.alibaba.fastjson.JSON; import com.fastunit.util.DateUtil; import java.lang.reflect.Method; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.exchange.MessageExchange; import org.codehaus.xfire.handler.AbstractHandler; import org.codehaus.xfire.service.OperationInfo; public class WebserviceServerHandler extends AbstractHandler { public void invoke(MessageContext context) throws Exception { try { print(context); } catch (Exception localException) { } } public static void print(MessageContext context) { try { MessageExchange exchange = context.getExchange(); OperationInfo opinfo = exchange.getOperation(); Method requestM = opinfo.getMethod();//獲取到客戶端請求的介面物件 //context.getInMessage().getBody() 這裡返回的是客戶端傳送請求的資訊 System.out.println(); System.out.println(); //輸出請求的介面名稱 System.out.println("===============================" + DateUtil.getCurrentDatetime() + "====BEGIN REQUEST "+ requestM.getName() + "========================================="); //輸出請求的介面引數 System.out.println("REQUEST PARAMS:\n[METHOD=" + requestM.getName() + "]:\t" + JSON.toJSONString(context.getInMessage().getBody())); } catch (Exception localException) { } } }
outHandler
import com.alibaba.fastjson.JSON; import com.fastunit.util.DateUtil; import java.lang.reflect.Method; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.exchange.MessageExchange; import org.codehaus.xfire.handler.AbstractHandler; import org.codehaus.xfire.service.OperationInfo; public class WebserviceServerOutHandler extends AbstractHandler { public void invoke(MessageContext context) throws Exception { try { print(context); } catch (Exception localException) { } } public void print(MessageContext context) { MessageExchange exchange = context.getExchange(); OperationInfo opinfo = exchange.getOperation(); Method responseM = opinfo.getMethod(); System.out.println("RESPONSE MSG:\n[METHOD = " + responseM.getName() + "]:\t" + JSON.toJSONString(context.getOutMessage().getBody())); System.out.println("===============================" + DateUtil.getCurrentDatetime() + "====OVER RESPONSE " + responseM.getName() + "========================================="); } }
2.配置xfire攔截器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>services</name> <namespace>http://xxx.com</namespace> <serviceClass>com.xxxxx.IServices</serviceClass> <implementationClass>com.xxxxx.ServicesImpl</implementationClass> <inHandlers> <handler handlerClass="com.xxx.WebserviceServerHandler"/> </inHandlers> <outHandlers> <handler handlerClass="com.xxx.WebserviceServerOutHandler"/> </outHandlers> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service> </beans>
3.顯示效果
===============================2018-09-19 04:53:27====BEGIN REQUEST connectHost=========================================
REQUEST PARAMS:
[METHOD=connectHost]: [{"code":"13476165039140","encryption":"0","expiration":"2099-12-31","groupid":"0","host":"218.70.82.202","port":"1000","signature":"302c021405d2a181d2cfdd69659b9599b24e949539eba5d6021458ed9671e6ebe72da05bf7507e5910c95b943a92","version":"1.0.6"},"plmuser","123456","2","5",""]
RESPONSE MSG:
[METHOD = connectHost]: ["1537304007780408"]
===============================2018-09-19 04:53:27====OVER RESPONSE connectHost=========================================