Thrift实现C#调用Java开发步骤详解

来源:互联网 发布:淘宝虚拟物品怎样退款 编辑:程序博客网 时间:2024/05/22 00:54

Apache Thrift 是 Facebook 实现的一种高效的、支持多种语言的远程服务调用的框架。

类似的跨语言RPC框架还有ICE、Hessian、Protocol Buffer、Avro等。
 

1 下载Thrift

    参考博客 http://blog.csdn.net/gaowenhui2008/article/details/70694560


    CentOS安装Thrift
    官方文档地址:
    http://thrift.apache.org/docs/install/centos

   Apache Thrift 官方JAVA教程
   官方教程的代码地址:
   https://git1-us-west.apache.org/repos/asf?p=thrift.git;a=tree;f=tutorial;hb=HEAD

下载地址:http://thrift.apache.org/download

thrift-0.10.0.exe 用于编译Thrift中间文件生成对应语言代码的工具 (ps:我是在linux环境里边安装的Thrift)

thrift-0.10.0.tar.gz 包含Thrift各个语言的源码库,以及一些测试程序代码等

2 编译生成.NET库(DLL)和Java库(Jar)

解压thrift-0.10.0.tar.gz文件。

(1) 生成.NET库

打开工程:E:\thrift\thrift-0.10.0\lib\csharp\src\Thrift.sln 编译,即可生成Thrift.dll

我的环境是VS2017以及.NET 4.5 

(2) 生成Java库

Java库是通过Ant构建的,需要安装Ant,安装步骤这里就不赘述了。

打开命令行CD到java库代码所在的路径:E:\thrift\thrift-0.10.0\lib\java(包含build.xml)

然后直接执行ant命令即可发现build目录下生成对应的jar文件。

3 编写thrift中间文件 hello.thrift

?
namespace java com.cnblogs.yjmyzz.demo.service.api.thrift
service ThriftHelloService{
    string ping()
}

4 生成Java和C#各自的接口文件

将接口文件放到thrift目录:
 
 

在thrift目录运行 thrift --gen java  hello.thrift



可以看到在当前目录下会出现生成的对应代码。



同样生成C#的代码

thrift --gen csharp hello.thrift




5 编写Java服务端代码

新建Maven项目,然后还要将生成的ThriftHelloService.java也加入到工程中,注意包名。



(1)编写接口实现类:

 

?
package com.cnblogs.yjmyzz.demo.service.api.thrift;import org.apache.thrift.TException;/** * Created by on 2017/6/6. */public class ThriftHelloServiceImpl implements ThriftHelloService.Iface {    @Override    public String ping() throws TException {        return "service call done: ThriftHelloService.ping()123456";    }}

 

(2)编写寄宿代码,启动并监听在指定端口:

?

 

package com.cnblogs.yjmyzz.demo.service.api.thrift;/** * Created by on 2017/6/6. */import org.apache.thrift.TProcessor;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TThreadPoolServer;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TTransportException;import com.cnblogs.yjmyzz.demo.service.api.thrift.ThriftHelloService;import com.cnblogs.yjmyzz.demo.service.api.thrift.ThriftHelloServiceImpl;public class HelloServiceServer {    /**     * 启动 Thrift 服务器     * @param args     */    public static void main(String[] args) {        try {            // 设置服务端口为 7911            TServerSocket serverTransport = new TServerSocket(7911);            // 设置协议工厂为 TBinaryProtocol.Factory            TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory();            // 关联处理器与 Hello 服务的实现            TProcessor processor = new ThriftHelloService.Processor(new ThriftHelloServiceImpl());            TThreadPoolServer.Args args1 = new TThreadPoolServer.Args(serverTransport);            args1.processor(processor);            args1.protocolFactory(proFactory);            TServer server = new TThreadPoolServer(args1);            System.out.println("Start server on port 7911...");            server.serve();        } catch (TTransportException e) {            e.printStackTrace();        }    }}

6 编写C#客户端代码

新建普通控制台项目,引入Thrift.dll;然后还要将生成的ThriftHelloService.cs也加入到工程中。

 


?
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift.Transport;
using Thrift.Protocol;
namespace ConsoleApp1.Properties
{
    class ClientTest
    {
        static void Main(string[] args)
        {
            TTransport transport = new TSocket("localhost", 7911);
            transport.Open();
            TProtocol protocol= new TBinaryProtocol(transport);
            ThriftHelloService.Client client = new ThriftHelloService.Client(protocol);
            Console.WriteLine("ThriftHelloService client.ping().....");
            Console.WriteLine(client.ping());
            System.Console.WriteLine("call done : Hello World! --》"+ client.ping());
            client.Dispose();
            transport.Close();
        }
    }
}

7 运行

运行java服务端

     HelloServiceServer.java:



运行C#客户端代码ClientTest.cs  Ctrl+F5



请按任意键继续…


调用成功!

代码和资料都已经打包,大家也可以下载 http://download.csdn.net/detail/gaowenhui2008/9863226

可以看到Thrift和ICE等跨语言RPC框架开发步骤非常相似,几乎相同,生成的文件也都差不多,但是和基于Servlet的Hessian这种跨语言RPC框架差别较大。

1 0
原创粉丝点击