Finagle is an extensible RPC system for the JVM, used to construct high-concurrency servers. Finagle implements uniform client and server APIs for several protocols, and is designed for high performance and concurrency. Most of Finagle’s code is protocol agnostic, simplifying the implementation of new protocols.

Today here I am going to implement the Finagle example using Scala where I am sending the request with some message & get future of response using Finagle.

First, let’s define a service. Here we define a service to receive an HTTP request.

def apply(request: Request) = {
 request.method match {

case Method.Post =>
 request.uri match {

case "/" =>
 log.error("in post")
 val str = request.getContentString()
 //any business logic
 val response = Response(Version.Http11, Status.Ok)
 response.contentString = "Hello..!! " + str
 Future.value(response)

case _ =>
 log.error("REQUEST NOT FOUND")
 Future.value(Response(Version.Http11, Status.NotFound))
 }

case Method.Get =>
 request.uri match {

case "/" =>
 val str = request.getContentString()
 //any business logic
 val response = Response(Version.Http11, Status.Ok)
 response.contentString = "Thank You " + str
 Future.value(response)

case _ =>
 log.error("REQUEST NOT FOUND")
 Future.value(Response(Version.Http11, Status.NotFound))
 }
 }
}

Here, we get the request. After that, we are simply matching what type of request it is either GET or POST. and request.uri tell us about the end-point of the request.

Then initiate and start our server.

import java.net.InetSocketAddress
import com.twitter.finagle.Http
import com.twitter.finagle.builder.{ Server, ServerBuilder }

class ComputeServerBuilder {
val response = new ComputeResponse
 val address = new InetSocketAddress(10000)

def start: Server = ServerBuilder()
 .stack(Http.server)
 .bindTo(address)
 .name("HttpServer")
 .build(response)
}

Last, let’s define a client to consume this server.

computeServerBuilder = new ComputeServerBuilder
 server = computeServerBuilder.start
 client = ClientBuilder()
 .stack(Http.client)
 .hosts(computeServerBuilder.address)
 .hostConnectionLimit(1)
.build()

Now, we can send any number of requests using this client to our server.

Here, I made some test cases to hit the server can check either we are getting response successfully or not like shown below:-

val postRequest = Request(Version.Http11, Method.Post, "/")
 postRequest.setContentString("Knoldus")
 val postFutureResponse = client(postRequest).asScala
 postFutureResponse.map(response => println(response.getContentString()))
postFutureResponse.map(response => assert(response.status === Status.Ok && response.contentString.contains("Knoldus")))

To run it by yourself you can also clone my sample example from my git repo.

When I start implementing the Finagle, the challenge that I face in using Finagle is in handling the future between two different API i.e. Scala & Twitter itself. To deal with this, I used some implicit conversion of futures between these two APIs. Code for the same is also available on repo.

To know about the core of Finagle you can also read this great blog: Finagle: Controlling the Future Of RPC systems which helps me a lot during my learnings.

References:

 


 

knoldus-advt-sticker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.