求生活大爆炸十季百度云1-10季的百度云高清中英字幕资源 谢谢。

Apache Thrift 学习 -
- ITeye技术网站
博客分类:
Apache Thrift 是跨语言服务访问的框架。最早由Facebook 开发,贡献给了Apache。
通过接口定义语言(IDL),定义和创建服务,Thrift生成特定语言的可供server和client 访问的代码。
Thrfit 有着非常优秀的效率,无论是内存还是 传输效率上。
Cassandra 支持多种语言的编程接口,正式由于使用了Thrift。架构
以上是创建server和client的stack。最上面的是IDL,然后生成Client和Processor。红色的是发送的数据。protocol和transport 是Thrift运行库的一部分。
通过Thrift 你只需要关心服务的定义,而不需要关心protocol和transport。
Thrift允许你选择 protocol ,transport和server。因为Thrift 最早是有C++开发的,Thrift在C++实现中有最大的变化。
Thrift支持 text 和 binary protocols,binary protocols要比text protocols,但是有时候 text protocols比较有用(例如:调试的时候)。支持的协议有:
TBinaryProtocol
-直接的二进制格式
TCompactProtocol
效率和高压缩编码数据
TDenseProtocoal
和TCompactProtocol相似,但是省略了meta信息,从哪里发送的,增加了receiver。还在实验中,java实现还不可用。
TJSONProtocoal 使用JSON
TSImpleJSONProtocoal
只写的protocol使用JSON。适合被脚本语言转化
TDebugProtocoal
使用人类可读的text 格式 帮助调试
上面的protocol 说明了 什么被传送,Thrift
transports 说明了怎样传送。支持的transport:
使用 blocking socket I/O
TFramedTransport
以帧的形式发送,每帧前面是一个长度。要求服务器来non-blocking server
TFileTransport
写到文件。没有包括在java实现中。
TMemoryTransport
使用内存 I/O 。java实现中在内部使用了ByteArrayOutputStream
TZlibTransport 压缩 使用zlib。在java实现中还不可用
最后,thrift 提供了servers:
TSimpleServer
单线程 server,使用标准的blocking IO。用于测试
TThreadPoolServer
使用标准的blocking IO
TNonblockingServer
使用 non-blocking IO (java实现中使用了NIO channels)。TFramedTransport必须使用在这个服务器。
一个server只允许定义一个接口服务。这样的话多个接口需要多个server。这样会带来资源的浪费。同意通过继承接口的方式。创建服务
1创建user.thrift
namespace java thrift.demo.gen
namespace py thrift.demo
struct User{
1: i32 id,
2: string username,
3: string password
exception UserNotFound{
1:string message
service UserService{
list&User& getUsers(),
User getUserByName(1:string username) throws(1:UserNotFound unf)
定义了一个User ,一个UerNotFound异常,一个UserService服务接口
服务定义主要使用了 类C的语法
Thrift 定义的类型
base type:bool,byte,i16,i32,i64,double,string(UTF-8 编码)
Contains:list&type&,set&type&,map&type&
services:包含了一组接口方法
注意定义的时候 序号是不可省略的,使用序号可以提升序列化和反序列对象的速度。
2.生成代码
命令行下执行
生成java代码
thrift-0.6.1.exe --gen java
user.thrift
生成python代码
thrift-0.6.1.exe --gen py user.thrift
3.实现服务端
3.1接口服务实现
package thrift.demo.
import java.util.ArrayL
import java.util.L
import org.apache.thrift.TE
import thrift.demo.gen.U
import thrift.demo.gen.UserNotF
import thrift.demo.gen.UserService.I
public class UserServiceHandler implements Iface {
public List&User& getUsers() throws TException {
List&User& list = new ArrayList&User&();
User user = new User();
user.setId(1);
user.setUsername("user1");
user.setPassword("pwd1");
list.add(user);
User user2 = new User();
user2.setId(1);
user2.setUsername("user2");
user2.setPassword("pwd2");
list.add(user2);
public User getUserByName(String username) throws UserNotFound, TException {
if ("user1".equals(username)) {
User user = new User();
user.setId(1);
user.setUsername("user1");
user.setPassword("pwd1");
throw new UserNotFound();
3.2 Server启动类
package thrift.demo.
import org.apache.thrift.TProcessorF
import org.apache.thrift.protocol.TCompactP
import org.apache.thrift.server.THsHaS
import org.apache.thrift.server.TS
import org.apache.thrift.transport.TFramedT
import org.apache.thrift.transport.TNonblockingServerS
import org.apache.thrift.transport.TTransportE
import thrift.demo.gen.UserS
public class UserServer {
public final static int PORT = 8989;
* @param args
public static void main(String[] args) {
TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT);
final UserService.Processor processor = new UserService.Processor(
new UserServiceHandler());
THsHaServer.Args arg = new THsHaServer.Args(socket);
arg.protocolFactory(new TCompactProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processor));
TServer server = new THsHaServer(arg);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
4.实现客户端
package thrift.demo.
import org.apache.thrift.TApplicationE
import org.apache.thrift.TE
import org.apache.thrift.protocol.TCompactP
import org.apache.thrift.protocol.TP
import org.apache.thrift.transport.TFramedT
import org.apache.thrift.transport.TS
import org.apache.thrift.transport.TT
import org.apache.thrift.transport.TTransportE
import thrift.demo.gen.UserNotF
import thrift.demo.gen.UserS
public class UserClient {
public static void main(String[] arsg) {
String address = "127.0.0.1";
int port = 8989;
int clientTimeout = 30000;
TTransport transport = new TFramedTransport(new TSocket(address, port,
clientTimeout));
TProtocol protocol = new TCompactProtocol(transport);
UserService.Client client = new UserService.Client(protocol);
transport.open();
System.out.println(client.getUserByName("user1"));
} catch (TApplicationException e) {
System.out.println(e.getMessage() + " " + e.getType());
} catch (TTransportException e) {
e.printStackTrace();
} catch (UserNotFound e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
transport.close();
注意 客户端和服务端要使用同一中 Protocol 和 Transport,否则会抛出异常
浏览 13646
浏览: 18511 次
来自: 杭州Apache Thrift - Home
The Apache 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.
Getting Started
Download Apache Thrift
To get started,
a copy of Thrift.
Build and Install the Apache Thrift compiler
You will then need to
the Apache Thrift compiler and install it. See the
guide for any help with this step.
Writing a .thrift file
After the Thrift compiler is installed you will need to create a thrift file. This file is an
made up of
and Services. The services you define in this file are implemented by the server and are called by any clients. The Thrift compiler is used to generate your Thrift File into source code which is used by the different client libraries and the server you write. To generate the source from a thrift file run
thrift --gen &language& &Thrift filename&
The sample tutorial.thrift file used for all the client and server tutorials can be found .
To learn more about Apache Thrift
Apache Thrift v0.10.0
Apache Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages. Instead of writing a load of boilerplate code to serialize and transport your objects and invoke remote methods, you can get right down to business.
The following example is a simple service to store user objects for a web front end.
service Calculator extends shared.SharedService {
* A method definition looks like C code. It has a return type, arguments,
* and optionally a list of exceptions that it may throw. Note that argument
* lists and exception lists are specified using the exact same syntax as
* field lists in struct or exception definitions.
void ping(),
i32 add(1:i32 num1, 2:i32 num2),
i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
* This method has a oneway modifier. That means the client only makes
* a request and does not listen for any response at all. Oneway methods
* must be void.
oneway void zip()
def main():
# Make socket
transport = TSocket.TSocket('localhost', 9090)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = Calculator.Client(protocol)
# Connect!
transport.open()
client.ping()
print('ping()')
sum_ = client.add(1, 1)
Initialize the Server:
TServerTransport serverTransport = new TServerSocket(9090);
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();
The CalculatorHandler:
public class CalculatorHandler implements Calculator.Iface {
private HashMap&Integer,SharedStruct& log;
public CalculatorHandler() {
log = new HashMap&Integer, SharedStruct&();
public void ping() {
System.out.println(&ping()&);
public int add(int n1, int n2) {
System.out.println(&add(& + n1 + &,& + n2 + &)&);
return n1 + n2;
public int calculate(int logid, Work work) throws InvalidOperation {
System.out.println(&calculate(& + logid + &, {& + work.op + &,& + work.num1 + &,& + work.num2 + &})&);
int val = 0;
switch (work.op) {
val = work.num1 + work.num2;
case SUBTRACT:
val = work.num1 - work.num2;
case MULTIPLY:
val = work.num1 * work.num2;
case DIVIDE:
if (work.num2 == 0) {
InvalidOperation io = new InvalidOperation();
io.whatOp = work.op.getValue();
io.why = &Cannot divide by 0&;
val = work.num1 / work.num2;
InvalidOperation io = new InvalidOperation();
io.whatOp = work.op.getValue();
io.why = &Unknown operation&;
SharedStruct entry = new SharedStruct();
entry.key = logid;
entry.value = Integer.toString(val);
log.put(logid, entry);
return val;
public SharedStruct getStruct(int key) {
System.out.println(&getStruct(& + key + &)&);
return log.get(key);
public void zip() {
System.out.println(&zip()&);主题 : Apache Thrift框架有用过的没?
级别: 新手上路
可可豆: 240 CB
威望: 240 点
在线时间: 57(时)
发自: Web Page
Apache Thrift框架有用过的没?&&&
Apache Thrift框架有用过的没?网络连接稳定否?会不会经常抛异常?
级别: 圣骑士
可可豆: 3029 CB
威望: 2949 点
在线时间: 484(时)
发自: Web Page
没用过,坐等高手指点,顺便帮顶...
技术杂工,救火队员,各种空降...求老司机指导
级别: 新手上路
可可豆: 131 CB
威望: 21 点
在线时间: 6(时)
发自: Web Page
能用于IOS开发吗?
级别: 新手上路
可可豆: 131 CB
威望: 21 点
在线时间: 6(时)
发自: Web Page
我用过Hessian和Protocol Buffers
级别: 新手上路
可可豆: 15 CB
威望: 15 点
在线时间: 8(时)
发自: Web Page
可以,不过我只是实验性质的玩了一会,没实际用过
级别: 侠客
可可豆: 1160 CB
威望: 1160 点
在线时间: 84(时)
发自: Web Page
引用 引用第3楼方丈于 10:10发表的&&:我用过Hessian和Protocol Buffers 能大致说下,你们的架构是怎么做的吗?或者通讯,现在是protobuf 遇见困难问题。服务器端是用java开发。怎样才能设计好呢?
欧洲杯荷兰淘汰了,自己的假期就这样结束了。找工作咯。
级别: 新手上路
可可豆: 64 CB
威望: 64 点
在线时间: 506(时)
发自: Web Page
在ios8上有问题,ios7上很正常,请高手指点ios多次间隔请求,请求没响应![ 此帖被xingyong723在 14:42重新编辑 ]
级别: 禁止发言
可可豆: 60 CB
威望: 60 点
在线时间: 25(时)
发自: Web Page
用户被禁言,该主题自动屏蔽!
级别: 新手上路
可可豆: 18 CB
威望: 18 点
在线时间: 332(时)
发自: Web Page
回 6楼(xingyong723) 的帖子
你是怎么使用thrift,刚接触,一头雾水
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版

我要回帖

更多关于 生活大爆炸全季百度云 的文章

 

随机推荐