4. Thrift
Thrift와 Protocol Buffer와 비교
Thrift Protocol Buffer
개발사 Facebook Apache Google
채택사 Facebook, Hadoop (Eco
System 관련)
Google
사용자 많다 보통
지원 대부분 C++,java,python
지원기능 많다 적은편
자료형지원 Map, List, Set등 지원 지원안함
5. Thrift
Thrift 설치
• 설치준비 필요환경
– https://thrift.apache.org/docs/install/
– Basic requirements
• A relatively POSIX-compliant *NIX system
• Cygwin or MinGW can be used on Windows (but there are better options, see
below)
• g++ 4.2
• boost 1.53.0
• Runtime libraries for lex and yacc might be needed for the compiler.
– Requirements for building from source
• GNU build tools:
• autoconf 2.65
• automake 1.13
• libtool 1.5.24
• pkg-config autoconf macros (pkg.m4)
• lex and yacc (developed primarily with flex and bison)
• libssl-dev
– Requirements for building the compiler from source on Windows
• Visual Studio C++
• Flex and Bison (e.g. the WinFlexBison package)
...
6. Thrift
Thrift 설치
• Thrift 설치환경준비
sudo yum -y update
sudo yum -y groupinstall "Development Tools"
sudo yum install -y wget
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar xvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=/usr
make
sudo make install
cd ..
wget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz
tar xvf automake-1.14.tar.gz
cd automake-1.14
./configure --prefix=/usr
make
sudo make install
cd ..
https://thrift.apache.org/docs/install/centos
7. Thrift
Thrift 설치
• Thrift 설치환경준비
wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
tar xvf bison-2.5.1.tar.gz
cd bison-2.5.1
./configure --prefix=/usr
make
sudo make install
cd ..
git clone https://git-wip-us.apache.org/repos/asf/thrift.git
cd thrift
./bootstrap.sh
./configure --with-lua=no
make
sudo make install
8. Thrift
Thrift 프로젝트 생성
• Ant 다운로드 및 설치
– yum -y install ant
이후, Thrift 압축해제 디렉터리에 가서 다음 명령어를 실행함
– ant -Dproxy.enabled=1 -Dproxy.host=myproxyhost -Dproxy.user=thriftuser -
Dproxy.pass=topsecret
https://thrift.apache.org/lib/java
9. Thrift
Thrift 프로젝트 생성
• Thrift 코드 Import시 의존라이브러리
– 아래 의존라이브러리를 다운로드후 ‘add JAR’하여 프로젝트에 포함
– slf4j
• http://www.slf4j.org/dist/
• slf4j-api-1.7.9.jar
– Libthrift
• http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.9.2/
• libthrift-0.9.2.jar
10. Thrift
Thrift Interface Description Language
• Thrift I.D.L 작성
– 공통으로 사용할 인터페이스를 정의합니다.
namespace java tutorial.arithmetic.gen
namespace js tutorial.arithmetic.gen
//Namespace setting for each programming language
typedef i64 long
typedef i32 int
service ArithmeticService { // defines simple arithmetic service
long add(1:int num1, 2:int num2),
long multiply(1:int num1, 2:int num2),
}
Thrift –gen java calc.thrift로 java코드 생성시 파일명과 서비스명이됨
< calc.thrift >
https://thrift.apache.org/docs/idl
11. Thrift
Thrift I.D.L에 기반한 인터페이스 코드 생성
• IDL기반 언어별 Thrift 인터페이스 생성
• Node.JS 예
– gen-node에 생성됨
• JAVA 예
$ thrift --gen java calc.thrift
thrift --gen <language> <Thrift filename>
thrift --gen js:node calc.thrift
12. Thrift
Thrift 서버
• Java 인터페이스 코드생성
– # thrift --gen java calc.thrift
– ‘namespace java tutorial.arithmetic.gen’에 따라 디렉터리 생성됨
– Tutorial 디렉터리를 자바 프로젝트 폴더로 옮김
13. Thrift
Thrift 서버
public class Server {
private void start() {
try {
TServerSocket serverTransport = new TServerSocket(7911);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new
ArithmeticServiceImpl());
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).
processor(processor));
System.out.println("Starting server on port 7911 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Server srv = new Server();
srv.start();
}
}
14. Thrift
Thrift 서버
public class ArithmeticServiceImpl implements ArithmeticService.Iface{
public long add(int num1, int num2) throws TException {
return num1 + num2;
}
public long multiply(int num1, int num2) throws TException {
return num1 * num2;
}
}
https://github.com/Flipkart/phantom/tree/master/sample-thrift-proxy/src/main/java/thrift