tirift程序(例1)
thrift链接:https://www.cnblogs.com/sxrtb/p/12209455.html
一 thrift文档编写(文件名称为data.thrift)
namespace java 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) }
文档说明请参考(https://www.cnblogs.com/sxrtb/p/12209719.html)。
文档编写完成之后,使用thrift --gen <language> <Thrift filename>进行文件编辑(对于java开发人员,运行thrift --gen java src/thrift/data.thrift),
我使用的文件目录
二 引入thrift依赖
对于maven管理
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.13.0</version> </dependency>
对于gradle管理
org.apache.thrift:libthrift:0.13.0
注意版本,应该为你本机安装的thrift版本信息
三 将生产后的文件copy到资源目录
四 编写服务端实现
1.编写实现thrift文件中Service的接口实现类
package com.study.thrift; import thrift.generated.DataException; import thrift.generated.Person; import thrift.generated.PersonService; import java.security.acl.Permission; public class PersonServiceImpl implements PersonService.Iface { @Override public Person getPersonByUsername(String username) throws DataException, org.apache.thrift.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, org.apache.thrift.TException { System.out.println("Got Client Param: "); System.out.println(person.getUsername()); System.out.println(person.getAge()); System.out.println(person.isMarried()); } }
2.编写服务启动代码
package com.study.thrift; 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; import thrift.generated.PersonService; 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 Stated"); server.serve(); } }
五 编写客户端实现
通常情况下,客户端和服务端不在同一个服务下,远程连接调用,需要ip+端口,我现在使用本机调用
package com.study.thrift; 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; import thrift.generated.Person; import thrift.generated.PersonService; 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()); }catch (Exception e){ throw new RuntimeException(e.getMessage(),e); }finally { transport.close(); } } }
好了,到现在为止,一个简单的thrift调用已经实现了。
posted on 2020-02-08 15:49 xingshouzhan 阅读(268) 评论(0) 编辑 收藏 举报