Top Banner
Pengembangan Aplikasi Terdistribusi Pandapotan Napitupulu Apache Thrift Berdasarkan Slide Achmad Imam Kistijantoro
23

4.distributed objects -_thrift_dan_protocol_buffer

Jan 20, 2017

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 4.distributed objects -_thrift_dan_protocol_buffer

Pengembangan AplikasiTerdistribusiPandapotan Napitupulu

Apache ThriftBerdasarkan Slide Achmad Imam Kistijantoro

Page 2: 4.distributed objects -_thrift_dan_protocol_buffer

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

Page 3: 4.distributed objects -_thrift_dan_protocol_buffer

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

Page 4: 4.distributed objects -_thrift_dan_protocol_buffer

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

Page 5: 4.distributed objects -_thrift_dan_protocol_buffer

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.

Page 6: 4.distributed objects -_thrift_dan_protocol_buffer

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.

Page 7: 4.distributed objects -_thrift_dan_protocol_buffer

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.

Page 8: 4.distributed objects -_thrift_dan_protocol_buffer

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

Page 9: 4.distributed objects -_thrift_dan_protocol_buffer

Model Pengembangan Aplikasi Buat thrift file (definisi service) Implementasi handler dan server Implementasi client

Page 10: 4.distributed objects -_thrift_dan_protocol_buffer

Contohnamespace java if4031

typedef i32 int

service CalculatorService

{

int multiply(1:int n1, 2:int n2),

int add(1:int n1, 2:int n2),

}

Page 11: 4.distributed objects -_thrift_dan_protocol_buffer

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)

Page 12: 4.distributed objects -_thrift_dan_protocol_buffer

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;

}

Page 13: 4.distributed objects -_thrift_dan_protocol_buffer

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();

}

Page 14: 4.distributed objects -_thrift_dan_protocol_buffer

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;

}...

}

Page 15: 4.distributed objects -_thrift_dan_protocol_buffer

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();

Page 16: 4.distributed objects -_thrift_dan_protocol_buffer

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);

Page 17: 4.distributed objects -_thrift_dan_protocol_buffer

Pengembangan AplikasiTerdistribusi

Protocol BufferAchmad Imam Kistijantoro ([email protected])

Page 18: 4.distributed objects -_thrift_dan_protocol_buffer

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

Page 19: 4.distributed objects -_thrift_dan_protocol_buffer

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; }

Page 20: 4.distributed objects -_thrift_dan_protocol_buffer

Pembangkitan kode

protoc -I=<src> --java_out=<dst> $srcdir/tutorial.proto

Page 21: 4.distributed objects -_thrift_dan_protocol_buffer

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();

Page 22: 4.distributed objects -_thrift_dan_protocol_buffer

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.

Page 23: 4.distributed objects -_thrift_dan_protocol_buffer

FileOutputStream output = new FileOutputStream(args[0]);

addressBook.build().writeTo(output);

AddressBook addressBook = AddressBook.parseFrom(new

FileInputStream(args[0]));