際際滷

際際滷Share a Scribd company logo
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-language
services 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 APIs
for 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
Finagle for server/client
Scrooge for code generation
sbt-scrooge for SBT plugin
https://github.com/bancek/sbt-scrooge
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 library dependency:
thrift --gen py:new_style,utf8strings ping.thrift
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?

More Related Content

SOA with Thrift and Finagle

  • 1. SERVICE ORIENTED ARCHITECTURE WITH THRIFT AND FINAGLE Luka Zakraj邸ek @bancek First Scala meetup in Ljubljana May 23, 2013
  • 2. HI I'm Luka. I work at Koofr, Slovenian startup of the year
  • 4. IT ENDS UP LIKE THIS
  • 5. COMPONENTS MUST TALK SOAP XML-RPC REST Google Protocol Buffers Apache Thrift
  • 7. APACHE THRIFT framework, for scalable cross-language services 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
  • 8. APACHE THRIFT Type system Transport layer Protocol layer Processors Server
  • 9. TWITTER FINAGLE extensible asynchronous (reactive) RPC system for the JVM uniform client and server APIs for several protocols high performance and concurrency written in Scala provides both Scala and Java idiomatic APIs
  • 10. TWITTER FINAGLE Asynchronous client/server for multiple protocols: HTTP Memcached Redis Protobuf Thrift MySQL mDNS ...
  • 11. 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) }
  • 12. SCALA SERVER/CLIENT Finagle for server/client Scrooge for code generation sbt-scrooge for SBT plugin https://github.com/bancek/sbt-scrooge
  • 13. 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) } }
  • 14. 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)
  • 15. 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()
  • 16. 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) }
  • 17. PYTHON CLIENT Thrift code generator: Thrift library dependency: thrift --gen py:new_style,utf8strings ping.thrift pip install thrift
  • 18. 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()
  • 19. 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