半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。
服务端HelloTHsHaServer.java:
- package cn.slimsmart.thrift.demo.helloworld;
- 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;
- /**
- * 注册服务端 半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。 THsHaServer
- * 非阻塞
- */
- public class HelloTHsHaServer {
- // 注册端口
- public static final int SERVER_PORT = 8080;
- public static void main(String[] args) throws TException {
- TProcessor tprocessor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl());
- // 传输通道 - 非阻塞方式
- TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);
- //半同步半异步
- THsHaServer.Args tArgs = new THsHaServer.Args(serverTransport);
- tArgs.processor(tprocessor);
- tArgs.transportFactory(new TFramedTransport.Factory());
- //二进制协议
- tArgs.protocolFactory(new TBinaryProtocol.Factory());
- // 半同步半异步的服务模型
- TServer server = new THsHaServer(tArgs);
- System.out.println("HelloTHsHaServer start....");
- server.serve(); // 启动服务
- }
- }
客户端HelloAsyncClient.java:
- package cn.slimsmart.thrift.demo.helloworld;
- import java.io.IOException;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.TimeUnit;
- import org.apache.thrift.TException;
- import org.apache.thrift.async.AsyncMethodCallback;
- import org.apache.thrift.async.TAsyncClientManager;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TProtocolFactory;
- import org.apache.thrift.transport.TNonblockingSocket;
- import org.apache.thrift.transport.TNonblockingTransport;
- import cn.slimsmart.thrift.demo.helloworld.HelloWorld.AsyncClient.sayHello_call;
- /**
- * 客户端异步调用,服务端需使用TNonblockingServer ,THsHaServer
- */
- public class HelloAsyncClient {
- 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, IOException, InterruptedException {
- //异步调用管理器
- TAsyncClientManager clientManager = new TAsyncClientManager();
- //设置传输通道,调用非阻塞IO
- TNonblockingTransport transport = new TNonblockingSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
- // 协议要和服务端一致
- //TProtocolFactory tprotocol = new TCompactProtocol.Factory();
- TProtocolFactory tprotocol = new TBinaryProtocol.Factory();
- HelloWorld.AsyncClient asyncClient = new HelloWorld.AsyncClient(tprotocol, clientManager, transport);
- CountDownLatch latch = new CountDownLatch(1);
- AsynCallback callBack = new AsynCallback(latch);
- System.out.println("call method sayHello start ...");
- // 调用服务
- asyncClient.sayHello("jack", callBack);
- System.out.println("call method sayHello .... end");
- //等待完成异步调用
- boolean wait = latch.await(30, TimeUnit.SECONDS);
- System.out.println("latch.await =:" + wait);
- }
- }
- 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();
- }
- }
使用SSL加密协议:
- //SSL服务端
- TSSLTransportParameters parameters = new TSSLTransportParameters();
- //keystore文件 密码
- parameters.setKeyStore("../../.keystore", "thrift");
- TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(8080, 3000, null, parameters);
- //SSL客户端
- TSSLTransportParameters parameters = new TSSLTransportParameters();
- parameters.setTrustStore("../../.trustore", "thrift", "SunX509", "JKS");
- TTransport tTransport = TSSLTransportFactory.getClientSocket("127.0.0.1", 8080, 3000, parameters);
http://blog.csdn.net/zhu_tianwei/article/details/44002943