使用Java的Akka中的REST客户端 [英] REST Client in Akka with Java

查看:87
本文介绍了使用Java的Akka中的REST客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何无缝机制来执行来自Akka的restful请求(仅使用带有java的akka​​)。有一个喷雾客户端 http://spray.io/documentation/1.2.3/spray -client / 但它接缝没有api for java或ssl支持。

还有Akka-Http包,但仍然是实验性的,我找不到客户端api的描述。

Is there any seamless mechanism for executing restful requests from Akka (using akka with java only). There is a spray client http://spray.io/documentation/1.2.3/spray-client/ but it seams there is no api for java nor ssl support.
Also there is Akka-Http package but is still experimental and I could not find a description of the client api.

到目前为止,我有一种方法可以使用泽西客户端( https:/ /jersey.java.net )并在Akka路由器后面隐藏同步调用,其中有一些演员来完成这项工作。

So far I have one approach to use jersey client (https://jersey.java.net) and hide synchronous calls behind an Akka router with some pool of actors doing this job.

还有其他任何建议吗?

Any other suggestions?

推荐答案

Akka HTTP附带一个客户端API,并且作为Typesafe支持的所有内容,我们还为它提供了一个Java API。 客户端API可在线获取。

Akka HTTP ships with a client API, and as everything backed by Typesafe, we also provided a Java API for it. Documentation for the client side API is available online.

有许多不同的API可供选择,请阅读上述文档,但最简单的是 singleRequest

There's a number of different APIs to chose from, read the above documentation, but the simplest one is singleRequest:

// valid for Akka Http 1.0 (experimental), APIs may change slightly still
final ActorSystem system = ActorSystem.create();
final ActorMaterializer materializer = ActorMaterializer.create(system);

final Future<HttpResponse> responseFuture =
  Http.get(system)
    .singleRequest(HttpRequest.create("http://akka.io"), materializer);

Akka HTTP支持TLS / SSL。它与 Play的WS 的不同之处在于它更低级,并且为您提供更多地控制如何/何时/什么。 WS更易于使用,也是异步的。 Akka HTTP能够流式传输响应正文,而WS和其他一些HTTP客户端无法执行此操作。使用 Akka Streams实现流式传输,通过获取响应正文流: response.entity()。getResponseBytes()这是 Source< ByteString,?>

Akka HTTP does support TLS/SSL. One thing it differs from Play's WS is that it's more low level, and gives you more control over how/when/what. WS is simpler to use and also asynchronous. Akka HTTP is able to stream the response body, whereas WS and some other HTTP clients are not able to do this. The streaming is achieved using Akka Streams, by getting the response body stream: response.entity().getResponseBytes() which is a Source<ByteString, ?>.

Play的WS或Akka HTTP的客户端应该做你需要的一切,挑选更适合你的用例。如果你需要流式传输 - Akka,如果你需要非常简单的东西 - 玩。

Either Play's WS or Akka HTTP's client-side should do everything you need, pick which fits your use-case more. If you need streaming - Akka, if you need very simple things - Play.

跟进,如果你正在制作来自Actor的Http请求并希望响应作为消息返回。您可以使用pipeTo模式将 Future 传递回 Actor ,如下所示:

Follow up, if you're making Http Requests from an Actor and want responses to come back as messages. You can use the pipeTo pattern to pipe the Future back to the Actor like so:

import static akka.pattern.Patterns.pipe;
// ... 

Future<HttpResponse> response = Http.get(system)
  .singleRequest(HttpRequest.create("http://akka.io"), materializer);
pipe(response, context.dispatcher()).to(self);

这篇关于使用Java的Akka中的REST客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆