<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

    Java專題文章博客和開源

    常用鏈接

    統(tǒng)計

    最新評論

    基于Spring AOP實現(xiàn)對外接口的耗時監(jiān)控

      AOP是Spring的核心,Spring不但自身對多種框架的集成是基于AOP,并且以非常方便的形式暴露給普通使用者。以前用AOP不多,主要是因為它以橫截面的方式插入到主流程中,擔(dān)心導(dǎo)致主流程代碼不夠清晰,定位問題不夠方便,而在計費(fèi)二期的項目里需要一個很適合用AOP來做的功能,就是要把對外接口和所調(diào)用的外部接口的耗時時間給記錄下來,這個需求主要來自于計費(fèi)一期的聯(lián)調(diào),常常發(fā)生系統(tǒng)間交互不夠順暢的情況,這就需要看每個接口調(diào)用時間來判定是誰的問題。

      計費(fèi)中心是整個后臺系統(tǒng)的中間環(huán)節(jié),與其他系統(tǒng)交互很多,這樣的接口也很多,如果在每個接口的調(diào)用前后加時間記錄比較繁瑣,也影響主流程代碼的美觀,因此比較優(yōu)雅的方式是用AOP,在不侵入原有代碼的情況下,加上對接口調(diào)用的監(jiān)控,并且可以在不需要的時候很容易移除。今天嘗試了一下,感覺還挺好用,下面講述一下實施步驟:托福答案

      1)引入包依賴

      本項目基于maven構(gòu)建,因此加上包依賴比較方便,我需要的AOP依賴庫有以下三個:

      [xhtml]

      <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aop</artifactId>

      <version>2.5.6</version>

      </dependency>

      <dependency>

      <groupId>org.aspectj</groupId>

      <artifactId>aspectjweaver</artifactId>

      <version>1.6.1</version>

      </dependency>

      <dependency>

      <groupId>org.aspectj</groupId>

      <artifactId>aspectjrt</artifactId>

      <version>1.6.1</version>

      </dependency>

      <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aop</artifactId>

      <version>2.5.6</version>

      </dependency>

      <dependency>

      <groupId>org.aspectj</groupId>

      <artifactId>aspectjweaver</artifactId>

      <version>1.6.1</version>

      </dependency>

      <dependency>

      <groupId>org.aspectj</groupId>

      <artifactId>aspectjrt</artifactId>

      <version>1.6.1</version>

      </dependency>

      2)加上AOP的Spring配置文件

      billing-spring-aop.xml:

      [xhtml]

      <?xml version="1.0" encoding="UTF-8"?>

      <beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:aop="http://www.springframework.org/schema/aop"

      xmlns:tx="http://www.springframework.org/schema/tx"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

      <bean id="openApiLogAspect" class="com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect">

      </bean>

      <aop:config>

      <!-- 配置aspect切面類 -->

      <aop:aspect ref="openApiLogAspect">

      <!-- 配置pointcut,即切入點(diǎn),對哪些類的哪些方法起到AOP的作用 -->

      <aop:pointcut id="EsbPriceService"

      expression="execution(* org.esb.biz.product.EsbPriceService.*())" />

      <aop:pointcut id="EsbProductService"

      expression="execution(* org.esb.biz.product.EsbProductService.*())" />

      <aop:pointcut id="IAuthorizeControllerService"

      expression="execution(* com.alibaba.bss.pc2.server.remoting.IAuthorizeControllerService.*())" />

      <aop:pointcut id="IOpenApiOrderItemService"

      expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiOrderItemService.*())" />

      <aop:pointcut id="IOpenApiBillingCollectService"

      expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiBillingCollectService.*())" />

      <aop:pointcut id="IOpenApiInvoiceService"

      expression="execution(* com.alibaba.itbu.billing.api.invoice.IOpenApiInvoiceService.*())" />

      <aop:pointcut id="IOpenApiChargeProductInfoService"

      expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiChargeProductInfoService.*())" />

      <!-- 配置advice,這里采用在業(yè)務(wù)方法執(zhí)行前后進(jìn)行攔截 -->

      <aop:around method="logExecuteTime" pointcut-ref="EsbPriceService" />

      <aop:around method="logExecuteTime" pointcut-ref="EsbProductService" />

      <aop:around method="logExecuteTime" pointcut-ref="IAuthorizeControllerService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiOrderItemService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiBillingCollectService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiInvoiceService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiChargeProductInfoService" />

      </aop:aspect>

      </aop:config>

      </beans>

      <?xml version="1.0" encoding="UTF-8"?>

      <beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:aop="http://www.springframework.org/schema/aop"

      xmlns:tx="http://www.springframework.org/schema/tx"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

      <bean id="openApiLogAspect" class="com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect">

      </bean>

      <aop:config>

      <!-- 配置aspect切面類 -->

      <aop:aspect ref="openApiLogAspect">

      <!-- 配置pointcut,即切入點(diǎn),對哪些類的哪些方法起到AOP的作用 -->

      <aop:pointcut id="EsbPriceService"

      expression="execution(* org.esb.biz.product.EsbPriceService.*())" />

      <aop:pointcut id="EsbProductService"

      expression="execution(* org.esb.biz.product.EsbProductService.*())" />

      <aop:pointcut id="IAuthorizeControllerService"

      expression="execution(* com.alibaba.bss.pc2.server.remoting.IAuthorizeControllerService.*())" />

      <aop:pointcut id="IOpenApiOrderItemService"

      expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiOrderItemService.*())" />

      <aop:pointcut id="IOpenApiBillingCollectService"

      expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiBillingCollectService.*())" />

      <aop:pointcut id="IOpenApiInvoiceService"

      expression="execution(* com.alibaba.itbu.billing.api.invoice.IOpenApiInvoiceService.*())" />

      <aop:pointcut id="IOpenApiChargeProductInfoService"

      expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiChargeProductInfoService.*())" />

      <!-- 配置advice,這里采用在業(yè)務(wù)方法執(zhí)行前后進(jìn)行攔截 -->

      <aop:around method="logExecuteTime" pointcut-ref="EsbPriceService" />

      <aop:around method="logExecuteTime" pointcut-ref="EsbProductService" />

      <aop:around method="logExecuteTime" pointcut-ref="IAuthorizeControllerService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiOrderItemService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiBillingCollectService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiInvoiceService" />

      <aop:around method="logExecuteTime" pointcut-ref="IOpenApiChargeProductInfoService" />

      </aop:aspect>

      </aop:config>

      </beans>

      我是基于配置完成AOP接入,這樣做的好處是不需要對原有主流程代碼有任何浸入,并且也比較容易移除本AOP的攔截,這段代碼主要就是配置aspect、pointcut和advice托福考前答案

      3)編寫監(jiān)控耗時的advice

      OpenApiLogAspect:

      [java]

      public class OpenApiLogAspect {

      private static LoggerService logger = LoggerFactory.getLogger(OpenApiLogAspect.class);

      public Object logExecuteTime(ProceedingJoinPoint joinPoint) throws Throwable{

      Date start = new Date();

      try{

      return joinPoint.proceed(joinPoint.getArgs());

      }catch(Exception err){

      throw err;

      }finally{

      Date end = new Date();

      logger.info("OpenApiExecuteTime:"+joinPoint.getSignature()。getName()+" takes "+(end.getTime()-start.getTime())+"ms");

      }

      }

      }

      public class OpenApiLogAspect {

      private static LoggerService logger = LoggerFactory.getLogger(OpenApiLogAspect.class);

      public Object logExecuteTime(ProceedingJoinPoint joinPoint) throws Throwable{

      Date start = new Date();

      try{

      return joinPoint.proceed(joinPoint.getArgs());

      }catch(Exception err){

      throw err;

      }finally{

      Date end = new Date();

      logger.info("OpenApiExecuteTime:"+joinPoint.getSignature()。getName()+" takes "+(end.getTime()-start.getTime())+"ms");

      }

      }

      }

      此段代碼就是基于around的方式來攔截接口調(diào)用,在實際調(diào)用的前后加上時間記錄,并最后在日志里打印出時間差。其中joinPoint.proceed(joinPoint.getArgs());是對實際接口的調(diào)用。

      4)使監(jiān)控可以配置化

      此功能只會在調(diào)試階段使用,并不需要在生產(chǎn)環(huán)境中運(yùn)行,因此需要可以配置是否監(jiān)控接口。實施這個配置化很簡單,只需要通過配置決定是否把a(bǔ)op spring的配置文件加入到容器里就可以了,因此在總?cè)萜鱝pplicationContext.xml.vm里加上如下代碼:托福改分

      #if(${monitor_openapi_showTime}=="true")

      <import resource="classpath*:bean/billing-spring-aop.xml" />

      #end

      在編譯打包過程中會根據(jù)變量monitor_openapi_showTime來決定是否把billing-spring-aop.xml引入進(jìn)來

      5)運(yùn)行效果

      在監(jiān)控開啟的情況下,若發(fā)生接口調(diào)用,能從日志里看到如下記錄:

      2010-01-08 18:30:13,197 [OpenApiLogAspect.java:20] [com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect] INFO com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect :: OpenApiExecuteTime:installOrderItem takes 71ms

      2010-01-08 18:30:27,188 [OpenApiLogAspect.java:20] [com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect] INFO com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect :: OpenApiExecuteTime:installOrderItem takes 0ms

      2010-01-08 18:30:37,838 [OpenApiLogAspect.java:20] [com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect] INFO com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect :: OpenApiExecuteTime:installOrderItem takes 1ms

     

    posted on 2013-09-02 09:24 好不容易 閱讀(289) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 美女黄网站人色视频免费国产| 日韩免费码中文在线观看| 亚洲色大网站WWW永久网站| 亚洲AV无码XXX麻豆艾秋| 18pao国产成视频永久免费| 亚洲最大AV网站在线观看| 亚洲国产成人久久一区二区三区| 18成禁人视频免费网站| 亚洲av永久无码精品国产精品| 一级毛片免费视频网站| 国产又大又长又粗又硬的免费视频 | 100000免费啪啪18免进| 亚洲视频在线观看| 国产成人免费ā片在线观看老同学| 亚洲精品成人网久久久久久| 亚洲精品天堂无码中文字幕| 在线观看日本免费a∨视频| 亚洲欧洲日产国码二区首页| 永久免费在线观看视频| 亚洲av永久中文无码精品| 免费很黄很色裸乳在线观看| 伊人免费在线观看| 亚洲乱码卡三乱码新区| 免费毛片网站在线观看| 精品久久久久久国产免费了| 亚洲国产精久久久久久久| 一本无码人妻在中文字幕免费| 久久精品国产亚洲av瑜伽| 亚洲欧洲无码AV电影在线观看 | 亚洲精品乱码久久久久久V| 亚洲国产婷婷综合在线精品| 久久99毛片免费观看不卡| 亚洲中文无码a∨在线观看| 国产无遮挡吃胸膜奶免费看| 在线观看片免费人成视频无码| 亚洲国产成a人v在线观看| 亚洲成AⅤ人影院在线观看| 99久久免费精品高清特色大片| 亚洲国产无线乱码在线观看 | 久久精品国产亚洲综合色| 一二三四视频在线观看中文版免费|