Quick Start with Finagle

Table of contents
Reading Time: 2 minutes

Finagle is an extensible RPC system for the JVM, used to construct high-concurrency servers. It 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 it 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

Written by 

Ayush is a Software Consultant, with experience of more than 1 year. He has specialisation in Hadoop and has good knowledge of many programming languages like C, Java and Scala. HQL, Pig Latin, HDFS, Flume and HBase adds to his forte. He is familiar with technology like Scala, Spark Kafka, Cassandra, Dynamo DB, Akka & many more. His hobbies include playing football and biking.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading