在上一篇文章中提到了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工具会自动从站点下载所需要的依赖包,编译完成后如图所示:
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
}
service ThriftCase { # 注释3 代码生成的类名,你的业务逻辑代码需要实现代码生成的ThriftCase.Iface接口
i32 testCase1(1:i32 num1, 2:i32 num2, 3:string num3) #注释4.1 方法名称和方法中的入参,入参类型参见wiki
list
void testCase3()
void testCase4(1:list
}
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个方法,服务器端被传入客户端数据,运行效果如图所示:
相关文章:
Apache Thrift入门1-架构&介绍
–end–
原文地址:Apache Thrift入门2-Java代码实现例子, 感谢原作者分享。

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.