当前位置:   article > 正文

Apache Thrift - java开发详解_java apache thrift

java apache thrift

1、添加依赖 jar

  1. <dependency>
  2. <groupId>org.apache.thrift</groupId>
  3. <artifactId>libthrift</artifactId>
  4. <version>0.8.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.slf4j</groupId>
  8. <artifactId>slf4j-log4j12</artifactId>
  9. <version>1.6.1</version>
  10. </dependency>

2、编写IDL文件 Hello.thrift

namespace java service.demo
service Hello {
    string helloString(1:string para)
    i32 helloInt(1:i32 para)
    bool helloBoolean(1:bool para)
    void helloVoid()
    string helloNull()
}


3、生成代码

thrift -o <output directory> -gen java Hello.thrift
生成代码缩略图:



4、编写实现类、实现Hello.Iface:

缩略图:



5、编写服务端,发布(阻塞式IO + 多线程处理)服务。

  1. /**
  2. * 阻塞式、多线程处理
  3. *
  4. * @param args
  5. */
  6. @SuppressWarnings({ "unchecked", "rawtypes" })
  7. public static void main(String[] args) {
  8. try {
  9. //设置传输通道,普通通道
  10. TServerTransport serverTransport = new TServerSocket(7911);
  11. //使用高密度二进制协议
  12. TProtocolFactory proFactory = new TCompactProtocol.Factory();
  13. //设置处理器HelloImpl
  14. TProcessor processor = new Hello.Processor(new HelloImpl());
  15. //创建服务器
  16. TServer server = new TThreadPoolServer(
  17. new Args(serverTransport)
  18. .protocolFactory(proFactory)
  19. .processor(processor)
  20. );
  21. System.out.println("Start server on port 7911...");
  22. server.serve();
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }



6、编写客户端,调用(阻塞式IO + 多线程处理)服务:

  1. public static void main(String[] args) throws Exception {
  2. // 设置传输通道 - 普通IO流通道
  3. TTransport transport = new TSocket("localhost", 7911);
  4. transport.open();
  5. //使用高密度二进制协议
  6. TProtocol protocol = new TCompactProtocol(transport);
  7. //创建Client
  8. Hello.Client client = new Hello.Client(protocol);
  9. long start = System.currentTimeMillis();
  10. for(int i=0; i<10000; i++){
  11. client.helloBoolean(false);
  12. client.helloInt(111);
  13. client.helloNull();
  14. client.helloString("dongjian");
  15. client.helloVoid();
  16. }
  17. System.out.println("耗时:" + (System.currentTimeMillis() - start));
  18. //关闭资源
  19. transport.close();
  20. }



现在已完成整个开发过程,超级无敌简单。

其中服务端使用的协议需要与客户端保持一致

-------------------------------------------------------------------------------------------------------------------


上面展示了普通且常用的服务端和客户端,下面请看非阻塞IO,即java中的NIO:


基于非阻塞IO(NIO)的服务端

  1. public static void main(String[] args) {
  2. try {
  3. //传输通道 - 非阻塞方式
  4. TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
  5. //异步IO,需要使用TFramedTransport,它将分块缓存读取。
  6. TTransportFactory transportFactory = new TFramedTransport.Factory();
  7. //使用高密度二进制协议
  8. TProtocolFactory proFactory = new TCompactProtocol.Factory();
  9. //设置处理器 HelloImpl
  10. TProcessor processor = new Hello.Processor(new HelloImpl());
  11. //创建服务器
  12. TServer server = new TThreadedSelectorServer(
  13. new Args(serverTransport)
  14. .protocolFactory(proFactory)
  15. .transportFactory(transportFactory)
  16. .processor(processor)
  17. );
  18. System.out.println("Start server on port 7911...");
  19. server.serve();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }



调用非阻塞IO(NIO)服务的客户端

  1. public static void main(String[] args) throws Exception {
  2. //设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
  3. TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));
  4. transport.open();
  5. //使用高密度二进制协议
  6. TProtocol protocol = new TCompactProtocol(transport);
  7. //创建Client
  8. Hello.Client client = new Hello.Client(protocol);
  9. long start = System.currentTimeMillis();
  10. for(int i=0; i<10000; i++){
  11. client.helloBoolean(false);
  12. client.helloInt(111);
  13. client.helloNull();
  14. client.helloString("360buy");
  15. client.helloVoid();
  16. }
  17. System.out.println("耗时:" + (System.currentTimeMillis() - start));
  18. //关闭资源
  19. transport.close();
  20. }



-----------------------------------------------------------------------------------------------------------------------------------

客户端异步调用

  1. /** 调用[非阻塞IO]服务,异步 */
  2. public static void main(String[] args) {
  3. try {
  4. //异步调用管理器
  5. TAsyncClientManager clientManager = new TAsyncClientManager();
  6. //设置传输通道,调用非阻塞IO。
  7. final TNonblockingTransport transport = new TNonblockingSocket("localhost", 7911);
  8. //设置协议
  9. TProtocolFactory protocol = new TCompactProtocol.Factory();
  10. //创建Client
  11. final Hello.AsyncClient client = new Hello.AsyncClient(protocol, clientManager, transport);
  12. // 调用服务
  13. System.out.println("开始:" + System.currentTimeMillis());
  14. client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
  15. public void onError(Exception exception) {
  16. System.out.println("错误1: " + System.currentTimeMillis());
  17. }
  18. public void onComplete(helloBoolean_call response) {
  19. System.out.println("完成1: " + System.currentTimeMillis());
  20. try {
  21. client.helloBoolean(false, new AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {
  22. public void onError(Exception exception) {
  23. System.out.println("错误2: " + System.currentTimeMillis());
  24. }
  25. public void onComplete(helloBoolean_call response) {
  26. System.out.println("完成2: " + System.currentTimeMillis());
  27. transport.close();
  28. }
  29. });
  30. } catch (TException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. });
  35. System.out.println("结束:" + System.currentTimeMillis());
  36. Thread.sleep(5000);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. }


-----------------------------------------------------------------------------------------------------------------------------------

使用SSL的服务端:



调用基于SSL服务端的客户端:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58542
推荐阅读
相关标签
  

闽ICP备14008679号