服务端:
客户端
传输协议
创建文件demoHello.thrift
namespace java com.fengtang.thrift.demo //定义生成代码的命名空间,与package相对应。
//代码生成的类名,你的业务逻辑代码需要实现代码生成的HelloWorldService.Iface接口
service HelloWorldService {
//方法名称和方法中的入参
string sayHello(1:string username)
}
使用thrift-0.9.2.exe
生成代码,执行命令
thrift-0.9.2.exe -r -gen java ./demoHello.thrift
生成后的截图
3.将代码拷入IDEA
使用maven解决依赖关系
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
实现接口Iface
package com.fengtang.thrift.demo.service;
import com.fengtang.thrift.demo.HelloWorldService;
import org.apache.thrift.TException;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
*/
public class HelloWorldImpl implements HelloWorldService.Iface {
public HelloWorldImpl() {
}
@Override
public String sayHello(String username) throws TException {
return "Hi," + username + " I‘m the server ...";
}
}
TSimpleServer
服务端和客户端测试TSimpleServer
简单的单线程服务模型,一般用于测试
服务端
package com.fengtang.thrift.demo.server;
import com.fengtang.thrift.demo.HelloWorldService;
import com.fengtang.thrift.demo.service.HelloWorldImpl;
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;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
* TSimpleServer 简单的单线程服务模型
*/
public class TSimpServer {
public static final int SERVER_PORT = 8090;
/**
* 创建TProcessor
* 创建TServerTransport
* 创建TProtocol
* 创建TServer
* 启动Server
*/
public void startServer() {
System.out.println("TSimpleServer start ....");
try {
//创建TProcessor
TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(
new HelloWorldImpl());
//创建TServerTransport
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
//创建TServer
TServer server = new TSimpleServer(tArgs);
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TSimpServer server = new TSimpServer();
server.startServer();
}
}
客户端
package com.fengtang.thrift.demo.client;
import com.fengtang.thrift.demo.HelloWorldService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
*/
public class TSimpleClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;
public static final int TIMEOUT = 30000;
/**
* @param userName
*/
public void startClient(String userName) {
TTransport transport = null;
try {
transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);
transport.open();
long start = System.currentTimeMillis();
String result = client.sayHello(userName);
System.out.println("Total time is " + (System.currentTimeMillis() - start) + " ms ");
System.out.println("Thrift client result : \n" + result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
public static void main(String[] args) {
TSimpleClient client = new TSimpleClient();
client.startClient("Nicholas");
}
}
运行结果截图
TThreadPoolServer
服务端和客户端测试TThreadPoolServer
线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求
服务端
package com.fengtang.thrift.demo.server;
import com.fengtang.thrift.demo.HelloWorldService;
import com.fengtang.thrift.demo.service.HelloWorldImpl;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
* 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
*/
public class TThrPoolServer {
public static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld TThreadPoolServer start ....");
TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(
new HelloWorldImpl());
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(
serverTransport);
ttpsArgs.processor(tprocessor);
ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());
// 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
TServer server = new TThreadPoolServer(ttpsArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TThrPoolServer server = new TThrPoolServer();
server.startServer();
}
}
客户端
package com.fengtang.thrift.demo.client;
import com.fengtang.thrift.demo.HelloWorldService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
*/
public class TThreadPoolClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;
public static final int TIMEOUT = 30000;
/**
* @param userName
*/
public void startClient(String userName) {
TTransport transport = null;
try {
transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);
transport.open();
long start = System.currentTimeMillis();
String result = client.sayHello(userName);
System.out.println("Total time is " + (System.currentTimeMillis() - start) + " ms ");
System.out.println("Thrift client result : \n" + result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
public static void main(String[] args) {
TSimpleClient client = new TSimpleClient();
client.startClient("Nicholas");
}
}
运行截图
TNonblockingServer
服务端和客户端测试TNonblockingServer
使用非阻塞式IO,服务端和客户端需要指定 TFramedTransport 数据传输的方式。
服务端
package com.fengtang.thrift.demo.server;
import com.fengtang.thrift.demo.HelloWorldService;
import com.fengtang.thrift.demo.service.HelloWorldImpl;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
* 使用非阻塞式IO,服务端和客户端需要指定 TFramedTransport 数据传输的方式。
*/
public class TNonbloServer {
public static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld TNonblockingServer start ....");
TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(
new HelloWorldImpl());
TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(
SERVER_PORT);
TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(
tnbSocketTransport);
tnbArgs.processor(tprocessor);
tnbArgs.transportFactory(new TFramedTransport.Factory());
tnbArgs.protocolFactory(new TCompactProtocol.Factory());
// 使用非阻塞式IO,服务端和客户端需要指定TFramedTransport数据传输的方式
TServer server = new TNonblockingServer(tnbArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
public static void main(String[] args) {
TNonbloServer server = new TNonbloServer();
server.startServer();
}
}
客户端
package com.fengtang.thrift.demo.client;
import com.fengtang.thrift.demo.HelloWorldService;
import org.apache.thrift.TException;
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 org.apache.thrift.transport.TTransportException;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
*/
public class TNonblockingClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;
public static final int TIMEOUT = 30000;
/**
* @param userName
*/
public void startClient(String userName) {
TTransport transport = null;
try {
transport = new TFramedTransport(new TSocket(SERVER_IP,
SERVER_PORT, TIMEOUT));
// 协议要和服务端一致
TProtocol protocol = new TCompactProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(
protocol);
transport.open();
String result = client.sayHello(userName);
System.out.println("Thrify client result =: " + result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
public static void main(String[] args) {
TNonblockingClient client = new TNonblockingClient();
client.startClient("Nicholas");
}
}
运行截图
THsHaServer
服务端和客户端测试THsHaServer
半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。
服务端
package com.fengtang.thrift.demo.server;
import com.fengtang.thrift.demo.HelloWorldService;
import com.fengtang.thrift.demo.service.HelloWorldImpl;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
* 半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。
*/
public class ThshaServer {
public static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld THsHaServer start ....");
TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(
new HelloWorldImpl());
TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(
SERVER_PORT);
THsHaServer.Args thhsArgs = new THsHaServer.Args(tnbSocketTransport);
thhsArgs.processor(tprocessor);
thhsArgs.transportFactory(new TFramedTransport.Factory());
thhsArgs.protocolFactory(new TBinaryProtocol.Factory());
//半同步半异步的服务模型
TServer server = new THsHaServer(thhsArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
public static void main(String[] args) {
ThshaServer server = new ThshaServer();
server.startServer();
}
}
客户端
package com.fengtang.thrift.demo.client;
import com.fengtang.thrift.demo.HelloWorldService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
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 org.apache.thrift.transport.TTransportException;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
*/
public class THsHaClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;
public static final int TIMEOUT = 30000;
/**
* @param userName
*/
public void startClient(String userName) {
TTransport transport = null;
try {
transport = new TFramedTransport(new TSocket(SERVER_IP,
SERVER_PORT, TIMEOUT));
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);
transport.open();
String result = client.sayHello(userName);
System.out.println("Thrify client result =: " + result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
public static void main(String[] args) {
THsHaClient client = new THsHaClient();
client.startClient("Nicholas");
}
}
运行截图
服务端
package com.fengtang.thrift.demo.server;
import com.fengtang.thrift.demo.HelloWorldService;
import com.fengtang.thrift.demo.service.HelloWorldImpl;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
/**
* Create by fengtang
* 2015/8/14 0014
* Thrift
*/
public class AsynchronousServer {
public static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld TNonblockingServer start ....");
TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(
SERVER_PORT);
TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(
tnbSocketTransport);
tnbArgs.processor(tprocessor);
tnbArgs.transportFactory(new TFramedTransport.Factory());
tnbArgs.protocolFactory(new TCompactProtocol.Factory());
// 使用非阻塞式IO,服务端和客户端需要指定TFramedTransport数据传输的方式
TServer server = new TNonblockingServer(tnbArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
public static void main(String[] args) {
AsynchronousServer server = new AsynchronousServer();
server.startServer();
}
}
客户端
package com.fengtang.thrift.demo.client;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.fengtang.thrift.demo.HelloWorldService;
import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;
import com.fengtang.thrift.demo.HelloWorldService.AsyncClient.sayHello_call;
/**
* blog http://www.micmiu.com
*
* @author Michael
*/
public class AsynchronousClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;
public static final int TIMEOUT = 30000;
/**
* @param userName
*/
public void startClient(String userName) {
try {
TAsyncClientManager clientManager = new TAsyncClientManager();
TNonblockingTransport transport = new TNonblockingSocket(SERVER_IP,
SERVER_PORT, TIMEOUT);
TProtocolFactory tprotocol = new TCompactProtocol.Factory();
HelloWorldService.AsyncClient asyncClient = new HelloWorldService.AsyncClient(
tprotocol, clientManager, transport);
System.out.println("Client start .....");
CountDownLatch latch = new CountDownLatch(1);
AsynCallback callBack = new AsynCallback(latch);
System.out.println("call method sayHello start ...");
asyncClient.sayHello(userName, callBack);
System.out.println("call method sayHello .... end");
boolean wait = latch.await(30, TimeUnit.SECONDS);
System.out.println("latch.await =:" + wait);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("startClient end.");
}
public class AsynCallback implements AsyncMethodCallback<sayHello_call> {
private CountDownLatch latch;
public AsynCallback(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void onComplete(sayHello_call response) {
System.out.println("onComplete");
try {
System.out.println("AsynCall result =:"
+ response.getResult().toString());
} catch (TException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
}
@Override
public void onError(Exception exception) {
System.out.println("onError :" + exception.getMessage());
latch.countDown();
}
}
/**
* @param args
*/
public static void main(String[] args) {
AsynchronousClient client = new AsynchronousClient();
client.startClient("Michael");
}
}
运行截图
版权声明:欢迎交流
原文:http://blog.csdn.net/weiyongxuan/article/details/47661271