OkHttpClient 排队的 Feign 等价物
Feign Equivalent for OkHttpClient Enqueue
使用 OkHttpClient,我可以执行以下操作,这将允许我发送请求并尽可能处理响应。
okhttpClient
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onResponse(Call arg0, Response arg1) throws IOException {
log.info("Call success", arg1);
}
@Override
public void onFailure(Call arg0, IOException arg1) {
log.info("Call fail", arg1);
}
});
有没有办法用 Feign 做到这一点?我已经为 Feign 启用了 okhttpclient,是否有帮助?
见https://github.com/OpenFeign/feign#async-execution-via-completablefuture
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
CompletableFuture<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
public class MyApp {
public static void main(String... args) {
GitHub github = AsyncFeign.asyncBuilder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
因为他们的 OkHttp 客户端不是异步的,所以您需要使用
AsyncClient.AsyncClient(client, executorService) 或更优化地实现 OkHttpAsyncClient。
或接收https://github.com/OpenFeign/feign/tree/master/reactive
public interface GitHubReactiveX {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
Flowable<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
class Contributor {
String login;
public Contributor(String login) {
this.login = login;
}
}
}
public class ExampleRxJava2 {
public static void main(String args[]) {
GitHubReactiveX gitHub = RxJavaFeign.builder()
.target(GitHub.class, "https://api.github.com");
List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign")
.collect(Collectors.toList())
.block();
}
}
使用 OkHttpClient,我可以执行以下操作,这将允许我发送请求并尽可能处理响应。
okhttpClient
.newCall(request)
.enqueue(
new Callback() {
@Override
public void onResponse(Call arg0, Response arg1) throws IOException {
log.info("Call success", arg1);
}
@Override
public void onFailure(Call arg0, IOException arg1) {
log.info("Call fail", arg1);
}
});
有没有办法用 Feign 做到这一点?我已经为 Feign 启用了 okhttpclient,是否有帮助?
见https://github.com/OpenFeign/feign#async-execution-via-completablefuture
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
CompletableFuture<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
public class MyApp {
public static void main(String... args) {
GitHub github = AsyncFeign.asyncBuilder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
因为他们的 OkHttp 客户端不是异步的,所以您需要使用
AsyncClient.AsyncClient(client, executorService) 或更优化地实现 OkHttpAsyncClient。
或接收https://github.com/OpenFeign/feign/tree/master/reactive
public interface GitHubReactiveX {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
Flowable<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
class Contributor {
String login;
public Contributor(String login) {
this.login = login;
}
}
}
public class ExampleRxJava2 {
public static void main(String args[]) {
GitHubReactiveX gitHub = RxJavaFeign.builder()
.target(GitHub.class, "https://api.github.com");
List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign")
.collect(Collectors.toList())
.block();
}
}