gpt4 book ai didi

java - Spring AOP - 正确配置重试建议

转载 作者:行者123 更新时间:2023-12-01 21:37:28 25 4
gpt4 key购买 nike

我是 Spring AOP 的新手,并且已经进行了一些尝试。

我正在尝试通过 Spring AOP 为我的一个项目设置重试和速率限制器。用例是这样的:-

  1. 检查 TPS 是否可用。如果没有,则抛出 ThrottledException
  2. 如果抛出 ThrottledException,则重试

我遇到的问题是:这个限制和重试组合正在进入无限循环(如果 TPS = 0)。也就是说,重试在“x”次尝试后不会停止。

我的节流拦截器(在高层次上)是这样的:

@Before("<pointcut>")
public void invoke() throws ThrottlingException {
if (throttler.isThrottled(throttleKey)) {
throw new ThrottlingException("Call Throttled");
}
}

我的重试拦截器是这样的:

@AfterThrowing(pointcut="execution(* com.company.xyz.method())", throwing="exception")
public Object invoke(JoinPoint jp, ThrottlingException exception) throws Throwable {
return RetryingCallable.newRetryingCallable(new Callable<Object>() {

@Override
public Object call() throws Exception {
MethodSignature signature = (MethodSignature) p.getSignature();
Method method = signature.getMethod();
return method.invoke(jp.getThis(), (Object[]) null);
}

}, retryPolicy).call();
}

这里的RetryingCallable是一个简单的实现(由我公司的某人编写的内部库),它接受RetryAdvice并应用它。

我的相关spring-config如下:

<bean id="retryInterceptor" class="com.company.xyz.RetryInterceptor">
<constructor-arg index="0"><ref bean="retryPolicy"/></constructor-arg>
</bean>


<bean id="throttlingInterceptor" class="com.company.xyz.ThrottlingInterceptor">
<constructor-arg><value>throttleKey</value></constructor-arg>
</bean>
<context:component-scan base-package="com.company.xyz">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<aop:aspectj-autoproxy/>

据我所知,这里的问题是,在每个 ThrotdlingException 上,都会应用新的重试建议,而不是之前的生效。

有关如何解决此问题的任何意见?

最佳答案

免责声明:不是 Spring 用户,因此我将在这里展示一个纯粹的 AspectJ 解决方案。不过,它在 Spring AOP 中应该以相同的方式工作。您唯一需要更改的是将方面优先级配置从 @DeclarePresedence 切换到 @Order,如 Spring AOP manual 中所述。 .

驱动程序应用程序:

package de.scrum_master.app;

public class Application {
public static void main(String[] args) {
new Application().doSomething();
}

public void doSomething() {
System.out.println("Doing something");
}
}

限制异常类:

package de.scrum_master.app;

public class ThrottlingException extends RuntimeException {
private static final long serialVersionUID = 1L;

public ThrottlingException(String arg0) {
super(arg0);
}
}

节流拦截器:

为了模拟限制情况,我创建了一个辅助方法 isThrottled(),它在 3 种情况下有 2 种随机返回 true

package de.scrum_master.aspect;

import java.util.Random;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import de.scrum_master.app.ThrottlingException;

@Aspect
public class ThrottlingInterceptor {
private static final Random RANDOM = new Random();

@Before("execution(* doSomething())")
public void invoke(JoinPoint thisJoinPoint) throws ThrottlingException {
System.out.println(getClass().getSimpleName() + " -> " + thisJoinPoint);
if (isThrottled()) {
throw new ThrottlingException("call throttled");
}
}

private boolean isThrottled() {
return RANDOM.nextInt(3) > 0;
}
}

重试拦截器:

请注意,AspectJ 注释 @DeclarePrecedence("RetryInterceptor, *") 表示此拦截器将在任何其他拦截器之前执行。请在两个拦截器类上将其替换为 @Order 注释。否则,@Around 建议无法捕获节流拦截器抛出的异常。

还值得一提的是,该拦截器不需要任何反射来实现重试逻辑,它直接使用重试循环中的连接点来重试 thisJoinPoint.proceed()。这可以很容易地分解为实现不同类型重试行为的辅助方法或辅助类。只需确保使用 ProceedingJoinPoint 作为参数,而不是 Callable

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclarePrecedence;

import de.scrum_master.app.ThrottlingException;

@Aspect
@DeclarePrecedence("RetryInterceptor, *")
public class RetryInterceptor {
private static int MAX_TRIES = 5;
private static int WAIT_MILLIS_BETWEEN_TRIES = 1000;

@Around("execution(* doSomething())")
public Object invoke(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println(getClass().getSimpleName() + " -> " + thisJoinPoint);
ThrottlingException throttlingException = null;
for (int i = 1; i <= MAX_TRIES; i++) {
try {
return thisJoinPoint.proceed();
}
catch (ThrottlingException e) {
throttlingException = e;
System.out.println(" Throttled during try #" + i);
Thread.sleep(WAIT_MILLIS_BETWEEN_TRIES);
}
}
throw throttlingException;
}
}

成功重试的控制台日志:

RetryInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #1
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #2
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Doing something

重试失败的控制台日志:

RetryInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #1
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #2
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #3
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #4
ThrottlingInterceptor -> execution(void de.scrum_master.app.Application.doSomething())
Throttled during try #5
Exception in thread "main" de.scrum_master.app.ThrottlingException: call throttled
at de.scrum_master.aspect.ThrottlingInterceptor.invoke(ThrottlingInterceptor.aj:19)
at de.scrum_master.app.Application.doSomething_aroundBody0(Application.java:9)
at de.scrum_master.app.Application.doSomething_aroundBody1$advice(Application.java:22)
at de.scrum_master.app.Application.doSomething(Application.java:1)
at de.scrum_master.app.Application.main(Application.java:5)

如有任何与我的回答相关的后续问题,请随时提出。

<小时/>

更新:我不知道你的RetryingCallableRetryPolicy类/接口(interface)是如何工作的,你没有告诉我太多。但我编造了一些东西并让它像这样工作:

package de.scrum_master.app;

import java.util.concurrent.Callable;

public interface RetryPolicy<V> {
V apply(Callable<V> callable) throws Exception;
}
package de.scrum_master.app;

import java.util.concurrent.Callable;

public class DefaultRetryPolicy<V> implements RetryPolicy<V> {
private static int MAX_TRIES = 5;
private static int WAIT_MILLIS_BETWEEN_TRIES = 1000;

@Override
public V apply(Callable<V> callable) throws Exception {
Exception throttlingException = null;
for (int i = 1; i <= MAX_TRIES; i++) {
try {
return callable.call();
}
catch (ThrottlingException e) {
throttlingException = e;
System.out.println(" Throttled during try #" + i);
Thread.sleep(WAIT_MILLIS_BETWEEN_TRIES);
}
}
throw throttlingException;
}
}
package de.scrum_master.app;

import java.util.concurrent.Callable;

public class RetryingCallable<V> {
private RetryPolicy<V> retryPolicy;
private Callable<V> callable;

public RetryingCallable(Callable<V> callable, RetryPolicy<V> retryPolicy) {
this.callable = callable;
this.retryPolicy = retryPolicy;
}

public static <V> RetryingCallable<V> newRetryingCallable(Callable<V> callable, RetryPolicy<V> retryPolicy) {
return new RetryingCallable<V>(callable, retryPolicy);
}

public V call() throws Exception {
return retryPolicy.apply(callable);
}
}

现在像这样更改重试拦截器:

package de.scrum_master.aspect;

import java.util.concurrent.Callable;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclarePrecedence;

import de.scrum_master.app.DefaultRetryPolicy;
import de.scrum_master.app.RetryPolicy;
import de.scrum_master.app.RetryingCallable;

@Aspect
@DeclarePrecedence("RetryInterceptor, *")
public class RetryInterceptor {
private RetryPolicy<Object> retryPolicy = new DefaultRetryPolicy<>();

@Around("execution(* doSomething())")
public Object invoke(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println(getClass().getSimpleName() + " -> " + thisJoinPoint);
return RetryingCallable.newRetryingCallable(
new Callable<Object>() {
@Override
public Object call() throws Exception {
return thisJoinPoint.proceed();
}
},
retryPolicy
).call();
}
}

日志输出将与您之前看到的非常相似。对我来说这很有效。

关于java - Spring AOP - 正确配置重试建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36811268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com