Akka-http正是这么一套能高效解决以上问题的编程工具。Akka-http是一套支持Tcp传输标准及Http标准数据的编程工具。 ...由于Akka-http是基于Akka-stream功能之上的,它支持Http数据的流操作,也就是说它可以把一个Stream-Source放在Http消息的数据里,然后Akka-http的Client-Side-Api...不过Streaming功能只能在Akka-http-Api内实现。但用Akka-http-Server-Side-Api也可以很方便的实现标准Rest服务使其它异类系统可以顺利调用。...下面我们就用Akka-http做个Hello World Rest服务示范: import akka.actor._ import akka.stream._ import akka.stream.scaladsl...._ import akka.http.scaladsl.model._ import akka.http.scaladsl.Http import akka.http.scaladsl.server.Directives
AKKA提供的事件总线(Event Bus)可以看做是一种运用于特殊场景的消息总线,此时事件即为消息。...在AKKA中,Event Bus被定义为trait,定义了基本的订阅、取消订阅、发布等对应的方法,代码如下所示: trait EventBus { type Event type Classifier...Classifier): Boolean def unsubscribe(subscriber: Subscriber): Unit def publish(event: Event): Unit } 根据AKKA...Subscriber = ActorRef protected def compareSubscribers(a: ActorRef, b: ActorRef) = a compareTo b } AKKA...AKKA自身也提供了默认的处理器,可以配置在application.conf文件中: akka { event-handlers = ["akka.event.Logging$DefaultLogger
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
首先,用akka-http搭建一个http server框架: import akka.actor._ import akka.stream._ import akka.http.scaladsl.Http...这个过程包括把HttpResponse Entity里的数据从某种类型转换成通讯用的二进制数据流、到了客户端再转换成目标类型。...akka-http的数据转换机制Marshaller/Unmarshaller是通过类型转换的隐式实例来实现的,akka-http提供了多个标准类型数据转换的隐式实例,如StringMarshaller...还有一项需求是在Reponse里返回一个数据流(多条数据),如当前交易项目清单。这个也比较容易: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数据转换成某个自定义类型的实例。...下面是一些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...下面就是本次讨论的示范源代码: Unmarshalling import akka.actor._ import akka.stream._ import akka.http.scaladsl.unmarshalling.Unmarshal...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]的隐形实例。...shopid=1101 http://192.168.11.189:2588/pos/logsales?
值得庆幸的是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序列化和反序列化...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...import akka.http.scaladsl.Http import akka.http.scaladsl.server.directives.DebuggingDirectives import
Akka-http的客户端连接模式除Connection-Level和Host-Level之外还有一种非常便利的模式:Request-Level-Api。...._ 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
在上篇我们介绍了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]的隐形实例。
技术栈 这篇文章,我选择了Scala作为语言,Akka HTTP作为框架。...我们可以轻松地使用SBT创建一个新的Scala项目并定义build.sbt,如下所示: build.sbt 正如你所看到的,Akka HTTP项目的标准依赖关系(通用于提供者和消费者),spry-json...同时考虑到所有HTTP元素必须匹配(方法,url,标题,正文和查询) 用于验证消费者契约的实际测试的定义: 此代码将针对以前的方案运行,虚拟服务器将响应 交互部分中定义的唯一HTTP请求(如果响应为deined...进行单元测试: CategoriesRoutesSpec.scala 以及具有所有测试依赖性的基本测试类BaseTestAppServer: BaseTestAppServer.scala 该测试是使用Akka...在此之前,为了检查我们的服务是否符合消费者契约,我们必须完成定义Akka HTTP应用程序的基本服务: MyLibraryAppServer.scala 这个类定义了两个方法,一个是启动我们的服务器所必需的
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
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 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
akka-stream的数据流可以由一些组件组合而成。这些组件统称数据流图Graph,它描述了数据流向和处理环节。Source,Flow,Sink是最基础的Graph。...一个完整的(可运算的)数据流就是一个RunnableGraph。...代表合并处理后的开放型流图。...我们知道:akka-stream的Graph可以用更简单的Partial-Graph来组合,而所有Graph最终都是用基础流图Core-Graph如Source,Flow,Sink组合而成的。...._ import akka.stream._ import akka.stream.scaladsl._ import akka.stream.ActorAttributes._ import akka.stream.stage
我们可以通过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
经常有人问: Akka的Actor和Scala的Actor有什么不同?这里的回答是,从actor 模型角度讲,没什么不同,它们都实现了actor model....Akka actors and Scala actors are two implementations of that model....actor X to actor Y will arrive in the order thay were sent There is no difference between Scala and Akka...具体实现的不同请参考附件 我个人学习Akka的Actor的原因是Spark使用Akka作为消息驱动系统
领取专属 10元无门槛券
手把手带您无忧上云