Thrift交流(二)thrift服务端和客户端实现 Nifty

来源:互联网 发布:开展网络思想政治教育 编辑:程序博客网 时间:2024/05/21 13:53
Nifty是facebook公司开源的,基于netty的thrift服务端和客户端实现。然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。

https://github.com/facebook/nifty

Nifty简单例子

1)环境搭建

pom文件

[html] view plain copy
  1. <dependency>  
  2.             <groupId>com.facebook.nifty</groupId>  
  3.             <artifactId>nifty-core</artifactId>  
  4.             <version>0.9.0</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.apache.thrift</groupId>  
  8.             <artifactId>libthrift</artifactId>  
  9.             <version>0.9.1</version>  
  10.         </dependency>  

Thrift文件

[java] view plain copy
  1. namespace java example  // defines the namespace    
  2.      
  3. typedef i32 int  //typedefs to get convenient names for your types   
  4.      
  5. service ThriftTestService {    
  6.     string test(1:string name),//delay 3s  
  7. }  

Server文件

[java] view plain copy
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3.   
  4. import org.apache.thrift.TProcessor;  
  5.   
  6. import com.facebook.nifty.core.NettyServerTransport;  
  7. import com.facebook.nifty.core.ThriftServerDef;  
  8. import com.facebook.nifty.core.ThriftServerDefBuilder;  
  9.   
  10. import example.ThriftTestService;  
  11. import example.ThriftTestServiceImpl;  
  12.   
  13. public class Server {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         // Create the handler  
  20.                 ThriftTestService.Iface serviceInterface = new ThriftTestServiceImpl();  
  21.   
  22.                 // Create the processor  
  23.                 TProcessor processor = new ThriftTestService.Processor<ThriftTestService.Iface>(  
  24.                         serviceInterface);  
  25.   
  26.                 // Build the server definition  
  27.                 ThriftServerDef serverDef = new ThriftServerDefBuilder().listen(7790)  
  28.                         .withProcessor(processor).build();  
  29.   
  30.                 // Create the server transport  
  31.                 final NettyServerTransport server = new NettyServerTransport(serverDef);  
  32.   
  33.                 // Start the server  
  34.                 server.start();  
  35.   
  36.                 // Arrange to stop the server at shutdown  
  37.                 Runtime.getRuntime().addShutdownHook(new Thread() {  
  38.                     @Override  
  39.                     public void run() {  
  40.                         try {  
  41.                             server.stop();  
  42.                         } catch (InterruptedException e) {  
  43.                             Thread.currentThread().interrupt();  
  44.                         }  
  45.                     }  
  46.                 });  
  47.   
  48.                 System.out.println("服务器启动成功...");  
  49.   
  50.     }  
  51.   
  52. }  
Client

[java] view plain copy
  1. import org.apache.thrift.protocol.TBinaryProtocol;  
  2. import org.apache.thrift.protocol.TProtocol;  
  3. import org.apache.thrift.transport.TSocket;  
  4. import org.apache.thrift.transport.TTransport;  
  5.   
  6. import example.ThriftTestService;  
  7.   
  8. public class Client {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) throws Exception {  
  14.         TTransport transport = new TSocket("localhost"7790);  
  15.         transport.open();  
  16.         TProtocol protocol = new TBinaryProtocol(transport);  
  17.         ThriftTestService.Client client = new ThriftTestService.Client(protocol);  
  18.         System.out.println(client.test("aa"));  
  19.         transport.close();  
  20.     }  
  21.