Thrift 半同步半异步的服务模型-THsHaServer(异步调用客户端)

 

半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。

服务端HelloTHsHaServer.java:

 

[java]  view plain  copy
 
 print ?
  1. package cn.slimsmart.thrift.demo.helloworld;  
  2.   
  3. import org.apache.thrift.TException;  
  4. import org.apache.thrift.TProcessor;  
  5. import org.apache.thrift.protocol.TBinaryProtocol;  
  6. import org.apache.thrift.server.THsHaServer;  
  7. import org.apache.thrift.server.TServer;  
  8. import org.apache.thrift.transport.TFramedTransport;  
  9. import org.apache.thrift.transport.TNonblockingServerSocket;  
  10.   
  11. /** 
  12.  * 注册服务端 半同步半异步的服务端模型,需要指定为: TFramedTransport 数据传输的方式。 THsHaServer 
  13.  * 非阻塞 
  14.  */  
  15. public class HelloTHsHaServer {  
  16.     // 注册端口  
  17.     public static final int SERVER_PORT = 8080;  
  18.   
  19.     public static void main(String[] args) throws TException {  
  20.         TProcessor tprocessor = new HelloWorld.Processor(new HelloWorldImpl());  
  21.         // 传输通道 - 非阻塞方式    
  22.         TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);  
  23.         //半同步半异步  
  24.         THsHaServer.Args tArgs = new THsHaServer.Args(serverTransport);  
  25.         tArgs.processor(tprocessor);  
  26.         tArgs.transportFactory(new TFramedTransport.Factory());  
  27.         //二进制协议  
  28.         tArgs.protocolFactory(new TBinaryProtocol.Factory());  
  29.         // 半同步半异步的服务模型  
  30.         TServer server = new THsHaServer(tArgs);  
  31.         System.out.println("HelloTHsHaServer start....");  
  32.         server.serve(); // 启动服务  
  33.     }  
  34. }  

客户端HelloAsyncClient.java

 

 

[java]  view plain  copy
 
 print ?
  1. package cn.slimsmart.thrift.demo.helloworld;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.concurrent.CountDownLatch;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.apache.thrift.TException;  
  8. import org.apache.thrift.async.AsyncMethodCallback;  
  9. import org.apache.thrift.async.TAsyncClientManager;  
  10. import org.apache.thrift.protocol.TBinaryProtocol;  
  11. import org.apache.thrift.protocol.TProtocolFactory;  
  12. import org.apache.thrift.transport.TNonblockingSocket;  
  13. import org.apache.thrift.transport.TNonblockingTransport;  
  14.   
  15. import cn.slimsmart.thrift.demo.helloworld.HelloWorld.AsyncClient.sayHello_call;  
  16.   
  17. /** 
  18.  * 客户端异步调用,服务端需使用TNonblockingServer ,THsHaServer 
  19.  */  
  20. public class HelloAsyncClient {  
  21.     public static final String SERVER_IP = "127.0.0.1";  
  22.     public static final int SERVER_PORT = 8080;  
  23.     public static final int TIMEOUT = 30000;  
  24.   
  25.     public static void main(String[] args) throws TException, IOException, InterruptedException {  
  26.         //异步调用管理器  
  27.         TAsyncClientManager clientManager = new TAsyncClientManager();  
  28.         //设置传输通道,调用非阻塞IO  
  29.         TNonblockingTransport transport = new TNonblockingSocket(SERVER_IP, SERVER_PORT, TIMEOUT);  
  30.         // 协议要和服务端一致  
  31.         //TProtocolFactory tprotocol = new TCompactProtocol.Factory();  
  32.         TProtocolFactory tprotocol = new TBinaryProtocol.Factory();  
  33.           
  34.         HelloWorld.AsyncClient asyncClient = new HelloWorld.AsyncClient(tprotocol, clientManager, transport);  
  35.         CountDownLatch latch = new CountDownLatch(1);  
  36.         AsynCallback callBack = new AsynCallback(latch);  
  37.         System.out.println("call method sayHello start ...");  
  38.         // 调用服务  
  39.         asyncClient.sayHello("jack", callBack);  
  40.         System.out.println("call method sayHello .... end");  
  41.         //等待完成异步调用  
  42.         boolean wait = latch.await(30, TimeUnit.SECONDS);  
  43.         System.out.println("latch.await =:" + wait);  
  44.     }  
  45. }  
  46. class AsynCallback implements AsyncMethodCallback {  
  47.     private CountDownLatch latch;  
  48.   
  49.     public AsynCallback(CountDownLatch latch) {  
  50.         this.latch = latch;  
  51.     }  
  52.   
  53.     @Override  
  54.     public void onComplete(sayHello_call response) {  
  55.         System.out.println("onComplete");  
  56.         try {  
  57.             System.out.println("AsynCall result :" + response.getResult().toString());  
  58.         } catch (TException e) {  
  59.             e.printStackTrace();  
  60.         } catch (Exception e) {  
  61.             e.printStackTrace();  
  62.         } finally {  
  63.             latch.countDown();  
  64.         }  
  65.     }  
  66.     @Override  
  67.     public void onError(Exception exception) {  
  68.         System.out.println("onError :" + exception.getMessage());  
  69.         latch.countDown();  
  70.     }  
  71. }  

 

使用SSL加密协议:

 

[java]  view plain  copy
 
 print ?
  1. //SSL服务端  
  2.         TSSLTransportParameters parameters = new TSSLTransportParameters();  
  3.         //keystore文件  密码  
  4.         parameters.setKeyStore("../../.keystore""thrift");  
  5.         TServerTransport serverTransport = TSSLTransportFactory.getServerSocket(80803000null, parameters);  
  6.           
  7.         //SSL客户端  
  8.         TSSLTransportParameters parameters = new TSSLTransportParameters();  
  9.         parameters.setTrustStore("../../.trustore""thrift""SunX509""JKS");  
  10.         TTransport tTransport = TSSLTransportFactory.getClientSocket("127.0.0.1"80803000, parameters);  

 

http://blog.csdn.net/zhu_tianwei/article/details/44002943

 

你可能感兴趣的:(技术总结,RPC框架)