1. Background
2. Environment Installation
Download thrift compiler
Download: http: //www.apache.org/dyn/closer.cgi path = / thrift / 0.12.0 / thrift-0.12.0.exe?
Click to download Figure
After removing the download version:
Set the environment variable:
Check whether the installation was successful:
Environmental perfect!
3.demo demo
Write .thrift file
namespace java com.wfd360.demo07.thrift namespace py py.thrift.generated typedef i16 short typedef i32 int typedef i64 long typedef bool boolean typedef string String struct Person { 1: optional String username, 2: optional int age, 3: optional boolean married } exception DataException { 1: optional String message, 2: optional String callStack, 3: optional String date } service PersonService { Person getPersonByUsername(1: required String username) throws (1: DataException dataException), void savePerson(1: required Person person) throws (1: DataException dataException) }
Use thrift compiled java files, execute the following command:
View the generated code:
Copy the code to the project
Write to implement the interface
package com.wfd360.demo07; import com.wfd360.demo07.thrift.DataException; import com.wfd360.demo07.thrift.Person; import com.wfd360.demo07.thrift.PersonService; import org.apache.thrift.TException; public class PersonServiceImpl implements PersonService.Iface { @Override public Person getPersonByUsername(String username) throws DataException, TException { System.out.println("Got Client Param: " + username); Person person = new Person(); person.setUsername(username); person.setAge(20); person.setMarried(false); return person; } @Override public void savePerson(Person person) throws DataException, TException { System.out.println("Got Client Param: "); System.out.println(person.getUsername()); System.out.println(person.getAge()); System.out.println(person.isMarried()); } }
Write server
package com.wfd360.demo07; import com.wfd360.demo07.thrift.PersonService; import org.apache.thrift.TProcessorFactory; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.server.THsHaServer; import org.apache.thrift.server.TServer; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TNonblockingServerSocket; public class ThriftServer { public static void main(String[] args) throws Exception { TNonblockingServerSocket socket = new TNonblockingServerSocket(8899); THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4); PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl()); arg.protocolFactory(new TCompactProtocol.Factory()); arg.transportFactory(new TFramedTransport.Factory()); arg.processorFactory(new TProcessorFactory(processor)); TServer server = new THsHaServer(arg); System.out.println("Thrift Server Started!"); server.serve(); } }
Write a client
package com.wfd360.demo07; import com.wfd360.demo07.thrift.Person; import com.wfd360.demo07.thrift.PersonService; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; public class ThriftClient { public static void main(String[] args) { TTransport transport = new TFramedTransport(newTSocket ( "localhost", 8899), 600 ); TProtocol Protocol = new new TCompactProtocol (Transport); PersonService.Client Client = new new PersonService.Client (Protocol); the try { transport.open (); // The interface name, service call the interface end Person person = client.getPersonByUsername ( "wild" ); System.out.println (person.getUsername ()); System.out.println (person.getAge ()); System.out.println (person.isMarried ()); System.out.println ( "-------" ); the Person PERSON2 = new newThe Person (); person2.setUsername ( "Zhang Min" ); person2.setAge ( 30 ); person2.setMarried ( to true ); // According to the interface name, call the service side of the interface client.savePerson (PERSON2); System.out.println ( "client calls completed ------ --------" ); } the catch (Exception EX) { the throw new new a RuntimeException (ex.getMessage (), EX); } the finally { transport.close ( ); } } }
Test: First start the server, start the client, the server can find the interface method of the client can call normal
The server console:
Client console:
perfect!