Service oriented architecture  – with Thrift and Finagle



Service oriented architecture  – with Thrift and Finagle

2 2


soa-with-thrift-and-finagle

Service oriented architecture with Thrift and Finagle

On Github bancek / soa-with-thrift-and-finagle

Service oriented architecture 

with Thrift and Finagle

Luka Zakrajšek

@bancek

First Scala meetup in Ljubljana

May 23, 2013

Hi

I'm Luka. I work at Koofr,
Slovenian startup of the year

Distributed systems

  • redundancy
  • modularity
  • flexibility
  • needs to be simple                                             

It ends up like this

Components must talk

  • SOAP
  • XML-RPC
  • REST
  • Google Protocol Buffers
  • Apache Thrift                                                             

Example

Apache Thrift

  • framework, for scalable cross-languageservices development
  • code generation engine
  • C++, Java, Python, PHP, Ruby, Erlang,Perl, Haskell, C#, Cocoa, JavaScript,Node.js, Smalltalk, OCaml, Delphi, ...
  • Developed by Facebook,opensourced in April 2007

Apache Thrift

  • Type system
  • Transport layer
  • Protocol layer
  • Processors
  • Server                                                                            

Twitter Finagle

  • extensible asynchronous (reactive)RPC system  for the JVM
  • uniform client and server APIsfor several protocols
  • high performance and concurrency
  • written in Scala
  • provides both Scala and Java idiomatic APIs

TWITTER FINAGLE

Asynchronous client/server for multiple protocols:
  • HTTP
  • Memcached
  • Redis
  • Protobuf
  • Thrift
  • MySQL
  • mDNS                                                          
  • ...

ThriFT IDL

ping.thrift
namespace java com.example.ping
typedef string UUID
typedef i64 DateTime

struct Message {
    1: required UUID id;
    2: required string body;
    3: optional DateTime sent;
}

service Ping {

    Message ping(1:Message msg)

}

Scala server/client

SCALA server

import com.twitter.util.Future

import com.example.ping._

class PingImpl extends Ping.FutureIface {

  def ping(message: Message): Future[Message] = {
    val returnMessage = message.copy(message="pong")

    Future.value(returnMessage)
  }
  
}

SCALA server

import java.net.InetSocketAddress
import org.apache.thrift.protocol.TBinaryProtocol
import com.twitter.finagle.thrift.ThriftServerFramedCodec
import com.twitter.finagle.builder.ServerBuilder

val port = 1234

val processor = new PingImpl()

val service = new Ping.FinagledService(processor,
  new TBinaryProtocol.Factory())

ServerBuilder()
  .bindTo(new InetSocketAddress(port))
  .codec(ThriftServerFramedCodec())
  .name("ping")
  .build(service)

SCALA CLIENT

import java.net.InetSocketAddress
import org.apache.thrift.protocol.TBinaryProtocol
import com.twitter.finagle.Service
import com.twitter.finagle.CodecFactory
import com.twitter.finagle.thrift.{ThriftClientFramedCodec,
ThriftClientRequest}
import com.twitter.finagle.builder.ClientBuilder

import com.example.ping._

val serviceCodec = ThriftClientFramedCodec()

val service: Service[ThriftClientRequest, Array[Byte]] =
  ClientBuilder()
    .hosts(new InetSocketAddress(host, port))
    .codec(serviceCodec)
    .build()

SCALA CLIENT

val message = Message(
  id = "12341234-1234-1234-1234-123412341234",
  body = "ping",
  sent = 1369315198125
)

val pongFuture = client.ping(message)

pongFuture.onSuccess { pong =>
  println(pong.message)
}

Python client

Thrift code generator:
thrift --gen py:new_style,utf8strings ping.thrift
Thrift library dependency:
pip install thrift

Python client

from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol

from ping import Ping
from ping.ttypes import Message

transport = TSocket.TSocket('localhost', 1234)
transport = TTransport.TFramedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
transport.open()

Python client

client = Ping.Client(protocol)

message = Message(
    id='12341234-1234-1234-1234-123412341234',
    body='ping',
    sent=1369315198125
)

pong = client.ping(message)

print pong.message

Questions?