thrift使用http并部署到iis
来源:互联网 发布:淘宝宝贝七天下架吗 编辑:程序博客网 时间:2024/06/07 15:21
一、简介
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=newTHttpClient("http://localhost:8088/thrift");
transport.open();
TProtocol protocol =new TBinaryProtocol(transport);
Calculator.Client client = new Calculator.Client(protocol);
client.ping();
intre=client.add(1, 2);
System.out.println(re);
transport.close();
}catch(Exceptione){
System.out.println(e);
}
这里的THttpClient中的url就是Web.config里面的path配置的thrift,这两个要一致,才能正确的调用服务端的函数。
- thrift使用http并部署到iis
- 从 HTTP 到 HTTPS - IIS 部署免费 HTTPS
- 项目部署到IIS,并实现可调试
- xbap 部署到IIS
- WCF部署到IIS
- 部署到本地iis
- 项目部署到iis
- 使用hexo并部署到aws上
- 部署NET MVC3 到iis
- WCF部署到IIS服务器
- WebService部署到IIS上
- WCF 部署到IIS 上
- mvc 部署到iis 提示错误未能加载文件或程序集System.Web.Http.WebHost
- vs中编辑ASP.net网站并部署到IIS上
- 首次尝试构建网站并部署到本地IIS服务器上
- 使用IIS配置Http
- IIS部署WEBSERVICE,提示HTTP 错误 500.21
- 使用hexo搭建一个博客并部署到Github
- Android源码下载 清华AOSP镜像源
- 表数据的插入,更新,删除
- Swift-Xcode真机运行出现Reason: image not found错误
- 支付遇到的问题
- JavaScript 学习笔记--02
- thrift使用http并部署到iis
- eclipse tomcat内存溢出,加大内存
- 使用jquery插件 jquery.table2excel.js 将页面中的表格下载到本地(excel格式)
- <VMware>VMware12下Ubuntu15.10磁盘扩容
- 【Unity开发】NGUI动态刷新ScrollView
- JS任意元素的任意值运动
- Java API 远程连接Standalone模式的Hbase
- 在win7下GreenOdoo9开发环境的建立
- Editable和Selection来设置edittext所在的位置