Thrift 各种服务端和客户端实现

时间:2021-05-25 14:57:30
//简单的单线程服务模型
import org.apache.thrift.TException;
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;
import org.apache.thrift.transport.TTransportException;

public class HelloTSimpleServer {

public static final int SERVER_PORT = 8080;

public void startServer() throws TTransportException {
TProcessor tprocessor = new Hello.Processor<>(new HelloImpl());
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tArgs);
System.out.println("init...");
server.serve();
}

public static void main(String[] args) throws TException {
HelloTSimpleServer hss = new HelloTSimpleServer();
hss.startServer();
}

}
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;public class HelloClient {	public static final String SERVER_IP = "127.0.0.1";	public static final int SERVER_PORT = 8080;	public static final int TIMEOUT = 30000;	public static void main(String[] args) throws TException {		// 设置传输通道		TTransport transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);		// 协议要和服务端一致		// 使用二进制协议		TProtocol protocol = new TBinaryProtocol(transport);		// 创建Client		Hello.Client client = new Hello.Client(protocol);		transport.open();		String result = client.helloString("111");		System.out.println("result : " + result);		// 关闭资源		transport.close();	}}

//线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

public class HelloTThreadPoolServer {

public static final int SERVER_PORT = 8080;

public void startServer() throws TTransportException {
TProcessor tprocessor = new Hello.Processor<>(new HelloImpl());
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(
serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());

TThreadPoolServer ttps = new TThreadPoolServer(tArgs);
System.out.println("init...");
ttps.serve();
}

public static void main(String[] args) throws TException {
HelloTThreadPoolServer hss = new HelloTThreadPoolServer();
hss.startServer();
}

}
客户端不变

//使用非阻塞式IO,服务端和客户端需要指定 TFramedTransport 数据传输的方式。
import org.apache.thrift.TException;
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;
import org.apache.thrift.transport.TTransportException;

public class HelloTNonblockingServer {

public static final int SERVER_PORT = 8080;

public void startServer() throws TTransportException {
TProcessor tprocessor = new Hello.Processor<>(new HelloImpl());
TNonblockingServerSocket tnbServerTransport = new TNonblockingServerSocket(
SERVER_PORT);
TNonblockingServer.Args tArgs = new TNonblockingServer.Args(
tnbServerTransport);
tArgs.processor(tprocessor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());

TServer ttps = new TNonblockingServer(tArgs);
System.out.println("init...");
ttps.serve();
}

public static void main(String[] args) throws TException {
HelloTNonblockingServer hss = new HelloTNonblockingServer();
hss.startServer();
}

}
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;public class HelloClient {	public static final String SERVER_IP = "127.0.0.1";	public static final int SERVER_PORT = 8080;	public static final int TIMEOUT = 30000;	public static void main(String[] args) throws TException {		// 设置传输通道		TTransport transport = new TFramedTransport(new TSocket(SERVER_IP,				SERVER_PORT, TIMEOUT));		// 协议要和服务端一致		// 使用二进制协议		TProtocol protocol = new TCompactProtocol(transport);		// 创建Client		Hello.Client client = new Hello.Client(protocol);		transport.open();		String result = client.helloString("111");		System.out.println("result : " + result);		// 关闭资源		transport.close();	}}

//半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。
import org.apache.thrift.TException;
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;
import org.apache.thrift.transport.TTransportException;

public class HelloTHsHaServer {

public static final int SERVER_PORT = 8080;

public void startServer() throws TTransportException {
TProcessor tprocessor = new Hello.Processor<>(new HelloImpl());
TNonblockingServerSocket tnbServerTransport = new TNonblockingServerSocket(
SERVER_PORT);
THsHaServer.Args tArgs = new THsHaServer.Args(tnbServerTransport);
tArgs.processor(tprocessor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TBinaryProtocol.Factory());

TServer ttps = new THsHaServer(tArgs);
System.out.println("init...");
ttps.serve();
}

public static void main(String[] args) throws TException {
HelloTHsHaServer hss = new HelloTHsHaServer();
hss.startServer();
}

}
import org.apache.thrift.TException;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TFramedTransport;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;public class HelloClient {	public static final String SERVER_IP = "127.0.0.1";	public static final int SERVER_PORT = 8080;	public static final int TIMEOUT = 30000;	public static void main(String[] args) throws TException {		// 设置传输通道		TTransport transport = new TFramedTransport(new TSocket(SERVER_IP,				SERVER_PORT, TIMEOUT));		// 协议要和服务端一致		// 使用二进制协议		TProtocol protocol = new TBinaryProtocol(transport);		// 创建Client		Hello.Client client = new Hello.Client(protocol);		transport.open();		String result = client.helloString("111");		System.out.println("result : " + result);		// 关闭资源		transport.close();	}}