Thrift Introduction:
Thrift is a software framework for the development of extensible, cross-language services. It combines a powerful software stack and code generation engine to build in C + +, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, node. js, Smalltalk, and OCaml the seamless and efficient service between these programming languages.
Thrift allows you to define a simple definition file for data types and service interfaces to serve as input files, and the compiler generates code to easily generate RPC client and server communication seamlessly across programming languages.
Installation of thrift under the Windows environment:
1. Download the appropriate EXE file to the thrift website
2. Place it in a directory and rename it to Thrift.exe
3. Add the directory to the PATH environment variable
4. Run command thrift-version under command line, if "Thrift version xx.xx.xx" appears, the installation is successful.
Basic usage of thrift:
Thrift--gen Language Thrift filename
Where language is the programming language of the code to be generated, Thrift filename is based on the Thrift file.
Thrift's Hello World program:
0. Dependent jar Packages:
Libthrift-0.9.1.jar
Log4j.jar
Slf4j-api.jar
Slf4j-log4j12.jar
1. Generate a Helloworld.thrift file with the following content:
namespace Java com.zhang.thrift.server//defining a namespace
Service HelloWorldService {//Define services Class
String HelloWorld ()//method for defining the service
}
Where "Java" is the code language to be generated (I don't understand why it must be, the parameters of the thrift command have the generated code language).
Run the command in the same directory as the thrift file Thrift--gen java helloworld.thrift
A Gen-java folder is generated in this directory, with one class Com.zhang.thrift.server.HelloWorldService
2. Create a new class to implement the Com.zhang.thrift.server.HelloWorldService.Iface interface
public class Helloworldserviceimpl implements helloworldservice.iface{@Override public String HelloWorld () throws T Exception {return "Hello world!"; }}
3. Create a class that acts as a server-side
package com.zhang.thrift.server.java;import org.apache.thrift.server.tserver;import org.apache.thrift.server.tserver.args;import org.apache.thrift.server.tsimpleserver;import org.apache.thrift.transport.tssltransportfactory;import org.apache.thrift.transport.tssltransportfactory.tssltransportparameters;import Org.apache.thrift.transport.tserversocket;import org.apache.thrift.transport.tservertransport;import com.zhang.thrift.server.helloworldservice;import com.zhang.thrift.server.impl.helloworldserviceimpl ;p ublic class javaserver {public static helloworldserviceimpl Helloworldserviceimpl;public static helloworldservice.processor processor;public static void main (String[] args) {try {helloWorldServiceImpl = new Helloworldserviceimpl ();p rocessor = new helloworldservice.processor (HelloWorldServiceImpl); Runnable simple = nEw runnable () {public void run () {simple (processor);}};/ /runnable secure = new runnable () {//public void run () {//secure ( processor);//}//};new thread (Simple). Start ();//new thread (Secure). Start (); catch (exception x) {x.printstacktrace ();}} Public static void simple (helloworldservice.processor processor) {try { Tservertransport servertransport = new tserversocket (8080); Tserver server = new tsimpleserver (New args (Servertransport). Processor (processor));// use this for a multithreaded server// tserver server = new tthreadpoolserver (New// tthreadpoolserver.args (Servertransport). Processor (processor)); System.out.println ("Starting the simple server ..."); Server.serve ();} catch (exception e) {e.printstacktrace ();}}
4. Create a class that, as a client, invokes the test on the service.
package com.zhang.thrift.server.java;import org.apache.thrift.texception;import org.apache.thrift.protocol.tbinaryprotocol;import org.apache.thrift.protocol.tprotocol;import org.apache.thrift.transport.tsocket;import org.apache.thrift.transport.ttransport;import Com.zhang.thrift.server.helloworldservice;public class javasimpleclient {public static void main (String[] args) {try {ttransport transport;transport = new tsocket ("localhost", 8080); Transport.open (); Tprotocol protocol = new tbinaryprotocol (transport); Helloworldservice.client client = new helloworldservice.client (protocol);p Erform (Client); Transport.close ();} catch (texception x) {x.printstacktrace ();}} Private static void perform (helloworldservice.client client) throws TException {string result = client.helloworld (); SySTEM.OUT.PRINTLN (result);}}
This article is from the "theYllwRvrNo98" blog, make sure to keep this source http://yellowriver.blog.51cto.com/8753974/1673444
Hello World of 1.thrift