博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thrift java服务器与客户端示例 - john c - 博客园
阅读量:6934 次
发布时间:2019-06-27

本文共 2777 字,大约阅读时间需要 9 分钟。

Posted on 2011-06-19 03:04 阅读(3540) 评论(0)

简单的实现一个PING的功能

1.安装thrift

人人网镜像下载:

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
}
你可能感兴趣的文章
百度开源其NLP主题模型工具包,文本分类等场景可直接使用L——LDA进行主题选择本质就是降维,然后用于推荐或者分类...
查看>>
Oracle11g创建表空间
查看>>
try~Catch语句中异常的处理过程
查看>>
贝塞尔曲线与CAShapeLayer的关系以及Stroke动画
查看>>
阅读《Android 从入门到精通》(29)——四大布局
查看>>
IT运维的定义
查看>>
Temporary ASP.Net Files探究
查看>>
CSDN开源夏令营 百度数据可视化实践 ECharts(8)
查看>>
poj 1284 Primitive Roots(原根+欧拉函数)
查看>>
OpenJudge百炼习题解答(C++)--题4010:2011
查看>>
Oracle PL/SQL语句基础学习笔记(上)
查看>>
MVC,MVP 和 MVVM 的图示
查看>>
正則表達式常见例题
查看>>
STM32 使用 FreeRTOS过程记录
查看>>
ASP.NET Core身份认证服务框架IdentityServer4(2)-整体介绍
查看>>
P1064 金明的预算方案
查看>>
在 Spring 4.3.9下升级 Velocity 1.7.x to Velocity 2.0.x 出现的问题
查看>>
Python排序dict之list数组
查看>>
OKR
查看>>
cmake 常用变量和常用环境变量查表手册
查看>>