目录
一、AOP简介
1.1、定义
wiki:面向切面的编程
个人理解:利用切面思想,将与程序无关的业务和当前程序解耦开,并进行监控或者后续处理等功能。
1.2、AOP五种通知类型
(1)、Before:在目标方法被调用之前做增强处理,@Before只需要指定切入点表达式即可
(2)、AfterReturning:在目标方法正常完成后做增强,@AfterReturning除了指定切入点表达式后,还可以指定一个返回值形参名returning,代表目标方法的返回值
(3)、AfterThrowing:主要用来处理程序中未处理的异常,@AfterThrowing除了指定切入点表达式后,还可以指定一个throwing的返回值形参名,可以通过该形参名
来访问目标方法中所抛出的异常对象
(4)、After:在目标方法完成之后做增强,无论目标方法时候成功完成。@After可以指定一个切入点表达式
(5)、Around:环绕通知,在目标方法完成前后做增强处理,环绕通知是最重要的通知类型,像事务,日志等都是环绕通知,注意编程中核心是一个ProceedingJoinPoint
二、Hystrix和AOP的结合
主要利用了aop的Around环绕通知功能。
(1)Hystrix监控
import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.*;
import org.aopalliance.intercept.MethodInvocation;
/**
* Created by JackDing on 18/10/25.
*/
public class HelloWorldCommand extends HystrixCommand<String> {
private final String name;
private MethodInvocation methodInvocation;
public HelloWorldCommand(String name,MethodInvocation methodInvocation) {
super(
Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ServiceGroup"))//命名组
.andCommandKey(HystrixCommandKey.Factory.asKey("servcie1query")) //命名名称
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("service1ThreadPool")) //命令线程池
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withCoreSize(20))//服务线程池数量
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerErrorThresholdPercentage(60)//熔断器关闭到打开阈值
.withCircuitBreakerSleepWindowInMilliseconds(3000)//熔断器打开到关闭的时间窗长度
));
this.name = name;
this.methodInvocation=methodInvocation;
}
@Override
protected String getFallback() {
return "执行异常";
}
@Override
protected String run() throws Exception {
// 依赖逻辑封装在run()方法中
/* ShopService shopService= (ShopService) ApplicationContextUtils.getBeanByName("shopService");
List<Integer> shopIds=new ArrayList<>();
shopIds.add(5740403);
List<ShopDTO> babySubCardInfoDTO=shopService.findShops(shopIds);*/
Object result=null;
try {
result=methodInvocation.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return JSON.toJSONString(result);
}
}
(2)Around切面点逻辑处理
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* Created by JackDing on 18/10/30.
*/
public class Around implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("准备好了吗?");
HelloWorldCommand command=new HelloWorldCommand("test",methodInvocation);
Object result= command.queue().get();
System.out.println("已经结束了!");
return result;
}
}
(3)Around切面点需要关注的方法拦截
<bean id="aroundFilter" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<bean class="com.dianping.baby.activity.aop.Around"></bean>
</property>
<property name="patterns">
<list>
<value>.*test.*</value>
</list>
</property>
</bean>
<bean id="test" class="com.dianping.baby.activity.reliability.TestImpl">
<property name="groupName" value="groupTest"></property>
<property name="commandName" value="commandTest"></property>
<property name="threadPoolName" value="threadPoolTest"></property>
<property name="threadPoolSize" value="10"></property>
<property name="breakerErrorThreshold" value="20"></property>
<property name="breakerSleepWindowInMilliseconds" value="3000"></property>
</bean>
(4)切面点配置
<aop:config>
<aop:pointcut expression="execution(* com.dianping.baby.activity.reliability.Test.*())" id="pointcut"/>
<!-- <aop:advisor advice-ref="before" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="after" pointcut-ref="pointcut"/>-->
<aop:advisor advice-ref="around" pointcut-ref="pointcut"/>
</aop:config>