Java Thrift服务器和客户端创建
Posted 陈晓婵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Thrift服务器和客户端创建相关的知识,希望对你有一定的参考价值。
Thrift是一个RPC软件框架,
解决各系统间大数据量的传输通信以及系统之间语言环境不同需要跨平台的特性.近期的项目中用到了Thrift,做了一个简单的demo.环境:
1.eclipse
2.Thrift-0.10.0
Demo:
新建两个maven项目,thriftServer和thriftClient.其中thriftServer是服务端,thriftClient是客户端
Pom文件
分别在thriftServer和thriftClient的pom文件中添加下面的引用
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
ThriftServer
HelloWorldService.java
HelloWorldService.java是利用thrift命令生成的
(1)在本地磁盘上创建HelloWorld.thrift.
Service HelloWorldService
string sayHello(1:string username)
其中1:string username代表sayHello方法的第一个参数为username,参数类型为String.
如果有2个参数,第二个参数为String类型的pwd,写法为:
service HelloWorldService
string sayHello(1:string username,2:string pwd)
(2)利用thrift生成HelloWorldService.java文件,cmd指令下进入thrift当前目录下输入命令
thrift.exe -gen java HelloWorld.thrift
效果图:
效果图:
将生成的HelloWorldService.java复制到ThriftServer的src/main/java目录下.
由于HelloWorldService.java生成的代码比较多,在这里不贴出来了,有需要的,留言给我...
HelloWorldImpl.java
HelloWorldImpl.java是HelloWorldService.java的实现类,HelloWorldImpl实现HelloWorldService.IFace.
package com.bee.thrift.service;
import org.apache.thrift.TException;
public class HelloWorldImpl implements HelloWorldService.Iface
public String sayHello(String username) throws TException
return "Hi," + username
+ " welcome to my blog ";
HelloWorldServer.java
package com.bee.thrift.service;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
public class HelloWorldServer
public final static int SERVER_PORT = 7099;
private static String SERVER_IP = "localhost";
public void startServer()
try
System.out.println("HelloWorld Server start...");
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args args = new TServer.Args(serverTransport);
TProcessor process = new HelloWorldService.Processor(
new HelloWorldImpl());
TBinaryProtocol.Factory portFactory = new TBinaryProtocol.Factory(
true, true);
args.processor(process);
args.protocolFactory(portFactory);
TServer server = new TSimpleServer(args);
server.serve();
catch (Exception e)
System.out.println("Server start error");
e.printStackTrace();
public static void main(String[] args)
HelloWorldServer server = new HelloWorldServer();
server.startServer();
ThriftClient
HelloWorldClient.java
package com.bee.thrift.service;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class HelloWorldClient
public static final int SERVER_PORT = 7099;
public static final String SERVER_IP = "localhost";
public void startClient(String username)
TTransport tTransport = null;
try
tTransport = new TSocket(SERVER_IP, SERVER_PORT);
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(tTransport);
HelloWorldService.Client client = new HelloWorldService.Client(
protocol);
tTransport.open();
String result = client.sayHello(username);
System.out.println("Thrift client result=" + result);
catch (Exception e)
e.printStackTrace();
public static void main(String[] args)
HelloWorldClient client = new HelloWorldClient();
client.startClient("chenxiaochan");
项目结构图:
效果图:
1.启动server端
2.启动client端
dubbo和Thrift对比:
相同点:
dubbo和thrift都是RPC,都有api接口,生产者和消费者.
不同点:
thrift是可支持各种语言,dubbo是针对java语言的.以上是关于Java Thrift服务器和客户端创建的主要内容,如果未能解决你的问题,请参考以下文章
Thrift介绍以及Java中使用Thrift实现RPC示例