我一直在尝试使用 Resilience4j + OpenFeign 来调用服务。当我收到错误时,超时工作正常,但是,如果成功,它总是返回null
,我找不到原因。有人可以帮我吗?
创建构建器:
public AgentesAPI getAPI(String name, String url) {
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults(name);
RateLimiter rateLimiter = RateLimiter.ofDefaults(name);
FeignDecorators decorators =
FeignDecorators.builder()
.withCircuitBreaker(circuitBreaker)
.withRateLimiter(rateLimiter)
.withFallback(new AgentesAPIFallback())
.build();
return Resilience4jFeign.builder(decorators)
.logger(new Slf4jLogger())
.logLevel(Level.BASIC)
.target(AgentesAPI.class, url);
}
使用 RateLimiter 获得 CompletableFuture:
public class AgentesAPITimeLimiterService {
public CompletableFuture getCompletableFuture(String name, long miliseconds, Supplier supplier) {
TimeLimiter timeLimiter = getTimeLimiter(name, miliseconds);
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
return timeLimiter.executeCompletionStage(
scheduler, () -> CompletableFuture.supplyAsync(supplier))
.toCompletableFuture();
}
private TimeLimiter getTimeLimiter(String name, long miliseconds) {
return TimeLimiter.of(name,
TimeLimiterConfig.custom().timeoutDuration(
Duration.ofMillis(miliseconds)).build());
}
}
调用服务并始终获得null
结果:
public Agencias getAgencias(Map<String, Integer> queryMap) {
try {
CompletableFuture<Agencias> result =
_agentesAPITimeLimiterService.getCompletableFuture(configuration.backendName(),
configuration.timeout(), (() -> agentesAPI.getAgencias(queryMap)));
return result.get();
} catch (Exception e) {
throw new RuntimeException("Error getting Agencias!");
}
}
public interface AgentesAPI {
@RequestLine("POST /obtiene_agencias")
@Headers("Content-Type: application/x-www-form-urlencoded")
Agencias getAgencias(@QueryMap Map<String, Integer> queryMap);
}
API 正在返回一些东西,但是,我无法以这种方式工作。有什么我在这里想念的吗?
我正在使用以下版本:
compileInclude group: "io.github.openfeign", name: "feign-core", version: "11.0"
compileInclude group: "io.github.openfeign", name: "feign-gson", version: "11.0"
compileInclude group: "io.github.openfeign", name: "feign-slf4j", version: "11.0"
compileInclude group: "io.github.resilience4j", name: "resilience4j-all", version: "1.5.0"
compileInclude group: "io.github.resilience4j", name: "resilience4j-feign", version: "1.5.0"
compileInclude group: "io.github.resilience4j", name: "resilience4j-timelimiter", version: "1.5.0"
compileInclude group: "org.slf4j", name: "slf4j-api", version: "1.7.30"
compileInclude group: "org.slf4j", name: "slf4j-simple", version: "1.7.30"
提前致谢。