thirft 生成各种语言远程调用接口

简介: 首先先安装好 thirft   1、添加依赖 jar <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.8.0</version>&

首先先安装好 thirft  

1、添加依赖 jar

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.8.0</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</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 + 多线程处理)服务。

[java]  view plain  copy
  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.             //使用高密度二进制协议  
  13.             TProtocolFactory proFactory = new TCompactProtocol.Factory();  
  14.               
  15.             //设置处理器HelloImpl  
  16.             TProcessor processor = new Hello.Processor(new HelloImpl());  
  17.               
  18.             //创建服务器  
  19.             TServer server = new TThreadPoolServer(  
  20.                     new Args(serverTransport)  
  21.                     .protocolFactory(proFactory)  
  22.                     .processor(processor)  
  23.                 );  
  24.               
  25.             System.out.println("Start server on port 7911...");  
  26.             server.serve();  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  



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

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



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

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

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


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


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

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



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

[java]  view plain  copy
  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.         //使用高密度二进制协议  
  7.         TProtocol protocol = new TCompactProtocol(transport);  
  8.           
  9.         //创建Client  
  10.         Hello.Client client = new Hello.Client(protocol);  
  11.           
  12.         long start = System.currentTimeMillis();  
  13.         for(int i=0; i<10000; i++){  
  14.             client.helloBoolean(false);  
  15.             client.helloInt(111);  
  16.             client.helloNull();  
  17.             client.helloString("360buy");  
  18.             client.helloVoid();  
  19.         }  
  20.         System.out.println("耗时:" + (System.currentTimeMillis() - start));  
  21.           
  22.         //关闭资源  
  23.         transport.close();  
  24.     }  



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

客户端异步调用

[java]  view plain  copy
  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(falsenew 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(falsenew AsyncMethodCallback<Hello.AsyncClient.helloBoolean_call>() {  
  22.                             public void onError(Exception exception) {  
  23.                                 System.out.println("错误2: " + System.currentTimeMillis());  
  24.                             }  
  25.                               
  26.                             public void onComplete(helloBoolean_call response) {  
  27.                                 System.out.println("完成2: " + System.currentTimeMillis());  
  28.                                 transport.close();  
  29.                             }  
  30.                         });  
  31.                     } catch (TException e) {  
  32.                         e.printStackTrace();  
  33.                     }  
  34.                 }  
  35.             });  
  36.             System.out.println("结束:" + System.currentTimeMillis());  
  37.             Thread.sleep(5000);  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  


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

使用SSL的服务端:



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



目录
相关文章
|
自然语言处理 JavaScript 算法
【插件】IDEA这款插件,爱到无法自拔
本文介绍了阿里云「通义灵码」这一强大IDEA插件,它不仅能够智能生成代码、解答研发问题,还支持多种编程语言和编辑器。文章详细展示了如何安装使用该插件,并通过多个实际案例说明其在代码解释、优化、生成注释及单元测试等方面的应用,助力开发者提高效率。强烈推荐尝试!
364 1
【插件】IDEA这款插件,爱到无法自拔
从 Angular 中的 URL 获取查询参数
本文介绍了如何从 Angular 中的 URL 获取查询参数。 通过注入ActivatedRoute的实例,可以订阅各种可观察对象,包括queryParams和params observable。以下是范例: import { ActivatedRoute } from '@angular/rou...
1694 0
|
数据采集 缓存 前端开发
前端之SEO搜索引擎优化
前端之SEO搜索引擎优化
328 0
|
JavaScript
《Vue3实战》 第十章 Element plus指南
《Vue3实战》 第十章 Element plus指南
305 0
|
数据安全/隐私保护 UED 索引
大文件上传和优化
最近项目里面有一些视频处理的功能,大概流程就是后端拿到文件以后,使用FFmpeg等底层命令进行去水印,裁切等功能,虽然现在是短视频的年代,但是依然会出现一些高分辨率,高时长的大文件视频,这时候因为一些原因,如网络等,失败率会陡增。
|
小程序 JavaScript
小程序云函数调用http或https请求外部数据
小程序云函数调用http或https请求外部数据
787 0
|
并行计算 Java C#
【AXI】解读AXI协议原子化访问
【AXI】解读AXI协议原子化访问
【AXI】解读AXI协议原子化访问
|
缓存 资源调度 算法
包管理工具Yarn的使用和命令总结
包管理工具Yarn的使用和命令总结
541 0
包管理工具Yarn的使用和命令总结
|
Java Spring
影响spring 事务失效的写法(下)
这篇主要介绍一些其他问题
210 1
|
安全 Apache 流计算
Apache Flink 1.14.4 Release Announcement
这个版本修复了 51 个 bug 和漏洞,并对 Flink 1.14 进行了小的改进。下面是所有 bug 修复和改进的列表(不包括对构建基础结构和构建稳定性的改进)。有关所有更改的完整列表,请参阅: JIRA 我们强烈建议所有用户升级到 Flink 1.14.4。