4.distributed objects -_thrift_dan_protocol_buffer
TRANSCRIPT
Pengembangan AplikasiTerdistribusiPandapotan Napitupulu
Apache ThriftBerdasarkan Slide Achmad Imam Kistijantoro
Apache Thrift RPC-based software library Fokus pada scalability, efficient & reliable communication,
multi language Dikembangkan di Facebook, Saat ini dikelola oleh Apache Foundation
• Karena solusi yang ada tidak memenuhi keinginan merekaakan kinerja tinggi, fleksibilitas dan simplisitas• Banyak service tapi komunikasi antar service
bermasalah
Apache Thrift Support: ActionScript 3,C++ ,C# , D, Delphi, Erlang,
Haskell, Java, JavaScript, Node.js, Objective-C/Cocoa, Ocaml, Perl, PHP, Python, Ruby, Smalltalk
Menangani serialisasi, service definition,server threading
Arsitektur TProtocol:menangani
bagaimana format data dikirim antar client dan server, e.g. text,biner, compress
TTransport:menangani bagaimana mekanisme data dikirimkan, e.g. socket, memory,file
Protocol Layer BinaryProtocol
A straight-forward binary format encoding numeric values as binary, rather than converting to text.
TCompactProtocol Very efficient, dense encoding ofdata
TDenseProtocol Similar toTCompactProtocol but strips off the meta information from
what is transmitted, and adds it back in at thereceiver.TDenseProtocol is still experimental and not yet available in the Java implementation.
TJSONProtocol Uses JSON for encoding of data.
TSimpleJSONProtocol A write-only protocol using JSON.Suitable for parsing by scripting
languages TDebugProtocol
Uses a human-readable text format to aid in debugging.
Transport Layer Tsocket Uses blocking socket I/O for transport.
TFramedTransport Sends data in frames, where each frame is preceded by a length.This
transport is required when using a non-blocking server. TFileTransport This transport writes to a file.While this transport is not included
with the Java implementation, it should be simple enough to implement.
TMemoryTransport Uses memory for I/O.The Java implementation uses a simple
ByteArrayOutputStream internally. TZlibTransport Performs compression using zlib. Used in conjunction with another
transport. Not available in the Java implementation.
Processor & Server Digunakan di sisi server, menerima input, melakukan
pemrosesan dengan bantuan handler, dan menghasilkan output
Thrift menyediakan pola server TSimpleServer A single-threaded server using std blocking io. Useful for testing.
TThreadPoolServer A multi-threaded server using std blocking io.
TNonblockingServer A multi-threaded server using non-blocking io (Java implementation
uses NIO channels).TFramedTransport must be used with this server.
Type System Tipe dasar: bool, byte, i16, i32, i64, double, string Tipe khusus: binary Struct: mirip denganC Containers: list, set, map Exception Services: terdiri atas nama fungsi yang disediakan oleh
server
Model Pengembangan Aplikasi Buat thrift file (definisi service) Implementasi handler dan server Implementasi client
Contohnamespace java if4031
typedef i32 int
service CalculatorService
{
int multiply(1:int n1, 2:int n2),
int add(1:int n1, 2:int n2),
}
Pembangkitan kode thrift –gen <bahasa> calculator.thrift
Menghasilkan: Kelas interface (Iface) service Kelas client dan Processor (untuk server) Kelas yang merepresentasikan struktur/tipe data dan exception
(jika ada)
public class CalculatorService {
public interface Iface {
public int multiply(int n1, int n2)throws org.apache.thrift.TException;
public int add(int n1, int n2)throws org.apache.thrift.TException;
}
public static class Client extends org.apache.thrift.TServiceClient implements Iface {
public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
...
int n2) throws org.apache.thrift.TException
n2) throws org.apache.thrift.TException
public int multiply(int n1,
{
send_multiply(n1, n2);
return recv_multiply();
}
public int add(int n1, int
{
send_add(n1, n2);
return recv_add();
}
Implementasi server Buat implementasi handler yang akan menangani request di sisi
serverpublic class CalculatorHandler implementsCalculatorService.Iface {
n1, int n2) throws@Overridepublic int multiply(int
TException {+ n1 + "," + n2System.out.println("Multiply("
+ ")");return n1 * n2;
}...
}
Implementasi server (simple) Program Utama:
CalculatorHandler handler = new CalculatorHandler();
CalculatorService.Processor processor = newCalculatorService.Processor(handler);
TServerTransport serverTransport = newTServerSocket(9090);
TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
server.serve();
Implementasi client
9090);
TTransport transport;
transport = new TSocket("localhost",
transport.open();
TProtocol protocol = newTBinaryProtocol(transport);
CalculatorService.Client client = newCalculatorService.Client(protocol);
int product = client.multiply(3,5);
Protocol Buffer (Protobuf) data serialization library description language (IDL) compiler library
fokus: efisiensi, language interoperability, usability multi language: official: C++, Java dan Python Dikembangkan dan digunakan di Google
Contoh deskripsipackage tutorial;option java_package = "com.example.tutorial"; option java_outer_classname = "AddressBookProtos"; message Person {requiredrequiredoptional
string name = 1; int32 id = 2; string email = 3;
= HOME];
enum PhoneType {MOBILE = 0; HOME = 1; WORK = 2;
}message PhoneNumber {
required string number = 1;optional PhoneType type = 2 [default
}repeated PhoneNumber phone = 4;
}
message AddressBook { repeated Person person = 1; }
contoh pemakaianPerson john =
Person.newBuilder().setId(1234).setName("John Doe").setEmail("[email protected]").addPhone(
Person.PhoneNumber.newBuilder().setNumber("555-4321").setType(Person.PhoneType.HOME)
).build();
Parsing & Serialization byte[] toByteArray();: serializes the message and returnsa byte
array containing its raw bytes. static Person parseFrom(byte[] data);: parses a message
from the given byte array. void writeTo(OutputStream output);: serializes the message
and writes it to anOutputStream. static Person parseFrom(InputStream input);: reads and
parses a message from anInputStream.