AOP是Spring的核心,Spring不但自身对多种框架的集成是基于AOP,并且以非常方便的形式暴露给普通使用者。以前用AOP不多,主要是因为它以横截面的方式插入到主流程中,担心导致主流程代码不够清晰,定位问题不够方便,而在计费二期的项目里需要一个很适合用AOP来做的功能,就是要把对外接口和所调用的外部接口的耗时时间给记录下来,这个需求主要来自于计费一期的联调,常常发生系统间交互不够顺畅的情况,这就需要看每个接口调用时间来判定是谁的问题。

计费中心是整个后台系统的中间环节,与其他系统交互很多,这样的接口也很多,如果在每个接口的调用前后加时间记录比较繁琐,也影响主流程代码的美观,因此比较优雅的方式是用AOP,在不侵入原有代码的情况下,加上对接口调用的监控,并且可以在不需要的时候很容易移除。今天尝试了一下,感觉还挺好用,下面讲述一下实施步骤:

1)引入包依赖

本项目基于maven构建,因此加上包依赖比较方便,我需要的AOP依赖库有以下三个:

[xhtml] view plaincopy

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-aop</artifactId>
  4. <version>2.5.6</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.aspectj</groupId>
  8. <artifactId>aspectjweaver</artifactId>
  9. <version>1.6.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.aspectj</groupId>
  13. <artifactId>aspectjrt</artifactId>
  14. <version>1.6.1</version>
  15. </dependency>

2)加上AOP的Spring配置文件

billing-spring-aop.xml:

[xhtml] view plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <bean id="openApiLogAspect" class="com.alibaba.itbu.billing.framework.aop.OpenApiLogAspect">
  10. </bean>
  11. <aop:config>
  12. <!-- 配置aspect切面类 -->
  13. <aop:aspect ref="openApiLogAspect">
  14. <!-- 配置pointcut,即切入点,对哪些类的哪些方法起到AOP的作用 -->
  15. <aop:pointcut id="EsbPriceService"
  16. expression="execution(* org.esb.biz.product.EsbPriceService.*(..))" />
  17. <aop:pointcut id="EsbProductService"
  18. expression="execution(* org.esb.biz.product.EsbProductService.*(..))" />
  19. <aop:pointcut id="IAuthorizeControllerService"
  20. expression="execution(* com.alibaba.bss.pc2.server.remoting.IAuthorizeControllerService.*(..))" />
  21. <aop:pointcut id="IOpenApiOrderItemService"
  22. expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiOrderItemService.*(..))" />
  23. <aop:pointcut id="IOpenApiBillingCollectService"
  24. expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiBillingCollectService.*(..))" />
  25. <aop:pointcut id="IOpenApiInvoiceService"
  26. expression="execution(* com.alibaba.itbu.billing.api.invoice.IOpenApiInvoiceService.*(..))" />
  27. <aop:pointcut id="IOpenApiChargeProductInfoService"
  28. expression="execution(* com.alibaba.itbu.billing.api.collect.IOpenApiChargeProductInfoService.*(..))" />
  29. <!-- 配置advice,这里采用在业务方法执行前后进行拦截 -->
  30. <aop:around method="logExecuteTime" pointcut-ref="EsbPriceService" />
  31. <aop:around method="logExecuteTime" pointcut-ref="EsbProductService" />
  32. <aop:around method="logExecuteTime" pointcut-ref="IAuthorizeControllerService" />
  33. <aop:around method="logExecuteTime" pointcut-ref="IOpenApiOrderItemService" />
  34. <aop:around method="logExecuteTime" pointcut-ref="IOpenApiBillingCollectService" />
  35. <aop:around method="logExecuteTime" pointcut-ref="IOpenApiInvoiceService" />
  36. <aop:around method="logExecuteTime" pointcut-ref="IOpenApiChargeProductInfoService" />
  37. </aop:aspect>
  38. </aop:config>
  39. </beans>

我是基于配置完成AOP接入,这样做的好处是不需要对原有主流程代码有任何浸入,并且也比较容易移除本AOP的拦截,这段代码主要就是配置aspect、pointcut和advice

3)编写监控耗时的advice

OpenApiLogAspect:

  1. public class OpenApiLogAspect {
  2. private static LoggerService logger = LoggerFactory.getLogger(OpenApiLogAspect.class);
  3. public Object logExecuteTime(ProceedingJoinPoint joinPoint) throws Throwable{
  4. Date start = new Date();
  5. try{
  6. return joinPoint.proceed(joinPoint.getArgs());
  7. }catch(Exception err){
  8. throw err;
  9. }finally{
  10. Date end = new Date();
  11. logger.info("OpenApiExecuteTime:"+joinPoint.getSignature().getName()+" takes "+(end.getTime()-start.getTime())+"ms");
  12. }
  13. }
  14. }

此段代码就是基于around的方式来拦截接口调用,在实际调用的前后加上时间记录,并最后在日志里打印出时间差。其中joinPoint.proceed(joinPoint.getArgs());是对实际接口的调用。

4)使监控可以配置化

此功能只会在调试阶段使用,并不需要在生产环境中运行,因此需要可以配置是否监控接口。实施这个配置化很简单,只需要通过配置决定是否把aop spring的配置文件加入到容器里就可以了,因此在总容器applicationContext.xml.vm里加上如下代码:

#if(${monitor_openapi_showTime}=="true")  <import resource="classpath*:bean/billing-spring-aop.xml" />  #end

在编译打包过程中会根据变量monitor_openapi_showTime来决定是否把billing-spring-aop.xml引入进来

5)运行效果

在监控开启的情况下,若发生接口调用,能从日志里看到如下记录:

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

最新文章

  1. 阶段一:AsyncTask的三个属性值和四个步骤
  2. 山东第一届省赛1001 Phone Number(字典树)
  3. Poj2033
  4. apache 虚拟主机详细配置:http.conf配置详解
  5. Oracle 常用函数
  6. ThinkPHP3.2.3--相对路径的写法
  7. 重新想象 Windows 8.1 Store Apps (76) - 新增控件: SearchBox
  8. C# HttpWebRequest 绝技
  9. C puzzles详解【21-25题】
  10. HDU 3308 LCIS (线段树区间合并)
  11. Android Studio快速生成get set等函数
  12. github 中redisPhpAdmin redis 可视化界面
  13. JavaBean中DAO设计模式介绍
  14. Linode开通新加坡机房:vps速度快,价格不变!
  15. SpringBoot系列: 设计Restful风格的API
  16. Tsung&#160;超详细的的tsung性能测试资料
  17. Oracle存储过程的调试
  18. java面试题:多线程与并发
  19. protobuf 语法简介
  20. UI设计不就是画线框,凭什么年薪30W?

热门文章

  1. 【redis】01Redis的介绍与安装部署
  2. HDU4276 The Ghost Blows Light SPFA&amp;&amp;树dp
  3. HDU 1540 / POJ 2892 Tunnel Warfare (单点更新,区间合并,求包含某点的最大连续个数)
  4. grunt安装失败处理
  5. 无法解析指定的连接标识符 oracle错误12154
  6. cojs 白树黑 黑树白 题解报告
  7. 李洪强iOS开发之keychain的使用
  8. java io流缓冲理解
  9. 从零开始,让你的框架支持CocoaPods
  10. POJ2891——Strange Way to Express Integers(模线性方程组)