Hystrix+AOP的完美结合

本文深入探讨了面向切面编程(AOP)的概念及其五种通知类型,重点介绍了如何利用AOP的环绕通知功能结合Hystrix实现服务监控与故障隔离。通过具体代码示例,展示了如何创建Hystrix命令并将其与Spring AOP的切面逻辑相结合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 

一、AOP简介

1.1、定义

1.2、AOP五种通知类型

二、Hystrix和AOP的结合

(1)Hystrix监控

(2)Around切面点逻辑处理

(3)Around切面点需要关注的方法拦截

(4)切面点配置


一、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>

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值