1.背景
2.环境安装
下载thrift编译器
下载地址:http://www.apache.org/dyn/closer.cgi?path=/thrift/0.12.0/thrift-0.12.0.exe
点击如图进行下载
下载后去掉版本号:
设置环境变量:
检查是否安装成功:
环境完美!
3.demo演示
编写.thrift文件
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) }
利用thrift编译生成java文件,执行命令如下:
查看生成的代码:
将代码拷贝到项目中
编写实现接口
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()); } }
编写服务端
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(); } }
编写客户端
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(new TSocket("localhost", 8899), 600); TProtocol protocol = new TCompactProtocol(transport); PersonService.Client client = new PersonService.Client(protocol); try { transport.open(); //根据接口名称,调用服务端的接口 Person person = client.getPersonByUsername("无忌"); System.out.println(person.getUsername()); System.out.println(person.getAge()); System.out.println(person.isMarried()); System.out.println("-------"); Person person2 = new Person(); person2.setUsername("张敏"); person2.setAge(30); person2.setMarried(true); //根据接口名称,调用服务端的接口 client.savePerson(person2); System.out.println("------客户端调用完成--------"); } catch (Exception ex) { throw new RuntimeException(ex.getMessage(), ex); } finally { transport.close(); } } }
测试:先启动服务端,在启动客户端,可以发现客户端能正常调用服务端的接口方法
服务端控制台:
客户端控制台:
完美!