Thrift java服务器与客户端示例
  WaYJTbj6RMqU 2023年11月02日 24 0

简单的实现一个PING的功能


1.安装thrift


http://thrift.apache.org/download/


人人网镜像下载:


http://labs.renren.com/apache-mirror/thrift/0.6.1/thrift-0.6.1.exe


2.编写Thrift文件(定义接口,结构,异常等),保存为test.thrift


namespace java net.johnc.thrift
service Test{
 
  void  ping( 1 : i32 length)
 
}

3.生成接口代码


把thrift-0.6.1.exe和test.thrift文件放在同一个目录,当然也可以把thrift-0.6.1.exe文件放进环境变量


进入DOS命令执行:thrift-0.6.1.exe --gen java test.thrift


生成文件 gen-java/net/johnc/thrift/Test.java


4.编写服务端接口实现类


在POM.xml文件加入以下依赖:

<dependency>
       <groupId>org.apache.thrift</groupId>
       <artifactId>libthrift</artifactId>
       <version> 0.6 . 1 </version>
   </dependency>


 把生成的Test.java复制到项目下


1 package net.johnc.thrift;
2
3   import org.apache.thrift.TException;
4
5   public class TestImpl implements Test.Iface {
6
7 public void ping( int length) throws TException {
8 System.out.println( " calling ping ,length= " + length);
9 }
10
11 }


5.编写启动服务代码



1 package net.johnc.thrift;
2
3   import net.johnc.thrift.Test.Processor;
4
5   import org.apache.thrift.protocol.TBinaryProtocol;
6   import org.apache.thrift.protocol.TBinaryProtocol.Factory;
7   import org.apache.thrift.server.TServer;
8   import org.apache.thrift.server.TThreadPoolServer;
9   import org.apache.thrift.server.TThreadPoolServer.Args;
10   import org.apache.thrift.transport.TServerSocket;
11   import org.apache.thrift.transport.TTransportException;
12
13   public class Server {
14 public void startServer() {
15 try {
16
17 TServerSocket serverTransport = new TServerSocket( 1234 );
18
19 Test.Processor process = new Processor( new TestImpl());
20
21 Factory portFactory = new TBinaryProtocol.Factory( true , true );
22
23 Args args = new Args(serverTransport);
24 args.processor(process);
25 args.protocolFactory(portFactory);
26
27 TServer server = new TThreadPoolServer(args);
28 server.serve();
29 } catch (TTransportException e) {
30 e.printStackTrace();
31 }
32 }
33
34 public static void main(String[] args) {
35 Server server = new Server();
36 server.startServer();
37 }
38 }



6.编写客户端代码


1 package net.johnc.thrift;
2
3   import org.apache.thrift.TException;
4   import org.apache.thrift.protocol.TBinaryProtocol;
5   import org.apache.thrift.protocol.TProtocol;
6   import org.apache.thrift.transport.TSocket;
7   import org.apache.thrift.transport.TTransport;
8   import org.apache.thrift.transport.TTransportException;
9
10   public class Client {
11
12 public void startClient() {
13 TTransport transport;
14 try {
15 transport = new TSocket( " localhost " , 1234 );
16 TProtocol protocol = new TBinaryProtocol(transport);
17 Test.Client client = new Test.Client(protocol);
18 transport.open();
19 client.ping( 2012 );
20 transport.close();
21 } catch (TTransportException e) {
22 e.printStackTrace();
23 } catch (TException e) {
24 e.printStackTrace();
25 }
26 }
27
28 public static void main(String[] args) {
29 Client client = new Client();
30 client.startClient();
31 }
32 }



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jnZtF7Co41Wg   2023年11月22日   22   0   0 linuxApacheci
  UP4ONKOBnkdD   2023年11月28日   22   0   0 java
  9JCEeX0Eg8g4   2023年12月10日   30   0   0 应用程序javaApache
  KRsXEGSB49bk   2023年11月27日   28   0   0 javaApache
  jnZtF7Co41Wg   2023年11月24日   28   0   0 mysqlApachecentos