Thrift Java使用实例(修改版)

2024-06-07 06:08

本文主要是介绍Thrift Java使用实例(修改版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

根据Apache Thrift的官方站点的描述,Thrift是一个:

software framework, for scalable cross-language services development, 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, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
 
安装Thrift比较的烦,但是在Windows下官方编译了一个 thrift.exe,下载就行了。点击一闪即逝 ,木有关系。
拷贝到c:\windows目录下(或者任何目录下),然后就可以在dos环境下使用了

写 Thrift定义文件(.thrift file)

如果你之前有接触过这个东西的话,写定义文件非常的简单。但这里可以参考官方的教程快速开始。

示例定义文件(add.thrift)

namespace java com.samples.thrift.server  // defines the namespace   typedef i32 int  //typedefs to get convenient names for your types  service AdditionService {  // defines the service to add two numbers  int add(1:int n1, 2:int n2), //defines a method  
}

编译Thrift定义文件

下面的命令编译.thrift文件

thrift --gen <language> <Thrift filename>

对于我的例子来讲,命令是:

thrift-0.9.1 -gen java E:\workspace\ThriftTest\src\main\java\add.thrift




在执行完代码后,在gen-java目录下你会发现构建RPC服务器和客户端有用的源代码。创建一个叫做AddtionService.java的java文件。

创建Maven项目

添加
<dependencies><dependency><groupId>org.apache.thrift</groupId><artifactId>libthrift</artifactId><version>0.9.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.5.7</version></dependency></dependencies>

写一个 service handler
Service handler 类必须实现 AdditionService.Iface接口。
示例Service handler(AdditionServiceHandler.java)

package com.samples.thrift.server;import org.apache.thrift.TException;  public class AdditionServiceHandler implements AdditionService.Iface {  public int add(int n1, int n2) throws TException {  return n1 + n2;  }  }

写一个简单的服务器

下面的示例代码是一个简单的Thrift服务器。可以看到下面的代码中有一段是注释了的,可以去掉注释来启用多线程服务器。
示例服务器(MyServer.java)

package com.samples.thrift.server;import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TServerTransport;  
import org.apache.thrift.server.TServer;  
import org.apache.thrift.server.TServer.Args;  
import org.apache.thrift.server.TSimpleServer;  public class MyServer {  public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {  try {  TServerTransport serverTransport = new TServerSocket(4578);  TServer server = new TSimpleServer(  new Args(serverTransport).processor(processor));  // Use this for a multithreaded server  // TServer server = new TThreadPoolServer(new  // TThreadPoolServer.Args(serverTransport).processor(processor));  System.out.println("Starting the simple server...");  server.serve();  } catch (Exception e) {  e.printStackTrace();  }  }  public static void main(String[] args) {  StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));  }  }

写一个客户端

下面的例子是一个使用Java写的客户端短使用AdditionService的服务。

运行服务端代码(MyServer.java)将会看到下面的输出。

然后运行客户端代码(AdditionClient.java),将会看到如下输出。

package com.samples.thrift.server;import org.apache.thrift.TException;  
import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.protocol.TProtocol;  
import org.apache.thrift.transport.TSocket;  
import org.apache.thrift.transport.TTransport;  
import org.apache.thrift.transport.TTransportException;  public class AdditionClient {  public static void main(String[] args) {  try {  TTransport transport;  transport = new TSocket("localhost", 4578);  transport.open();  TProtocol protocol = new TBinaryProtocol(transport);  AdditionService.Client client = new AdditionService.Client(protocol);  System.out.println(client.add(100, 200));  transport.close();  } catch (TTransportException e) {  e.printStackTrace();  } catch (TException x) {  x.printStackTrace();  }  }  }


运行服务端代码(MyServer.java)将会看到下面的输出。

    Starting the simple server...

然后运行客户端代码(AdditionClient.java),将会看到如下输出。

    300


参考文档:http://my.oschina.net/jack230230/blog/66041

               http://www.cnblogs.com/liyonghui/archive/2013/07/12/3186140.html

               http://gemantic.iteye.com/blog/1199214

               https://thrift.apache.org/

               

网盘下载:http://pan.baidu.com/s/1sjI1S4l


http://blog.csdn.net/qiangweiloveforever/article/details/7195145 ,除此之外,博客还有很多很有深度的文章

这篇关于Thrift Java使用实例(修改版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1038364

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依