1. Información general
En este artículo, descubriremos cómo desarrollar aplicaciones cliente-servidor multiplataforma con la ayuda del marco RPC llamado Apache Thrift.
Cubriremos:
- Definición de tipos de datos e interfaces de servicio con IDL
- Instalar la biblioteca y generar las fuentes para diferentes idiomas
- Implementar las interfaces definidas en un lenguaje particular
- Implementación de software cliente / servidor
Si desea ir directamente a los ejemplos, vaya directamente a la sección 5.
2. Apache Thrift
Apache Thrift fue desarrollado originalmente por el equipo de desarrollo de Facebook y actualmente es mantenido por Apache.
En comparación con Protocol Buffers, que administran procesos de serialización / deserialización de objetos multiplataforma, Thrift se enfoca principalmente en la capa de comunicación entre los componentes de su sistema.
Thrift utiliza un lenguaje de descripción de interfaz (IDL) especial para definir tipos de datos e interfaces de servicio que se almacenan como archivos .thrift y que el compilador utiliza más tarde como entrada para generar el código fuente del software de cliente y servidor que se comunica a través de diferentes lenguajes de programación.
Para usar Apache Thrift en su proyecto, agregue esta dependencia de Maven:
org.apache.thrift libthrift 0.10.0
Puede encontrar la última versión en el repositorio de Maven.
3. Idioma de descripción de la interfaz
Como ya se describió, IDL permite definir interfaces de comunicación en un lenguaje neutral. A continuación encontrará los tipos admitidos actualmente.
3.1. Tipos de base
- bool - un valor booleano (verdadero o falso)
- byte : un entero de 8 bits con signo
- i16 : un entero de 16 bits con signo
- i32 : un entero de 32 bits con signo
- i64 : un entero de 64 bits con signo
- doble : un número de punto flotante de 64 bits
- cadena : una cadena de texto codificada con codificación UTF-8
3.2. Tipos especiales
- binario : una secuencia de bytes no codificados
- opcional : un tipo opcional de Java 8
3.3. Estructuras
Las estructuras de ahorro son equivalentes a las clases en los lenguajes de programación orientada a objetos, pero sin herencia. Una estructura tiene un conjunto de campos fuertemente tipados, cada uno con un nombre único como identificador. Los campos pueden tener varias anotaciones (ID de campo numérico, valores predeterminados opcionales, etc.).
3.4. Contenedores
Los contenedores de segunda mano son contenedores fuertemente tipificados:
- lista : una lista ordenada de elementos
- conjunto : un conjunto desordenado de elementos únicos
- mapa : un mapa de claves estrictamente únicas para valores
Los elementos del contenedor pueden ser de cualquier tipo de ahorro válido.
3.5. Excepciones
Las excepciones son funcionalmente equivalentes a las estructuras , excepto que heredan de las excepciones nativas.
3.6. Servicios
Los servicios son en realidad interfaces de comunicación definidas mediante tipos Thrift. Consisten en un conjunto de funciones con nombre, cada una con una lista de parámetros y un tipo de retorno.
4. Generación de código fuente
4.1. Ayuda de idioma
Existe una larga lista de idiomas admitidos actualmente:
- C ++
- C#
- Vamos
- Haskell
- Java
- Javascript
- Node.js
- Perl
- PHP
- Pitón
- Rubí
Puedes consultar la lista completa aquí.
4.2. Usando el archivo ejecutable de la biblioteca
Simplemente descargue la última versión, compílela e instálela si es necesario, y use la siguiente sintaxis:
cd path/to/thrift thrift -r --gen [LANGUAGE] [FILENAME]
En los comandos establecidos anteriormente, [LANGUAGE] es uno de los idiomas admitidos y [FILENAME ] es un archivo con definición IDL.
Tenga en cuenta la bandera -r . Le dice a Thrift que genere código de forma recursiva una vez que se da cuenta de que se incluye en un archivo .thrift determinado .
4.3. Usando el complemento Maven
Agregue el complemento en su archivo pom.xml :
org.apache.thrift.tools maven-thrift-plugin 0.1.11 path/to/thrift thrift-sources generate-sources compile
Después de eso, simplemente ejecute el siguiente comando:
mvn clean install
Tenga en cuenta que este complemento ya no tendrá más mantenimiento. Visite esta página para obtener más información.
5. Ejemplo de una aplicación cliente-servidor
5.1. Definición de archivo de ahorro
Escribamos un servicio simple con excepciones y estructuras:
namespace cpp com.baeldung.thrift.impl namespace java com.baeldung.thrift.impl exception InvalidOperationException { 1: i32 code, 2: string description } struct CrossPlatformResource { 1: i32 id, 2: string name, 3: optional string salutation } service CrossPlatformService { CrossPlatformResource get(1:i32 id) throws (1:InvalidOperationException e), void save(1:CrossPlatformResource resource) throws (1:InvalidOperationException e), list getList() throws (1:InvalidOperationException e), bool ping() throws (1:InvalidOperationException e) }
As you can see, the syntax is pretty simple and self-explanatory. We define a set of namespaces (per implementation language), an exception type, a struct, and finally a service interface which will be shared across different components.
Then just store it as a service.thrift file.
5.2. Compiling and Generating a Code
Now it's time to run a compiler which will generate the code for us:
thrift -r -out generated --gen java /path/to/service.thrift
As you might see, we added a special flag -out to specify the output directory for generated files. If you did not get any errors, the generated directory will contain 3 files:
- CrossPlatformResource.java
- CrossPlatformService.java
- InvalidOperationException.java
Let's generate a C++ version of the service by running:
thrift -r -out generated --gen cpp /path/to/service.thrift
Now we get 2 different valid implementations (Java and C++) of the same service interface.
5.3. Adding a Service Implementation
Although Thrift has done most of the work for us, we still need to write our own implementations of the CrossPlatformService. In order to do that, we just need to implement a CrossPlatformService.Iface interface:
public class CrossPlatformServiceImpl implements CrossPlatformService.Iface { @Override public CrossPlatformResource get(int id) throws InvalidOperationException, TException { return new CrossPlatformResource(); } @Override public void save(CrossPlatformResource resource) throws InvalidOperationException, TException { saveResource(); } @Override public List getList() throws InvalidOperationException, TException { return Collections.emptyList(); } @Override public boolean ping() throws InvalidOperationException, TException { return true; } }
5.4. Writing a Server
As we said, we want to build a cross-platform client-server application, so we need a server for it. The great thing about Apache Thrift is that it has its own client-server communication framework which makes communication a piece of cake:
public class CrossPlatformServiceServer { public void start() throws TTransportException { TServerTransport serverTransport = new TServerSocket(9090); server = new TSimpleServer(new TServer.Args(serverTransport) .processor(new CrossPlatformService.Processor(new CrossPlatformServiceImpl()))); System.out.print("Starting the server... "); server.serve(); System.out.println("done."); } public void stop() { if (server != null && server.isServing()) { System.out.print("Stopping the server... "); server.stop(); System.out.println("done."); } } }
First thing is to define a transport layer with the implementation of TServerTransport interface (or abstract class, to be more precise). Since we are talking about server, we need to provide a port to listen to. Then we need to define a TServer instance and choose one of the available implementations:
- TSimpleServer – for simple server
- TThreadPoolServer – for multi-threaded server
- TNonblockingServer – for non-blocking multi-threaded server
And finally, provide a processor implementation for chosen server which was already generated for us by Thrift, i.e. CrossPlatofformService.Processor class.
5.5. Writing a Client
And here is the client's implementation:
TTransport transport = new TSocket("localhost", 9090); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); CrossPlatformService.Client client = new CrossPlatformService.Client(protocol); boolean result = client.ping(); transport.close();
From a client perspective, the actions are pretty similar.
First of all, define the transport and point it to our server instance, then choose the suitable protocol. The only difference is that here we initialize the client instance which was, once again, already generated by Thrift, i.e. CrossPlatformService.Client class.
Since it is based on .thrift file definitions we can directly call methods described there. In this particular example, client.ping() will make a remote call to the server which will respond with true.
6. Conclusion
In this article, we've shown you the basic concepts and steps in working with Apache Thrift, and we've shown how to create a working example which utilizes Thrift library.
Como es habitual, todos los ejemplos siempre se pueden encontrar en el repositorio de GitHub.