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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do