soa with thrift and finagle

20
SERVICE ORIENTED ARCHITECTURE WITH THRIFT AND FINAGLE Luka Zakrajšek @bancek First Scala meetup in Ljubljana May 23, 2013

Upload: luka-zakrajsek

Post on 15-Jan-2015

7.697 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: SOA with Thrift and Finagle

SERVICE ORIENTED ARCHITECTURE WITH THRIFT AND FINAGLE

Luka Zakrajšek

@bancek

First Scala meetup in Ljubljana

May 23, 2013

Page 2: SOA with Thrift and Finagle

HII'm Luka. I work at Koofr,

Slovenian startup of the year

Page 3: SOA with Thrift and Finagle

DISTRIBUTED SYSTEMSredundancymodularityflexibilityneeds to be simple

Page 4: SOA with Thrift and Finagle

IT ENDS UP LIKE THIS

Page 5: SOA with Thrift and Finagle

COMPONENTS MUST TALKSOAPXML-RPCRESTGoogle Protocol BuffersApache Thrift

Page 6: SOA with Thrift and Finagle

EXAMPLE

Page 7: SOA with Thrift and Finagle

APACHE THRIFTframework, 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

Page 8: SOA with Thrift and Finagle

APACHE THRIFTType systemTransport layerProtocol layerProcessorsServer

Page 9: SOA with Thrift and Finagle

TWITTER FINAGLEextensible asynchronous (reactive)RPC system for the JVMuniform client and server APIsfor several protocolshigh performance and concurrency

written in Scalaprovides both Scala and Java idiomatic APIs

Page 10: SOA with Thrift and Finagle

TWITTER FINAGLEAsynchronous client/server for multiple protocols:

HTTPMemcachedRedisProtobufThriftMySQLmDNS ...

Page 11: SOA with Thrift and Finagle

THRIFT IDLping.thrift

namespace java com.example.pingtypedef string UUIDtypedef i64 DateTime

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

service Ping {

Message ping(1:Message msg)

}

Page 12: SOA with Thrift and Finagle

SCALA SERVER/CLIENTFinagle for server/client

Scrooge for code generation

sbt-scrooge for SBT plugin https://github.com/bancek/sbt-scrooge

Page 13: SOA with Thrift and Finagle

SCALA SERVERimport 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) } }

Page 14: SOA with Thrift and Finagle

SCALA SERVERimport java.net.InetSocketAddressimport org.apache.thrift.protocol.TBinaryProtocolimport com.twitter.finagle.thrift.ThriftServerFramedCodecimport 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)

Page 15: SOA with Thrift and Finagle

SCALA CLIENTimport java.net.InetSocketAddressimport org.apache.thrift.protocol.TBinaryProtocolimport com.twitter.finagle.Serviceimport com.twitter.finagle.CodecFactoryimport 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()

Page 16: SOA with Thrift and Finagle

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

val pongFuture = client.ping(message)

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

Page 17: SOA with Thrift and Finagle

PYTHON CLIENTThrift code generator:

Thrift library dependency:

thrift --gen py:new_style,utf8strings ping.thrift

pip install thrift

Page 18: SOA with Thrift and Finagle

PYTHON CLIENTfrom thrift.transport import TSocket, TTransportfrom thrift.protocol import TBinaryProtocol

from ping import Pingfrom ping.ttypes import Message

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

Page 19: SOA with Thrift and Finagle

PYTHON CLIENTclient = Ping.Client(protocol)

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

pong = client.ping(message)

print pong.message

Page 20: SOA with Thrift and Finagle

QUESTIONS?