As we all know, Akka system is a distributed computing system based on actor mode, which is very suitable for building big data platform. As a result, there is an unavoidable need to integrate between standalone systems, with heterogeneous systems, and with mobile systems. Because of the heterogeneous and mobile systems involved, the system docking must be conducted on a set of open standards, including data format and transmission standards. In fact, for the standard transmission connection and standard data encoding, transmission, decoding the whole process of software programming is very complex and difficult. Akka-http is such a set of programming tools that can solve these problems efficiently. Akka-http is a set of programming tools that support TCP transmission standards and HTTP standard data.
The HTTP mode of communication is fixed one-way: The party will always be the initiator of the dialogue, and the other party will always be the responder. The method of operation is: the initiator constructs an HTTP message structure that is request, transmits it to the receiver through TCP, the receiver interprets the message and follows the initiator's request to write the message and constructs a reply message that is response and passes it back to the sender. In practice, these two parties form a server-to-client relationship: The customer direction service sends a request for service, and the service provides the corresponding operation according to the request and responds to the result with response.
The composition of the HTTP message consists of two parts: the description of the message itself, including the version of the HTTP protocol, character set, encryption, compression, data type, security mechanism, and so on, the other part is the content of the message, namely the data itself, the message Description section also has some descriptions for the data.
From a practical point of view, a service and service usage calculation mode is implemented on TCP via HTTP message exchange. The service provider server is in a passive call state, and the client client requests a service request to the service party by requesting that the Service party perform the relevant operations on the service side and return the results to the client by response. It can be seen that both sides of the server are involved in the construction, parsing, and transmission of HTTP messages, while the service provider adds the parsing logic and corresponding computing services to the request service.
Analysis from a higher application level: system integration is essentially a data exchange between two systems via an HTTP protocol. The entire integration process can be summarized as follows: The client encapsulates the data as request, then parses the request after receiving it via TCP, and decodes the data from the request into the internal structure data. ; The server-side operation is carried out according to request, the result data is encapsulated into response, then the response is returned client;client the response is parsed, and the data in response is decoded to form the internal structure data.
Akka-http provides server-side SERVER-SIDE-API and client Client-side-api to help programmers simplify programming. The two APIs include the build, parse, and Transfer helper functions for HTTP messages. Server-side-api also includes a set of DSLs to facilitate http-server function programming.
For a population, HTTP is an extremely cumbersome protocol: It includes message formats, data charging, decoding, compression, communication protocols, transport security, and so on. A new set of definitions and inflexible rules cannot be avoided without the effect of programming efficiency, if programmed simply by the HTTP protocol. Akka-http should have been designed for this crowd.
Akka-http models the components of an HTTP message: Using class to represent the data structure. A large number of helper functions (helpers) are then provided in the various companion objects to assist in the construction, matching, and other operations of the type. This can greatly simplify the operation of HTTP message programming. For example: request and response two types of messages are defined as follows:
/** * The immutable model HTTP request model. */final class HttpRequest (Val Method:httpmethod, Val Uri:uri, Val headers:immutable. Seq[httpheader], Val entity:requestentity, Val protocol:httpprotocol) .../** * The immutable HTTP response model. */final class HttpResponse (Val Status:statuscode, Val headers:immutable. Seq[httpheader], Val entity:responseentity, Val protocol:httpprotocol) object HttpRequest {.../* Manual case Class Things, to ease bin-compat */def apply (Method:httpmethod = Httpmethods.get, Uri:uri = uri./, headers:immutable. Seq[httpheader] = Nil, entity:requestentity = httpentity.empty, Protocol:httpprotocol = Httpprotocols. ' http/1.1 ') = new HttpRequest (method, Uri, headers, entity, Protocol) def unapply (any:httprequest) = new Opthttprequest (Any) ...} Object HttpResponse {/* Manual case Class things, to Easen Bin-compat */def apply (status: StatusCode = Statuscodes.ok, headers:immutable. Seq[httpheader] = Nil, entity:responseentity = httpentity.empty, Protocol:httpprotocol = Httpprotocols. ' http/1.1 ') = new HttpResponse (status, headers, entity, Protocol) def unapply (any:httpresponse): OPTHTTPR Esponse = new Opthttpresponse (Any)}
import httpmethods._//construct a simple GET Request to ' Homeuri ' val homeuri = Uri ("/abc") HttpRequest (GET, Uri = Homeuri)//construct simple GET request to "/index" (i Mplicit string to URI conversion) HttpRequest (GET, uri = "/index")//construct simple POST request containing entityval dat A = ByteString ("abc") HttpRequest (POST, uri = "/receive", entity = data) Import statuscodes._//simple OK response without D ATA created using the integer status Codehttpresponse ($)//404 response created using the named StatusCode CONSTANTHTTPR Esponse (NotFound)//404 response with a body explaining the Errorhttpresponse (404, Entity = "Unfortunately, the resource C Ouldn ' t be found. ") A redirecting response containing an extra headerval Locationheader = headers. Location ("Http://example.com/other") HttpResponse (Found, headers = List (locationheader))
The Entity,header in the HTTP message also corresponds with the Httpentity,httpheader type.
The operation of the URI is also cumbersome, so Akka-http also provides the URI type:
/*http://tools.ietf.org/html/rfc3986. */sealedabstractcaseclass Uri (scheme:string, Authority:authority, Path:path, rawquerystring:option[string], fragment:option[string]) {...}
This makes it easy to simplify the building and parsing of URIs:
Uri ("Ftp://ftp.is.co.za/rfc/rfc1808.txt") shouldequal Uri. from(Scheme ="FTP", host ="ftp.is.co.za", Path ="/rfc/rfc1808.txt") Uri ("Http://www.ietf.org/rfc/rfc2396.txt") shouldequal Uri. from(Scheme ="http", host ="www.ietf.org", Path ="/rfc/rfc2396.txt") Uri ("Ldap://[2001:db8::7]/c=gb?objectclass?one") shouldequal Uri. from(Scheme ="LDAP", host ="[2001:db8::7]", Path ="/C=GB", queryString = Some ("Objectclass?one") ) Uri ("Mailto:[email protected]") shouldequal Uri. from(Scheme ="mailto", Path ="[email protected]") Uri ("News:comp.infosystems.www.servers.unix") shouldequal Uri. from(Scheme ="News", Path ="Comp.infosystems.www.servers.unix") Uri ("tel:+1-816-555-1212") shouldequal Uri. from(Scheme ="Tel", Path ="+1-816-555-1212") Uri ("telnet://192.0.2.16:80/") shouldequal Uri. from(Scheme ="telnet", host ="192.0.2.16", Port = the, Path ="/") Uri ("urn:oasis:names:specification:docbook:dtd:xml:4.1.2") shouldequal Uri. from(Scheme ="urn", Path ="oasis:names:specification:docbook:dtd:xml:4.1.2")
The Http server section is also a major part of system integration because, in the vast majority of cases, Http-server is on the data platform and is responsible for pooling system data and sharing platform data with other systems. This integration is typically accomplished by building rest data Services on the platform with Http-server. Since Akka-http is based on the Akka-stream function, it supports streaming of HTTP data, which means that it can put a stream-source in the data of an HTTP message. Then Akka-http's CLIENT-SIDE-API can compute these source. This can greatly facilitate data exchange between databases and improve the efficiency of data integration. However, the streaming function can only be implemented within AKKA-HTTP-API. However, it is easy to implement standard rest services with AKKA-HTTP-SERVER-SIDE-API so that other heterogeneous systems can be called smoothly. Let's do a Hello World rest service demonstration with Akka-http:
import akka.actor._import akka.stream._import akka.stream.scaladsl._import Akka.http.scaladsl.model._import Akka.http.scaladsl.Httpimport Akka.http.scaladsl.server.directives._import scala.concurrent._ObjectHellohttp extends App {ImplicitVal Httpsys = Actorsystem ("Httpsys") ImplicitVal Httpmat =Actormaterializer ()ImplicitVal Httpec =Httpsys.dispatcher val (host,port)= ("localhost",8088) Val services:flow[httprequest, HttpResponse, any]= Path ("Hello") { Get{Complete (httpentity (contenttypes. ' Text/html (utf-8)`,""))}} Val futbinding:future[http.serverbinding]=Http (). Bindandhandle (Services,host,port) println (s"Server running at $host $port. Press any key to exit ...") Scala.io.StdIn.readLine () Futbinding.flatmap (_.unbind ()). OnComplete (_=httpsys.terminate ())}
As you can see, it is easy to implement a rest service architecture with Akka-http.
Akka (): Http:about akka-http