zoukankan      html  css  js  c++  java
  • Thrift实现C#调用Java开发步骤详解

    概述

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

    详细

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

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

    一、下载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各个语言的源码库,以及一些测试程序代码等

    二、编译生产 .NET库(DLL)和JAVA库(jar)

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

    (1) 生成.NET库

    打开工程:E: hrift hrift-0.10.0libcsharpsrcThrift.sln 编译,即可生成Thrift.dll

    我的环境是VS2017以及.NET 4.5

    (2) 生成Java库

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

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

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

    三、编写thrift中间文件hello.thrift

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

    四、生成java和c#各自的接口文件

    将接口文件放到thrift目录:

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

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

    同样生成C#的代码

    thrift --gen csharp hello.thrift

    五、编写java服务端代码

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

    (1)编写接口实现类:

    package com.cnblogs.yjmyzz.demo.service.api.thrift;
    
    import org.apache.thrift.TException;
    
    /** 
      *Created by on 2017/8/4
     */
       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/8/4
     */
    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();
            }
        }
    }

    六、编写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();
            }
        }
    }

    七、运行

    运行java服务端,HelloServiceServer.java:

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

    请按任意键继续…

    调用成功!

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

    注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

  • 相关阅读:
    A1023 Have Fun with Numbers (20分)(大整数四则运算)
    A1096 Consecutive Factors (20分)(质数分解)
    A1078 Hashing (25分)(哈希表、平方探测法)
    A1015 Reversible Primes (20分)(素数判断,进制转换)
    A1081 Rational Sum (20分)
    A1088 Rational Arithmetic (20分)
    A1049 Counting Ones (30分)
    A1008 Elevator (20分)
    A1059 Prime Factors (25分)
    A1155 Heap Paths (30分)
  • 原文地址:https://www.cnblogs.com/demodashi/p/8486531.html
Copyright © 2011-2022 走看看