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

转载自 CSDN Arjick


Nifty是facebook公司开源的,基于nettythrift服务端和客户端实现。然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。

https://github.com/facebook/nifty

Nifty简单例子

1)环境搭建

pom文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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.   
  22. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nifty是facebook公司开源的,基于nettythrift服务端客户端实现。 然后使用此包就可以快速发布出基于netty的高效的服务端客户端代码。 示例: public void startServer() { // Create the handler MyService.Iface serviceInterface = new MyServiceHandler(); // Create the processor TProcessor processor = new MyService.Processor<>(serviceInterface); // Build the server definition ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor) .build(); // Create the server transport final NettyServerTransport server = new NettyServerTransport(serverDef, new NettyConfigBuilder(), new DefaultChannelGroup(), new HashedWheelTimer()); // Create netty boss and executor thread pools ExecutorService bossExecutor = Executors.newCachedThreadPool(); ExecutorService workerExecutor = Executors.newCachedThreadPool(); // Start the server server.start(bossExecutor, workerExecutor); // Arrange to stop the server at shutdown Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { server.stop(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }); } Or the same thing using guice: public void startGuiceServer() { final NiftyBootstrap bootstrap = Guice.createInjector( Stage.PRODUCTION, new NiftyModule() { @Override protected void configureNifty() { // Create the handler MyService.Iface serviceInterface = new MyServiceHandler(); // Create the processor TProcessor processor = new MyService.Processor<>(serviceInterface); // Build the server definition ThriftServerDef serverDef = new ThriftServerDefBuilder().withProcessor(processor) .build(); // Bind the definition bind().toInstance(serverDef); } }).getInstance(NiftyBootstrap.class); // Start the server bootstrap.start(); // Arrange to stop the server at shutdown Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { bootstrap.stop(); } }); } 标签:Nifty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值