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服务端的客户端:



目录
相关文章
|
2月前
|
Java Apache
远程调用工具HttpClient工具类封装
java远程调用工具HttpClient工具类类封装
|
6月前
|
Java Maven 微服务
【Java用法】微服务之间的相互调用方式之一,通过FeignClient客户端调用其他微服务的方法
【Java用法】微服务之间的相互调用方式之一,通过FeignClient客户端调用其他微服务的方法
81 0
|
3月前
|
Go
Go语言RPC实战:打造自己的远程调用服务
Go语言RPC实战:打造自己的远程调用服务
33 0
|
5月前
|
JSON 监控 测试技术
RESTful API设计与实现在员工行为监控系统中的数据交互接口(Go语言)
在现代企业环境中,对员工行为进行监控已经成为确保组织安全和合规性的重要手段。为了提高监控系统的效率和可靠性,自动化测试在系统开发过程中发挥着关键作用。本文将探讨在员工行为监控系统开发中采用JUnit进行自动化测试的实际应用,并通过代码示例演示其工作原理。
186 1
|
9月前
|
存储 搜索推荐 API
如何设计 RPC 接口
如何设计 RPC 接口
175 0
|
10月前
|
网络协议 Dubbo Java
【远程调用框架概述 一】基于HTTP和RPC的远程调用方式
【远程调用框架概述 一】基于HTTP和RPC的远程调用方式
250 0
|
10月前
|
SQL 负载均衡 Java
怎么设计一个高质量的接口API设计
什么是幂等性?对于同一笔业务交易,不管调用多少次,只会成功处理一次。二、幂等性设计我们转账业务为例,来说明一下这个问题,转账接口一定要做到幂等性,否则会出现重复转账的问题。调用转账接口从A中转100元资金给B,参数中会携带业务流水号biz_no和源账户A,目的账户B,和转账金额100,业务流水号biz_no是唯一的。转账接口实现有以下实现方式。
|
11月前
|
消息中间件 XML JSON
一文就读懂RPC远程调用核心原理
rpc的全称是Remote Procedure Call,即远程过程调用,是分布式系统的常用通信方法。 Remote,简单来说的话就是两个不同的服务之间,两个服务肯定是两个不同的进程。因此,我们就从跨进程进行访问的角度去理解就行了。 Procedure,意思是一串可执行的代码,我们写Java的方法,就是一段课程行的代码。 Call,即调用,调用的就是跨了进程的方法。
258 0
一文就读懂RPC远程调用核心原理
|
12月前
|
测试技术 Python
07 WebSocket接口:如何测试一个完全陌生的协议接口?
07 WebSocket接口:如何测试一个完全陌生的协议接口?
|
API
如何优雅的使用Fegin去构造通用的服务调用的API
如何优雅的使用Fegin去构造通用的服务调用的API
100 0