gpt4 book ai didi

java - SpringBoot OAuth2RestTemplate 重试请求

转载 作者:行者123 更新时间:2023-12-02 05:52:34 28 4
gpt4 key购买 nike

在从 Spring 调用具有 Oauth2 的 API 时,我们需要添加重试。

我们还没有弄清楚如何以简单的方式做到这一点。我们甚至尝试过使用拦截器,但没有成功。

这是我们的代码:

应用程序.java

@EnableRetry
@SpringBootApplication
public class Application {

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

@Bean
public OAuth2RestTemplate oAuth2RestTemplate(ClientHttpRequestInterceptor interceptor) {
List<String> scopes = new ArrayList<>();
scopes.add("some-scope");

ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setAccessTokenUri("tokenUrl");
resourceDetails.setClientId("client-id");
resourceDetails.setClientSecret("client-secret");
resourceDetails.setScope(scopes);

OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(interceptor);
oAuth2RestTemplate.setInterceptors(interceptors);
return oAuth2RestTemplate;
}

@Bean
public CommandLineRunner run(OAuth2RestTemplate oAuth2RestTemplate) throws Exception {
return args -> {
oAuth2RestTemplate.getForObject(
"some-url-that-returns-error.com", Quote.class);
};
}
}

MyInterceptor.java

@Component
public class MyInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
return interceptWithRetry(httpRequest, bytes, execution);
}

@Retryable
private ClientHttpResponse interceptWithRetry(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
System.out.println("====================================");
return execution.execute(httpRequest, bytes);
}
}

以及我们的 pom.xml 中的依赖项

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

当我们运行这个并且出现错误时,我们看不到调用被重试,无论我们传递给@Retryable什么参数,拦截器都只会被调用一次。

有人可以为此提供一些指导吗?

谢谢

最佳答案

我们仍然不知道为什么Retryable在拦截器中不起作用。我们最终将注释移至服务中的方法并完全摆脱了拦截器。

SomeService.java

@Service
public class SomeService {

private OAuth2RestTemplate restTemplate;

public SomeService(OAuth2RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Retryable
public Quote getSomething() {
System.out.println("============= THIS WILL THROW AN EXCEPTION =================");
return restTemplate.getForObject(
"some-url-that-returns-error.com", Quote.class);
}

}

这有效。但是,最好有一些东西可以避免在每个服务的每个方法上重复 @Retryable(params)

现在,我会将问题标记为已回答。

关于java - SpringBoot OAuth2RestTemplate 重试请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56037795/

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