服务未使用 CompletableFuture 执行
Service is not being executed with CompletableFuture
我一直在尝试使用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"
提前致谢。
已解决向构建器添加解码器的问题。
我一直在尝试使用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"
提前致谢。
已解决向构建器添加解码器的问题。