`
raymond.chen
  • 浏览: 1418822 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Thrift代码范例

 
阅读更多

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端:

TNonblockingServerSocket serverSocket = new TNonblockingServerSocket(8080);

TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());

TNonblockingServer.Args tArgs = new TNonblockingServer.Args(serverSocket);
tArgs.processor(processor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());

TServer tserver = new TNonblockingServer(tArgs);
tserver.serve();

 

    client端:

TTransport transport = new TFramedTransport(new TSocket(host, port));
transport.open();

TProtocol protocol = new TCompactProtocol(transport);
Hello.Client client = new Hello.Client(protocol);

ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
Response response = client.sendMessage(new Message(1, data));
System.out.println(response.getCode() + ", " + response.getMessage());

 

 

    异步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));

 

7、TThreadedSelectorServer使用范例

服务端:

TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();
multiplexedProcessor.registerProcessor("Hello", new Hello.Processor<Hello.Iface>(new HelloServiceImpl()));

TNonblockingServerTransport transport = 
		new TNonblockingServerSocket(Configuration.SERVER_PORT, Configuration.TIMEOUT);

TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(transport);
tArgs.processor(multiplexedProcessor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());
tArgs.selectorThreads(5);
tArgs.workerThreads(50);

TServer server = new TThreadedSelectorServer(tArgs);
server.serve();

 

客户端:

TTransport transport = new TFramedTransport(new TSocket(Configuration.HOST, Configuration.SERVER_PORT, Configuration.TIMEOUT));
transport.open();

TProtocol protocol = new TCompactProtocol(transport);
TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "Hello");

client = new Hello.Client(multiplexedProtocol);

System.out.println(client.helloString("hello string"));

ByteBuffer data = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
Response response = client.sendMessage(new Message(1, data));
System.out.println(response.getCode() + ", " + response.getMessage());

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics