我正在尝试使一些微服务更具弹性,并且重试某些类型的HTTP请求将有助于此.
重试超时会给客户带来非常缓慢的体验,因此我不打算在这种情况下重试.重试400s无济于事,因为错误的请求在几毫秒后仍然是一个错误的请求.
我想还有其他原因不能重试一些其他类型的错误,但是哪些错误以及为什么?
我有这段代码
@Retryable(maxAttempts = 3, stateful = true, include = ServiceUnavailableException.class,
exclude = URISyntaxException.class, backoff = @Backoff(delay = 1000, multiplier = 2) )
public void testThatService(String serviceAccountId)
throws ServiceUnavailableException, URISyntaxException {
Run Code Online (Sandbox Code Playgroud)
//这里的一些实现}
有没有办法可以使用@Value配置maxAttempts,延迟和乘数?或者是否有任何其他方法可以使注释中的这些字段可配置?
在spring启动应用程序中,我在yaml文件中定义了一些配置属性,如下所示.
my.app.maxAttempts = 10
my.app.backOffDelay = 500L
Run Code Online (Sandbox Code Playgroud)
还有一个例子bean
@ConfigurationProperties(prefix = "my.app")
public class ConfigProperties {
private int maxAttempts;
private long backOffDelay;
public int getMaxAttempts() {
return maxAttempts;
}
public void setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
}
public void setBackOffDelay(long backOffDelay) {
this.backOffDelay = backOffDelay;
}
public long getBackOffDelay() {
return backOffDelay;
}
Run Code Online (Sandbox Code Playgroud)
我怎么能注入的价值观my.app.maxAttempts
和my.app.backOffdelay
对春重试注解?在下面的示例中,我想用配置属性的相应引用替换10
maxAttempts和500L
backoff值的值.
@Retryable(maxAttempts=10, include=TimeoutException.class, backoff=@Backoff(value = 500L))
Run Code Online (Sandbox Code Playgroud) 我想使用@Retryable
注释restTemplate
.我已经添加:
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
以及@EnableRetry
配置类.我标记了方法:
restTemplate.exchange(url, HttpMethod.POST, request, String.class);
Run Code Online (Sandbox Code Playgroud)
用(在一个新的主题中)
@Retryable(maxAttempts=4,value=Exception.class,backoff=@Backoff(delay = 2000))
Run Code Online (Sandbox Code Playgroud)
但我从catalina得到错误:
27-Oct-2017 18:11:41.023 SEVERE [http-nio-8080-exec-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at
<ommitted>
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is …
Run Code Online (Sandbox Code Playgroud) 以下代码未重试.我错过了什么?
@EnableRetry
@SpringBootApplication
public class App implements CommandLineRunner
{
.........
.........
@Retryable()
ResponseEntity<String> authenticate(RestTemplate restTemplate, HttpEntity<MultiValueMap<String, String>> entity) throws Exception
{
System.out.println("try!");
throw new Exception();
//return restTemplate.exchange(auth_endpoint, HttpMethod.POST, entity, String.class);
}
Run Code Online (Sandbox Code Playgroud)
我已将以下内容添加到pom.xml中.
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我也试过为@Retryable提供不同的参数组合.
@Retryable(maxAttempts=10,value=Exception.class,backoff=@Backoff(delay = 2000,multiplier=2))
Run Code Online (Sandbox Code Playgroud)
谢谢.
如何spring-retry
使用@Retryable()
注解注册监听器?
@Bean
public RetryListener myRetryListener() {
return new MyRetryListener();
}
@Service
public class SampleService {
private int cnt = 1;
@Retryable(RuntimeException.class)
public void retryMethod(int x) throws Exception {
System.out.println("SampleService invoked.");
Thread.sleep(500);
if (cnt++ < 4) {
throw new RuntimeException("SampleService throwing exception");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我创建任何侦听器 bean,它会自动注册到RetryTemplate
. 因此,如果我有多个侦听器,那么如何要求特定侦听器侦听我的可重试服务?
Spring Retry是否可以保证与Spring的@Transactional
注释一起使用?
具体来说,我正在尝试使用@Retryable
乐观锁定.似乎它将依赖于创建的AOP代理的顺序.例如,如果调用如下所示:
调用代码 - >重试代理 - >事务代理 - >实际数据库代码
然后它将正常工作,但如果代理结构如下:
调用代码 - >事务代理 - >重试代理 - >实际数据库代码
然后重试将不起作用,因为关闭事务的行为是抛出optmistic锁定异常的行为.
在测试中,它似乎生成了第一个案例(重试,然后交易),但我不知道这是保证行为还是幸运.
我有一个具有以下签名的方法:
public Optional<String> doSomething() {
...
}
Run Code Online (Sandbox Code Playgroud)
如果我得到一个空的,Optional
我想重试这个方法,只有在 3 次后才返回空的Optional
。
我查看并找到了Retryable
spring 注释,但它似乎只适用于异常。
如果可能的话,我想为此使用一个库,并避免:
是否可以根据错误状态代码在spring retry(https://github.com/spring-projects/spring-retry)中设置RetryPolicy ?例如我要重试上HttpServerErrorException
与HttpStatus.INTERNAL_SERVER_ERROR
状态码,其是503因此,应忽略所有其他错误代码- [500 - 502]和[504 - 511].
我用compile 'org.springframework.retry:spring-retry:1.2.2.RELEASE'
了Spring Boot 1.5.9.RELEASE
.
配置为重试我的方法,它运作良好:
@Retryable(value = { IOException.class }, maxAttempts = 5, backoff = @Backoff(delay = 500))
public void someMethod(){...}
Run Code Online (Sandbox Code Playgroud)
重试发生时如何输出某些特定消息?
spring-retry ×10
spring ×8
java ×7
spring-boot ×4
http ×1
httpresponse ×1
hystrix ×1
maven ×1
spring-data ×1