Home Database Mysql Tutorial Apache Thrift入门2-Java代码实现例子

Apache Thrift入门2-Java代码实现例子

Jun 07, 2016 pm 04:26 PM
apache thrift code getting Started accomplish

在上一篇文章中提到了Thrift的架构、传输协议(Ref),本篇文章将对Thrift的入门实例进行介绍。 分为如下5个部分: 运行环境、安装/配置、脚本文件、创建代码、运行程序。 一、开发环境(清单1) 1.操作系统 Server-Linux / Client-WinXP 2.SDK Sun JDK1.5+ 3.需

  在上一篇文章中提到了Thrift的架构、传输协议(Ref),本篇文章将对Thrift的入门实例进行介绍。 分为如下5个部分: 运行环境、安装/配置、脚本文件、创建代码、运行程序。

一、开发环境(清单1)
1.操作系统
    Server-Linux / Client-WinXP
2.SDK
    Sun JDK1.5+

3.需要的jar依赖包
    libthrift.jar
    slf4j-api-1.5.8.jar
    slf4j-log4j12-1.5.8.jar
    log4j-1.2.15.jar

4.编译工具
    Apache Ant  & Apache ivy

二、安装/配置 (清单2)
    1.下载thrift源文件
        http://labs.renren.com/apache-mirror//incubator/thrift/0.5.0-incubating/thrift-0.5.0.tar.gz
        
    2.编译thrift源文件
        1)解压 thrift-0.5.0.tar.gz
        2)用ant编译源代码,进入x:\thrift-0.5.0\lib\java目录,执行ant,通过ant中的ivy工具会自动从站点下载所需要的依赖包,编译完成后如图所示:
         http://zcitrq.bay.livefilestore.com/y1pA4vlwBHBEPK_vNd4oVbU_egUdTTdw7fKAW_rmoBiqEg-ZO8rfDednCMCylQqrM1SpKVOp49bbbD0rfMhiaRHwq93o5RHaQUG/complie-thirtf.png?psid=1
        3)编译过程中下载的依赖包在x:\thrift-0.5.0\lib\java\build\ivy\lib 目录下可以看见下载的jar依赖包,将编译成功以后的jar包加入Eclipse的开发环境中。

三、脚本文件(清单3)
    1.创建脚本
        创建脚本文件 testJava.thrift ,脚本文件内容如下:
        namespace java com.javabloger.gen.code   # 注释1   定义生成代码的命名空间,与你需要定义的package相对应。

        struct Blog {   #  注释2.1   定义实体名称和数据结构,类似你业务逻辑中的pojo get/set
            1: string topic     #  注释2.2  参数类型可以参见 Thrift wiki  
            2: binary content  
            3: i64    createdTime
            4: string id
            5: string ipAddress
            6: map props
          }
        service ThriftCase {  #  注释3    代码生成的类名,你的业务逻辑代码需要实现代码生成的ThriftCase.Iface接口
            i32 testCase1(1:i32 num1, 2:i32 num2, 3:string  num3) #注释4.1 方法名称和方法中的入参,入参类型参见wiki
            list testCase2(1:map  num1)
            void testCase3()
            void testCase4(1:list blog)   #  注释4.2   list 是thrift中基本数据类型中的一种,list中包含的Blog对象是上面struct中定义的
        }

    2.运行脚本
        1)从 thrift 站点下载windows版本的编译工具,下载地址:http://labs.renren.com/apache-mirror//incubator/thrift/0.5.0-incubating/thrift-0.5.0.exe
        2)通过Thrift的脚本文件,运行 thrift 命令创建生成的代码,例如:执行 thrift -gen java x:\testJava.thrift  命令,在当前运行的盘符下,可看见gen-java目录,在这里目录中可以看见生成的java代码,更多thrift 命令内容,请参见thrift命令自带的help。

3.Thrift 中的基本数据类型 (清单4)
    类型 描述
bool true, false
byte 8位的有符号整数
i16 16位的有符号整数
i32 32位的有符号整数
i64 64位的有符号整数
double 64位的浮点数
string UTF-8编码的字符串
binary 字符数组
struct 结构体
list 有序的元素列表,类似于STL的vector
set 无序的不重复元素集,类似于STL的set
map key-value型的映射,类似于STL的map
exception 是一个继承于本地语言的exception基类
service 服务。包含多个函数接口(纯虚函数)

四、创建代码(清单5)
    我将示例工程分了4个包,如下所示:
     \com\javabloger
            \client               # 1.客户端测试代码
            \gen\code              # 2.通过脚本生成的class
            \layer\transport       # 3.服务器端代码和定义的传输协议
            \layer\business      # 4.具体的业务逻辑代码
    具体代码内容这里就不阐述了,重点是要明白代码的结构和层次关系,其次是里面主要的几个类的含义,至于代码是怎么写的并不是非常重要,仅仅是我个人观点,仅供参考,谢谢。

代码示例的下载地址: http://javabloger-mini-books.googlecode.com/files/Thritf.zip

五、运行程序
    先运行server,再运行client ,客户端向服务器端发送数据调用服务器端的4个方法,服务器端被传入客户端数据,运行效果如图所示:
    run-thirtf-code.png

相关文章:
Apache Thrift入门1-架构&介绍

–end–
 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

How to check Debian OpenSSL configuration How to check Debian OpenSSL configuration Apr 12, 2025 pm 11:57 PM

This article introduces several methods to check the OpenSSL configuration of the Debian system to help you quickly grasp the security status of the system. 1. Confirm the OpenSSL version First, verify whether OpenSSL has been installed and version information. Enter the following command in the terminal: If opensslversion is not installed, the system will prompt an error. 2. View the configuration file. The main configuration file of OpenSSL is usually located in /etc/ssl/openssl.cnf. You can use a text editor (such as nano) to view: sudonano/etc/ssl/openssl.cnf This file contains important configuration information such as key, certificate path, and encryption algorithm. 3. Utilize OPE

How to use Debian Apache logs to improve website performance How to use Debian Apache logs to improve website performance Apr 12, 2025 pm 11:36 PM

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

See all articles