实际上针对标准的传输连接及标准数据编码、传输、解码全过程的软件编程是非常复杂及困难的。Akka-http正是这么一套能高效解决以上问题的编程工具。...要求进行Server端运算;将运算结果数据封装成Response;然后将Response返回Client;Client对Response进行解析;将Response里的数据解码形成内部结构数据。 ...对某个人群来说,Http是一个极其繁琐的协议:这里包括了消息格式、数据加码、解码、压缩、通讯协议、传输安全等等,等等。...由于Akka-http是基于Akka-stream功能之上的,它支持Http数据的流操作,也就是说它可以把一个Stream-Source放在Http消息的数据里,然后Akka-http的Client-Side-Api...._ import akka.http.scaladsl.model._ import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives
], T]]]],这样的结果。...eitherOfList: Right(List(1, 2, 3)) 下面的代码演示了并发发出100个RestAPi请求获取数据的处理代码,结果保存在Tuple里,分别是所有失败和成功的数据。...minute) println(s"Exceptions in Response: $result._1") println(s"Data in Response: $result._2") 另,Akka...HTTP默认主机连接数是32个,所以需要修改applicaiton.confg配置以支持更多连接数。...akka.http.host-connection-pool.max-open-requests = 128
Akka-http是一项系统集成工具。这主要依赖系统之间的数据交换功能。...String akka.http.scaladsl.model.FormData akka.http.scaladsl.model.MessageEntity T akka.http.scaladsl.model.Multipart...Akka-http通过akka-http-spray-json模块直接支持由Spray-Json实现的Json读写工具库。...akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.marshallers.sprayjson...._ import akka.stream._ import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model
在2017年我曾经写了一系列博客介绍akka-http,这里就不再叙述它的细节了。这篇我们只聚焦在解决当前问题上。...首先,用akka-http搭建一个http server框架: import akka.actor._ import akka.stream._ import akka.http.scaladsl.Http...akka-http的数据转换机制Marshaller/Unmarshaller是通过类型转换的隐式实例来实现的,akka-http提供了多个标准类型数据转换的隐式实例,如StringMarshaller...这个也比较容易:akka-http本身支持json-streaming。..." %% "akka-http" % "10.1.8" , "com.typesafe.akka" %% "akka-http-spray-json" % "10.1.8", "com.typesafe.akka
对akka-http用户来说,akka-grpc具有很大吸引(相对其它gRPC开放工具),因为它是基于akka-http的,看看下面grpc服务端的接口: // Bind service handler...那么可以想象得到如果需要支持http+rpc混合模式的应用,akka-grpc将会发挥很大作用,这也是akka-http下一步的发展趋势。..., "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion, "com.typesafe.akka" %% "akka-http-spray-json...._ import akka.http.scaladsl._ import com.typesafe.config.ConfigFactory import akka.http.scaladsl.Http...import akka.http.scaladsl.util.FastFuture import akka.grpc.scaladsl._ import learn.akka.grpc._ import
在前面几篇讨论里我们都提到过:Akka-http是一项系统集成工具库。它是以数据交换的形式进行系统集成的。...Akka-http是基于Akka-stream开发的:不但它的工作流程可以用Akka-stream来表达,它还支持stream化的数据传输。...简单来说:Akka-http的消息数据内容HttpEntity可以支持理论上无限长度的data-stream。...Akka-http的stream类型数据内容是以Source[T,_]类型表示的。...._ import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives
Unmarshalling是Akka-http内把网上可传输格式的数据转变成程序高级结构话数据的过程,比如把Json数据转换成某个自定义类型的实例。...因为A到B的转换是non-blocking的,所以可以立即返回Future类型结果。...下面是一些Unmarshal的用例: import akka.actor._ import akka.stream._ import akka.http.scaladsl.unmarshalling.Unmarshal...import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ object Unmarshalling...import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives._ import scala.concurrent
akka-http提供了一套功能强大,使用又很方便的Routing DSL。...这个是通过 ~ 操作符号实现的 在Akka-http的routing DSL里这些Route组合操作是通过Directive实现的。...Akka-http提供了大量现成的Directive,我们也可以自定义一些特殊功能的Directive,详情可以查询官方文件或者api文件。...Akka-http提供了所有22个TupleXX[L]的隐形实例。...再注意implicit class singleValueModifiers[T]:它提供了多层Directive的自动展平,能够实现下面的自动转换结果: Directive1[T] = Directive
值得庆幸的是akka-http已经提供了对缓存的支持,是基于java8 caffein的一套缓存操作工具包的。下面就介绍一下akka-http的caching。...akka-http caching 有个依赖: "com.typesafe.akka" %% "akka-http-caching" % akkaHttpVersion, 先从缓存存储结构开始,看看下面的一段缓存结构定义...: import akka.http.scaladsl.util.FastFuture import akka.http.caching.scaladsl.Cache import akka.http.caching.scaladsl.CachingSettings...我们来看看如何实现缓存管理: 在akka-http里可以用两种方式来实现缓存管理:1、直接用cache工具,2、用akka-http提供的Directive: cache, alwaysCache 我们先看看如何直接使用...import akka.http.caching.scaladsl.CachingSettings import akka.http.caching.LfuCache import akka.http.scaladsl.server.RequestContext
Akka-http的数据交换模式支持流式操作:代表交换数据可以是一种无限长度流的元素。...更重要的是:Akka-http还支持reactive-stream,可以避免由传输速率所产生的种种问题。在本篇我们讨论利用Akka-http进行文件的双向传递。 ...._ import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives...._ import akka.http.scaladsl.model._ import akka.http.scaladsl.model.HttpEntity._ import java.nio.file...._ import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpEntity.limitableByteSource
", // Akka HTTP项目的标准依赖关系 "com.typesafe.akka" %% "akka-http-spray-json" % "10.0.11", // 用于JSON序列化和反序列化...也可以在消费者(Consumer)处理的结果值上添加更多的检查(声明)。...import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model....import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model....import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.server.directives.DebuggingDirectives
关注一下这个andThen,它可以连接一串多个monadic运算,在不影响上游运算结果的情况下实现一些副作用计算。...._ import akka.stream._ import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives._...._ import akka.http.scaladsl.settings.ConnectionPoolSettings import akka.stream._ import akka.stream.scaladsl...._ import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import scala.util._ import de.heikoseeberger.akkahttpjson4s.Json4sSupport...import akka.http.scaladsl.unmarshalling._ import akka.http.scaladsl.marshalling.Marshal import scala.collection.SortedMap
技术栈 这篇文章,我选择了Scala作为语言,Akka HTTP作为框架。...我们可以轻松地使用SBT创建一个新的Scala项目并定义build.sbt,如下所示: build.sbt 正如你所看到的,Akka HTTP项目的标准依赖关系(通用于提供者和消费者),spry-json...也可以在消费者(Consumer)处理的结果值上添加更多的检查(声明)。 当然,我们可以添加更多场景和交互。我们也可以为许多生产者定义更多的契约。...在此之前,为了检查我们的服务是否符合消费者契约,我们必须完成定义Akka HTTP应用程序的基本服务: MyLibraryAppServer.scala 这个类定义了两个方法,一个是启动我们的服务器所必需的...Broker带入您的CI / CD流程,它是一个提供以下功能的应用程序(来自官方文档): 通过独立部署您的服务并避免集成测试的瓶颈,您可以快速,放心地利用客户价值 解决了如何在消费者和提供者项目之间共享契约验证结果的问题
在上篇我们介绍了Akka-http Low-Level-Api。实际上这个Api提供了Server对进来的Http-requests进行处理及反应的自定义Flow或者转换函数的接入界面。...我们看看下面官方文档给出的例子: import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.HttpMethods...._ import akka.http.scaladsl.model._ import akka.stream.ActorMaterializer import scala.io.StdIn object...Akka-http提供了一套routing DSL作为High-Level-Api的主要组成部分。...Akka-http提供了所有22个TupleXX[L]的隐形实例。
Akka-http的客户端Api应该是以HttpRequest操作为主轴的网上消息交换模式编程工具。我们知道:Akka-http是搭建在Akka-stream之上的。...所以,Akka-http在客户端构建与服务器的连接通道也可以用Akka-stream的Flow来表示。...://kazuhiro.github.io/scala/akka/akka-http/akka-streams/2016/01/31/connection-pooling-with-akka-http-and-source-queue.html...讨论的示范源代码: import akka.actor._ import akka.http.javadsl....://kazuhiro.github.io/scala/akka/akka-http/akka-streams/2016/01/31/connection-pooling-with-akka-http-and-source-queue.html
Akka-http routing DSL在Route运算中抛出的异常是由内向外浮出的:当内层Route未能捕获异常时,外一层Route会接着尝试捕捉,依次向外扩展。...Akka-http提供了ExceptionHandler类来处理Route运算产生的异常: trait ExceptionHandler extends ExceptionHandler.PF {...但实际上Akka-http提供了默认的handler ExceptionHandler.default: /** * Creates a sealed ExceptionHandler from...} } } 下面是本次讨论中的示范源代码: import akka.actor._ import akka.http.scaladsl.Http import akka.http.scaladsl.model...._ import akka.http.scaladsl.server._ import akka.http.scaladsl.server.Directives._ import akka.stream
Route 是Akka-http routing DSL的核心部分,使用户能比较方便的从http-server的角度筛选http-request、进行server运算、构建回复的http-response...Akka-http是通过在运行Route时用Route.seal(route)的方式来确保所有rejection在最终都会得到处理: override def seal(system: ActorSystem...{ import directives.ExecutionDirectives._ // optimized as this is the root handler for all akka-http...._ import akka.http.scaladsl.Http import akka.http.scaladsl.model._ import akka.http.scaladsl.server...._ import akka.http.scaladsl.server.Directives._ import akka.stream._ import akka.stream.scaladsl._ import
我们可以通过Akka-http的raw-header来实现附加自定义消息的传递,这项功能可以通过Akka-http提供的raw-header筛选功能来实现。...import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import scala.util._...import akka.http.scaladsl.common.EntityStreamingSupport import akka.http.scaladsl.model._ import spray.json...._ import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import akka._ import...import scala.concurrent._ import akka.http.scaladsl.server._ import akka.http.scaladsl.server.Directives
STTP 提供开箱即用的对第三方JOSN库的支持:包括Circe、Json4s、spray-json 等。 目前,从项目活跃度、维护程度上讲Circe应该是不错的选择。...(https://scala.libhunt.com/compare-circe-vs-spray-json) 2)Circe Circe竟然是基于挑战智商的 CAT 实现的!...官网: https://circe.github.io/circe/。 Circe使用 Encoder、Decoder 编解码Json。...Circe对Scala库中常见类型就提供了默认的隐式实例。 对于简单结构的 case class,使用 Circe 提供的自动、半自动的编解码就好。...import io.circe.generic.auto._ import sttp.client._ import sttp.client.circe._ case class Links
返回结果的 HTTP 状态码.png 返回结果的 HTTP 状态码 状态码的职责 当客户端向服务器端发送请求时,描述返回的请求结果 状态码的大致分类 1XX 信息性状态码 · 接收的请求正在处理 2XX...302 标准禁止 POST 变换成 GET 4XX 客户端错误 400 Bad Request 该状态码表示请求报文中存在语法错误 401 Unauthorized 该状态码表示发送的请求需要有通过 HTTP
领取专属 10元无门槛券
手把手带您无忧上云