On Github bancek / soa-with-thrift-and-finagle
Luka Zakrajšek
@bancek
First Scala meetup in Ljubljana
May 23, 2013
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) }
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) } }
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)
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()
val message = Message( id = "12341234-1234-1234-1234-123412341234", body = "ping", sent = 1369315198125 ) val pongFuture = client.ping(message) pongFuture.onSuccess { pong => println(pong.message) }
thrift --gen py:new_style,utf8strings ping.thrift
pip install thrift
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()
client = Ping.Client(protocol) message = Message( id='12341234-1234-1234-1234-123412341234', body='ping', sent=1369315198125 ) pong = client.ping(message) print pong.message