thrift使用http并部署到iis
2016-06-14 22:01
661 查看
一、简介
1. Thrift
Apache Thrift 是 Facebook
实现的一种高效的、支持多种编程语言的远程服务调用的框架。
http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/
这篇文章对thrift的使用有详细的介绍。
2. Thrift 协议栈
Tprotocol 协议层
将数据(model)编码 、解码 。
Ttransport 传输层
编码后的数据传输(简单socket、http)
Tserver
服务的Tserver类型,实现了几种rpc调用(单线程、多线程、非阻塞IO)
网上的例子大部分都是采用socket传输,Tserver是内置的TSimpleServer,TThreadPoolServer,这里介绍thrift采用http传输,并且将服务部署到iis,不采用内置的Tserver。客户端使用java编写。
参考:https://codealoc.wordpress.com/2012/04/06/thrift-over-http-with-iis/
http://stackoverflow.com/questions/35337221/can-you-use-thrift-as-a-method-of-communication-between-apps-in-cloud-foundry/35938562#35938562
二、例子
1 生成代码
编译工具:Thriftcompiler for Windows (thrift-0.9.3.exe)
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.3/thrift-0.9.3.exe
官方例子:Generatedthe tutorial.thrift and shared.thrift files
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/tutorial.thrift
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/shared.thrift
将编译程序和两个文件放在同一个文件夹
执行命令
thrift-0.9.3-r --gen csharp tutorial.thrift
thrift-0.9.3-r --gen java tutorial.thrift
就生成了相应的语言的代码
2 c#服务端
用的是vs2013
新建一个空的web工程。
将生成的c#代码添加到工程里。
下载thrift源码生成最新的c#的thrift.dll,添加到工程里。
实现接口
根据官网的教程添加实际接口函数的业务代码。
实现THttpHandler接口用于配置Web.config,使thrift通过http传输。
namespaceThriftWebApp
{
public class ThriftHttpHandler:THttpHandler
{
public ThriftHttpHandler()
:base(new Calculator.Processor(newCalculatorHandler()),new TBinaryProtocol.Factory())
{ }
}
}
在Web.config添加如下的项,这是iis7+的配置
Path是http的访问路径,客户端新建连接时需要与path匹配
<configuration>
<system.webServer>
<handlers>
<add name="HttpService"path="thrift" resourceType="Unspecified"type="ThriftWebApp.ThriftHttpHandler" verb="*"/>
</handlers>
</system.webServer>
</configuration>
Iis6可能以下的配置,没有试过。
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="thrift"
type=" ThriftWebApp.ThriftHttpHandler " />
</httpHandlers>
</system.web>
</configuration>
现在就可以启动调试了。
Web服务打包
部署到iis还需要打包
右击工程,选择发布,点击新建配置文件。
选择文件系统
最后会在选择的文件夹生成编译好的dll文件和配置文件。
Iis部署
在iis里添加新的网站,选择刚才打包的物理路径。
应用程序池,基本设置中设置.net4
启动web服务,就可以通过客户端访问了。
3 java客户端
同样的将刚才生成的java代码添加到工程里。
在main函数添加下面的测试代码
TTransport transport=null;
try{
HttpClienthttpclient =
new DefaultHttpClient();
transport=new
THttpClient("http://localhost:8088/thrift");
transport.open();
TProtocol protocol =
new TBinaryProtocol(transport);
Calculator.Client
client = new Calculator.Client(protocol);
client.ping();
int
re=client.add(1, 2);
System.out.println(re);
transport.close();
}catch(Exception
e){
System.out.println(e);
}
这里的THttpClient中的url就是Web.config里面的path配置的thrift,这两个要一致,才能正确的调用服务端的函数。
1. Thrift
Apache Thrift 是 Facebook
实现的一种高效的、支持多种编程语言的远程服务调用的框架。
http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/
这篇文章对thrift的使用有详细的介绍。
2. Thrift 协议栈
Tprotocol 协议层
将数据(model)编码 、解码 。
Ttransport 传输层
编码后的数据传输(简单socket、http)
Tserver
服务的Tserver类型,实现了几种rpc调用(单线程、多线程、非阻塞IO)
网上的例子大部分都是采用socket传输,Tserver是内置的TSimpleServer,TThreadPoolServer,这里介绍thrift采用http传输,并且将服务部署到iis,不采用内置的Tserver。客户端使用java编写。
参考:https://codealoc.wordpress.com/2012/04/06/thrift-over-http-with-iis/
http://stackoverflow.com/questions/35337221/can-you-use-thrift-as-a-method-of-communication-between-apps-in-cloud-foundry/35938562#35938562
二、例子
1 生成代码
编译工具:Thriftcompiler for Windows (thrift-0.9.3.exe)
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.3/thrift-0.9.3.exe
官方例子:Generatedthe tutorial.thrift and shared.thrift files
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/tutorial.thrift
https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob_plain;f=tutorial/shared.thrift
将编译程序和两个文件放在同一个文件夹
执行命令
thrift-0.9.3-r --gen csharp tutorial.thrift
thrift-0.9.3-r --gen java tutorial.thrift
就生成了相应的语言的代码
2 c#服务端
用的是vs2013
新建一个空的web工程。
将生成的c#代码添加到工程里。
下载thrift源码生成最新的c#的thrift.dll,添加到工程里。
实现接口
根据官网的教程添加实际接口函数的业务代码。
实现THttpHandler接口用于配置Web.config,使thrift通过http传输。
namespaceThriftWebApp
{
public class ThriftHttpHandler:THttpHandler
{
public ThriftHttpHandler()
:base(new Calculator.Processor(newCalculatorHandler()),new TBinaryProtocol.Factory())
{ }
}
}
在Web.config添加如下的项,这是iis7+的配置
Path是http的访问路径,客户端新建连接时需要与path匹配
<configuration>
<system.webServer>
<handlers>
<add name="HttpService"path="thrift" resourceType="Unspecified"type="ThriftWebApp.ThriftHttpHandler" verb="*"/>
</handlers>
</system.webServer>
</configuration>
Iis6可能以下的配置,没有试过。
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="thrift"
type=" ThriftWebApp.ThriftHttpHandler " />
</httpHandlers>
</system.web>
</configuration>
现在就可以启动调试了。
Web服务打包
部署到iis还需要打包
右击工程,选择发布,点击新建配置文件。
选择文件系统
最后会在选择的文件夹生成编译好的dll文件和配置文件。
Iis部署
在iis里添加新的网站,选择刚才打包的物理路径。
应用程序池,基本设置中设置.net4
启动web服务,就可以通过客户端访问了。
3 java客户端
同样的将刚才生成的java代码添加到工程里。
在main函数添加下面的测试代码
TTransport transport=null;
try{
HttpClienthttpclient =
new DefaultHttpClient();
transport=new
THttpClient("http://localhost:8088/thrift");
transport.open();
TProtocol protocol =
new TBinaryProtocol(transport);
Calculator.Client
client = new Calculator.Client(protocol);
client.ping();
int
re=client.add(1, 2);
System.out.println(re);
transport.close();
}catch(Exception
e){
System.out.println(e);
}
这里的THttpClient中的url就是Web.config里面的path配置的thrift,这两个要一致,才能正确的调用服务端的函数。
相关文章推荐
- http和https协议
- http和https协议
- C/C++网络编程总结
- 基于TCP/IP协议下的Socket通信
- 利用Wireshark 解密HTTPS流量
- Android网络编程(一)
- 【网络流24题----01】飞行员配对方案问题
- nginx做前端反代负载均衡,后端httpd+tomcat
- Spring3中js/css/jpg/gif等静态资源无法找到(No mapping found for HTTP request with URI)问题解决(转)
- 【51CTO学院三周年】藏头诗一首送祝福
- 阿里云 CentOS6.7 PHP5.2.17 lighttpd GD库 安装配置记录
- 最小连通网络
- libpcap/tcpdump filter 语法
- 线程与网络编程(第六节:通信协议与TCP socket初识)
- linux网络编程--TCP/IP协议
- MyAsyAsyncTask工具类网络请求HttpClient
- Deep Learning(深度学习)学习笔记整理系列之(七)Convolutional Neural Networks卷积神经网络
- 强大的请求网络图片并自动缓存的第三方框架------Picasso
- TCP/IP源码(59)——TCP中的三个接收队列
- Android中的TCP协议与UDP协议