Ir para o conteúdo

RPC: Thrift

Thrift

Instalação

IDL Thrift

  • Serviços
    1
    2
    3
    4
    5
    service ChaveValor {
        void set(1:i32 key, 2:string value),
        string get(1:i32 key) throws (1:KeyNotFound knf),
        void delete(1:i32 key)
    }
    
  • Não se pode retornar NULL!!!
  • Exceções
    1
    2
    3
    4
    exception KeyNotFound {
       1:i64 hora,
       2:string chaveProcurada="thrifty"
    }
    

Exemplo: chavevalor.thrift

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
namespace java chavevalor
namespace py chavevalor

exception KeyNotFound
{
   1:i64 time,
   2:i32 key
}

service ChaveValor
{
    string getKV(1:i32 key) throws (1:KeyNotFound knf),
    bool setKV(1:i32 key, 2:string value),
    void delKV(1:i32 key)
}

Compilação

thrift --gen java chavevalor.thrift

thrift --gen py chavevalor.thrift

ChaveValorHandler.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package chavevalor;

import org.apache.thrift.TException;
import java.util.HashMap;
import chavevalor.*;

public class ChaveValorHandler implements ChaveValor.Iface {
   private HashMap<Integer,String> kv = new HashMap<>();
   @Override
   public String getKV(int key) throws TException {
       if(kv.containsKey(key))
          return kv.get(key);
       else
          throw new KeyNotFound();
   }
   @Override
   public boolean setKV(int key, String valor) throws TException {
       kv.put(key,valor);
       return true;
   }
   @Override
   public void delKV(int key) throws TException {
       kv.remove(key);
   }    
}

Arquitetura

  • Runtime library -- componentes podem ser selecionados em tempo de execução e implementações podem ser trocadas
  • Protocol -- responsável pela serialização dos dados
  • TBinaryProtocol
  • TJSONProtocol
  • TDebugProtocol
  • ...
  • Transport -- I/O no ``fio''
  • TSocket
  • TFramedTransport (non-blocking server)
  • TFileTransport
  • TMemoryTransport
  • Processor -- Conecta protocolos de entrada e saída com o \emph{handler}

  • Handler -- Implementação das operações oferecidas

  • Server -- Escuta portas e repassa dados (protocolo) para o processors
  • TSimpleServer
  • TThreadPool
  • TNonBlockingChannel

Execução

1
2
3
javac  -cp jars/libthrift0.16.0.jar:jars/slf4japi1.7.36.jar:gen-java  -d . *.java 
java -cp jars/libthrift0.16.0.jar:jars/slf4japi1.7.36.jar:gen-java:. chavevalor.ChaveValorServer
java -cp jars/libthrift0.16.0.jar:jars/slf4japi1.7.36.jar:gen-java:. chavevalor.ChaveValorClient

Exercícios - Thrift

  • Usando o handler fornecido no threcho acima, implemente o servidor em java
  • Implemente o cliente em Java
  • Execute servidor e cliente e teste a invocação dos métodos remotos implementados
  • [EXTRA] Implemente o cliente em python e execute com o servidor em Java

Referências