`
tcspecial
  • 浏览: 897696 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

thrift servlet

    博客分类:
  • java
阅读更多

 

Thrift通常以独立服务存在,也支持HTTP协议,以servlet方式在Tomcat中运行。

 

一. 服务端Servlet

实现比较简单,只需实现TExtensibleServlet,并在getProcessor()中填充实现类。

/**
 * Thrift servlet
 */
@WebServlet(name="thrifttest",value="/thrifttest")
public class ThriftService extends TExtensibleServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected TProtocolFactory getInProtocolFactory() {
		TProtocolFactory factory = new TCompactProtocol.Factory();
		return factory;
	}

	@Override
	protected TProtocolFactory getOutProtocolFactory() {
		TProtocolFactory factory = new TCompactProtocol.Factory();
		return factory;
	}

	@Override
	protected TProcessor getProcessor() {
		// 接口实现类
		ThriftServiceImpl impl = new ThriftServiceImpl();

		// 返回处理器
		TProcessor tProcesser = new SimpleThriftService.Processor<SimpleThriftService.Iface>(impl); 
		return tProcesser;
	}
}

 

二. 客户端

/**
 * Http thrift client
 */
public static void httpClient() {  
	String url = "http://host:8080/servlet3/thrifttest";  
	try {
		TTransport transport = new THttpClient(url); 	         
		TProtocol protocol = new TCompactProtocol(transport); // HTTP通信协议     
		SimpleThriftService.Client client = new SimpleThriftService.Client(protocol);

		transport.open();  
		int val = client.getInt(10); 	// 调用方法
		System.out.println( "Val: " + val );

		transport.close();  
	} catch (TException e) {
		e.printStackTrace();
	}  
} 

 

三. 协议分析

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 72.8 KB
分享到:
评论

相关推荐

    thrift 生成的java包servlet-api-2.5.jar

    thrift 生成的java包servlet-api-2.5.jar

    the programmer's guide to apache thrift

    Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...

    thrift实现http协议案例

    thrift实现http协议案例,thrift官方好像就一个socket,此案例是通过继承servlet实现http协议通信

    thrift源码

    thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码...

    Windows下QT使用Thrift的样例

    网上一直搜不到Windows下QT使用thrift的例子,自己整了个 QT版本 5.8.0 Boost版本 boost_1_61_0 Thrift版本 thrift-0.10.0

    thrift安装

    thrift 安装包。

    使用wireshark抓取thrift协议接口调用

    使用wireshark抓取thrift协议接口调用

    thrift-0.9.2.tar.gz

    thrift,Apache Thrift 0.9.2 版本,解压后直接直接安装,可伸缩的跨语言服务开发框架,命令: 解压命令:tar -zxf thrift-0.9.2.tar.gz 安装命令:./configure --with-lua=no && make && make install 查看版本:...

    Thrift之C++初体验

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...

    thrift官方代码+与dubbo集成支持原生thrift协议

    thrift官方代码+与dubbo集成支持原生thrift协议

    Learning.Apache.Thrift.178588274

    Make applications cross-communicate using Apache Thrift! About This Book Leverage Apache Thrift to enable applications written in different programming languages (Java, C++, Python, PHP, Ruby, and so...

    thrift-编译工具

    thrift-编译工具

    java代码使用thrift2操作hbase示例

    java代码使用thrift2操作hbase示例,thrift2连接hbase添加数据,单条查找,删除数据,根据扫描器查找,修改数据等测试实例

    thrift-0.9.3.exe

    Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...

    thrift-0.9.1.exe和thrift-0.9.2.exe

    thrift文件生成工具thrift-0.9.1.exe和thrift-0.9.2.exe压缩包

    thrift-Demo

    thrift一个实例

    unity3d thrift twisted

    unity3d-thrift-twistedunity3d-thrift-twistedunity3d-thrift-twisted

    使用thrift、websocket在javascript和cpp之间建立rpc调用

    使用thrift、websocket在javascript和c++之间建立rpc调用机制。 如果要使用js+html来写界面,cpp来写底层业务逻辑,这就非常有用了。 当然,如果底层不用cpp来写,也可以参考本代码的js对thrift的rpc包装。

    Thrift入门简介

    通俗简单的介绍了什么是thrift,适用于thrift或RPC扫盲。

    用C#和C++写的Apache Thrift的小范例

    本例改编自Apache Thrift教程: http://mikecvet.wordpress.com/2010/05/13/apache-thrift-tutorial-the-sequel/ http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/ 原教程使用的是c++ server和...

Global site tag (gtag.js) - Google Analytics