SOA with Thrift and Finagle

12,361 views

Published on

Published in: Technology, Education

SOA with Thrift and Finagle

  1. 1. SERVICE ORIENTED ARCHITECTUREWITH THRIFT AND FINAGLELuka Zakrajšek@bancekFirst Scala meetup in LjubljanaMay 23, 2013
  2. 2. HIIm Luka. I work at Koofr,Slovenian startup of the year
  3. 3. DISTRIBUTED SYSTEMSredundancymodularityflexibilityneeds to be simple
  4. 4. IT ENDS UP LIKE THIS
  5. 5. COMPONENTS MUST TALKSOAPXML-RPCRESTGoogle Protocol BuffersApache Thrift
  6. 6. EXAMPLE
  7. 7. APACHE THRIFTframework, for scalable cross-languageservices developmentcode generation engineC++, Java, Python, PHP, Ruby, Erlang,Perl, Haskell, C#, Cocoa, JavaScript,Node.js, Smalltalk, OCaml, Delphi, ...Developed by Facebook,opensourced in April 2007
  8. 8. APACHE THRIFTType systemTransport layerProtocol layerProcessorsServer
  9. 9. TWITTER FINAGLEextensible asynchronous (reactive)RPC system for the JVMuniform client and server APIsfor several protocolshigh performance and concurrencywritten in Scalaprovides both Scala and Java idiomatic APIs
  10. 10. TWITTER FINAGLEAsynchronous client/server for multiple protocols:HTTPMemcachedRedisProtobufThriftMySQLmDNS...
  11. 11. THRIFT IDLping.thriftnamespace java com.example.pingtypedef string UUIDtypedef i64 DateTimestruct Message {1: required UUID id;2: required string body;3: optional DateTime sent;}service Ping {Message ping(1:Message msg)}
  12. 12. SCALA SERVER/CLIENTFinagle for server/clientScrooge for code generationsbt-scrooge for SBT pluginhttps://github.com/bancek/sbt-scrooge
  13. 13. SCALA SERVERimport com.twitter.util.Futureimport com.example.ping._class PingImpl extends Ping.FutureIface {def ping(message: Message): Future[Message] = {val returnMessage = message.copy(message="pong")Future.value(returnMessage)}}
  14. 14. SCALA SERVERimport java.net.InetSocketAddressimport org.apache.thrift.protocol.TBinaryProtocolimport com.twitter.finagle.thrift.ThriftServerFramedCodecimport com.twitter.finagle.builder.ServerBuilderval port = 1234val processor = new PingImpl()val service = new Ping.FinagledService(processor,new TBinaryProtocol.Factory())ServerBuilder().bindTo(new InetSocketAddress(port)).codec(ThriftServerFramedCodec()).name("ping").build(service)
  15. 15. 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.ClientBuilderimport com.example.ping._val serviceCodec = ThriftClientFramedCodec()val service: Service[ThriftClientRequest, Array[Byte]] =ClientBuilder().hosts(new InetSocketAddress(host, port)).codec(serviceCodec).build()
  16. 16. 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)}
  17. 17. PYTHON CLIENTThrift code generator:Thrift library dependency:thrift --gen py:new_style,utf8strings ping.thriftpip install thrift
  18. 18. PYTHON CLIENTfrom thrift.transport import TSocket, TTransportfrom thrift.protocol import TBinaryProtocolfrom ping import Pingfrom ping.ttypes import Messagetransport = TSocket.TSocket(localhost, 1234)transport = TTransport.TFramedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)transport.open()
  19. 19. PYTHON CLIENTclient = Ping.Client(protocol)message = Message(id=12341234-1234-1234-1234-123412341234,body=ping,sent=1369315198125)pong = client.ping(message)print pong.message
  20. 20. QUESTIONS?

×