thrift服务端和客户端实现:Nifty

jopen 11年前

Nifty是非死book公司开源的,基于netty的thrift服务端和客户端实现。

然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。

Thrift是一个可伸缩的跨语言的服务开发框架。It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

著名的 Key-Value 存储服务器 Cassandra 就是使用 Thrift 作为其客户端API的。

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();          }      });  }

项目主页:http://www.open-open.com/lib/view/home/1375190535577