Menu Close

Programmieren mit Thrift

Heute möchte ich die notwendigen Schritte beschreiben, um ein Grundgerüst mit Apache Thrift ans Laufen zu kriegen. Ich muss ehrlich gestehen, dass es mir nicht alleine gelungen ist dies zu bewerkstelligen. Einen großen Dank dafür an meinen Partner Nils.

Die Umgebung

Für das Praktikum setze ich eine VM mit folgender Konfiguration ein:

  • VM-Version: Workstation 8.0
  • RAM: 2 GB
  • CPU: 1 Prozessor mit 2 Kernen
  • HDD: 20 GB
  • OS: lubuntu 14.04 x64 (Link)

Vorbereitung

Werkzeuge und Compiler installieren

sudo apt-get install build-essential

Eclipse für C and C++ installieren

sudo apt-get install eclipse eclipse-cdt g++

Anforderungen von Thrift installieren

sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev libglib2.0-dev 

Git installieren

sudo apt-get install git

Thrift kopieren

git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift

Installation

Wechsle das Verzeichnis

cd thrift

Erzeuge die Konfigurationsskripte

./bootstrap.sh

Konfiguriere Thrift

./configure

Baue Thrift

make

Installiere Thrift

sudo make install

Kopiere libthrift.so

sudo cp /usr/local/lib/libthrift-1.0.0-dev.so /usr/lib/

Das Grundgerüst

Erzeugen der Thrift-Datei

  1. Erstelle eine Datei namens VS3.thrift
  2. Definiere darin Variablen, Konstanten, Strukturen und Dienste
service VS3 {
 void ping()
}

Erzeugen der Sourcen

thrift -r --gen cpp VS3.thrift

Wechsle das Verzeichnis

cd gen-cpp/

Diese Dateien sollten nun vorhanden sein

  1. VS3.cpp
  2. VS3.h
  3. VS3_constants.cpp
  4. VS3_constants.h
  5. VS3_types.cpp
  6. VS3_types.h
  7. VS3_server.skeleton.cpp

Kopieren der Serverdatei

cp VS3_server.skeleton.cpp VS3_server.cpp

Erzeugen des Makefile

GEN_SRC := VS3.cpp VS3_constants.cpp VS3_types.cpp
GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))

THRIFT_DIR := /usr/local/include/thrift
BOOST_DIR := /usr/local/include

INC := -I$(THRIFT_DIR) -I$(BOOST_DIR)

.PHONY: all clean

all: VS3_server VS3_client

%.o: %.cpp
	$(CXX) -Wall -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H $(INC) -c $< -o $@

VS3_server: VS3_server.o $(GEN_OBJ)
	$(CXX) $^ -o $@ -L/usr/local/lib -lthrift 

VS3_client: VS3_client.o $(GEN_OBJ)
	$(CXX) $^ -o $@ -L/usr/local/lib -lthrift 

clean:
	$(RM) *.o VS3_server VS3_client

Client implementieren

  1. Erstelle eine Datei namens VS3_client.cpp
  2. Implementiere die Client-Funktionen
#include "VS3.h"

#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>

using namespace std;

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using boost::shared_ptr;

int main() 
{
	try
	{
		shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
		shared_ptr<TTransport> transport(new  TBufferedTransport(socket));
		shared_ptr<TProtocol> protocol(new  TBinaryProtocol(transport));
		VS3Client client(protocol);
		transport->open();

		// Funktionen
		client.ping();

		transport->close();

		// Funktionen
		cout << "Hallo Welt";
	}

	catch(TException &tx) 
	{
		cout << "error calling server: " << tx.what() << endl;
	}
		
	return 0;
}

Baue Client und Server

make

Start Client und Server

./VS3_server
./VS3_client

Quellen

  • http://thrift.apache.org/download
  • http://thrift.apache.org/docs/install/ubuntu
  • http://thrift.apache.org/docs/BuildingFromSource

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahren Sie, wie Ihre Kommentardaten verarbeitet werden.

Index