1、Hello.thrift文件内容
namespace java com.seasy.thrift struct Message { 1: i32 type; 2: binary data; } struct Response { 1: i32 code; 2: string message; } service Hello{ string helloString(1:string param) i32 helloInt(1:i32 param) bool helloBoolean(1:bool param) void helloVoid() Response sendMessage(1:Message message) }
2、堵塞式线程池服务模型
server端:
TServerSocket serverTransport = new TServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT);
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);
tArgs.requestTimeoutUnit(TimeUnit.MILLISECONDS);
tArgs.requestTimeout(5000);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
tArgs.processor(processor);
TServer server = new TThreadPoolServer(tArgs);
server.serve();
client端:
TTransport transport = new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
client.sendMessage(new Message(1, data));
3、非堵塞式多线程服务模型
server端:
TNonblockingServerTransport serverTransport =
new TNonblockingServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT);
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
//TNonblockingServer.Args args = new TNonblockingServer.Args(serverTransport);
TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(serverTransport);
args.processor(processor);
args.transportFactory(new TFramedTransport.Factory());
args.protocolFactory(new TCompactProtocol.Factory());
//server = new TNonblockingServer(args);
server = new TThreadedSelectorServer(args);
server.serve();
client端:
TTransport transport = new TFramedTransport(new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT));
transport.open();
TProtocol protocol = new TCompactProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
client.sendMessage(new Message(1, data));
异步client端:
TAsyncClientManager asyncClientManager = new TAsyncClientManager();
final TNonblockingTransport nonblockingTransport = new TNonblockingSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT);
TProtocolFactory protocolFactory = new TCompactProtocol.Factory();
final Hello.AsyncClient client = new Hello.AsyncClient(protocolFactory, asyncClientManager, nonblockingTransport);
client.helloBoolean(true, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
@Override
public void onComplete(helloBoolean_call response) {
try {
System.out.println(response.getResult());
} catch (TException e) {
e.printStackTrace();
}
}
@Override
public void onError(Exception ex) {
ex.printStackTrace();
}
});
4、通讯层采用SSL安全认证
server端:
TSSLTransportParameters params = new TSSLTransportParameters();
params.setKeyStore("server.jks", "123456", "SunX509", "JKS");
TServerTransport serverTransport =
TSSLTransportFactory.getServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT, null, params);
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);
tArgs.processor(processor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
server = new TThreadPoolServer(tArgs);
server.serve();
client端:
TSSLTransportParameters params = new TSSLTransportParameters();
params.setTrustStore("CA.jks", "123456", "SunX509", "JKS");
transport = TSSLTransportFactory
.getClientSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT, params);
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
client.sendMessage(new Message(1, data));
5、多处理器服务模型
server端:
TMultiplexedProcessor processor = new TMultiplexedProcessor();
processor.registerProcessor("helloService", new Hello.Processor<Hello.Iface>(new HelloServiceImpl()));
TServerSocket serverTransport = new TServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT);
TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);
tArgs.requestTimeoutUnit(TimeUnit.MILLISECONDS);
tArgs.requestTimeout(5000);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
tArgs.processor(processor);
TServer server = new TThreadPoolServer(tArgs);
server.serve();
client端:
TTransport transport = new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "helloService");
Hello.Client client = new Hello.Client(multiplexedProtocol);
ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
client.sendMessage(new Message(1, data));
6、半同步半异步服务模型
server端:
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
TNonblockingServerSocket socketTransport = new TNonblockingServerSocket(Configuration.SERVER_PORT);
THsHaServer.Args thhsArgs = new THsHaServer.Args(socketTransport);
thhsArgs.processor(processor);
thhsArgs.transportFactory(new TFramedTransport.Factory());
thhsArgs.protocolFactory(new TCompactProtocol.Factory());
TServer server = new THsHaServer(thhsArgs);
server.serve();
client端:
TTransport transport = new TFramedTransport(new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT));
transport.open();
TProtocol protocol = new TCompactProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
client.sendMessage(new Message(1, data));