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是内置的TSimpleServerTThreadPoolServer,这里介绍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+的配置

Pathhttp的访问路径,客户端新建连接时需要与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,这两个要一致,才能正确的调用服务端的函数。

0 0
原创粉丝点击