• 使用thrift进行跨语言调用(php c# java)


    使用thrift进行跨语言调用(php c# java)

     

    1:前言

    实际上本文说的是跨进程的异构语言调用,举个简单的例子就是利用PHP写的代码去调C#或是java写的服务端。其实除了本文提供的办法还有其他办法,例如http+xml(json)等等都能做到。

    本文的意义是介绍thrift,记录我在调试thrift时遇到的问题和相应的解决方案,避免大家走弯路。

    2:大概的流程

    thrift是通过socket+序列化协议来完成跨语言调用的。类似的方案有protocolbuffer(http://code.google.com/p/protobuf/这个性能出众,thrift性能我回头再做测试。

    使用的流程是

    A:定义自己的通信接口,接口可以使用的数据类型有string,int32等,当然你也可以自己定义枚举和结构体

    B:使用thrift.exe生成相应的代码

    C:调用

    3:c#,PHP,Java在调试thrift时遇到的问题

    首先我们去下载thrift,地址是http://thrift.apache.org/

    解压缩后会看到

    lib下就是执行的各种语言的代码。

    A:调试PHP注意事项

    一定要注册各个php代码也就是这句。

    require_once __DIR__.'/lib/Thrift/ClassLoader/ThriftClassLoader.php';
    $loader = new ThriftClassLoader();
    $loader->registerNamespace('Thrift', __DIR__ . '/lib');
    $loader->register();

    B:调试java注意事项

    下载没有提供的包

    地址是:

    http://commons.apache.org/proper/commons-lang/download_lang.cgi

    http://hc.apache.org/downloads.cgi

    http://www.slf4j.org/

    3:demo

    定义接口(方法名不能一样)

    复制代码
    enum ParameterValueType
    {
      AnsiString = 1,
      Byte,
      Boolean,
      Currency = 4,
      Date,
      DateTime,
      Decimal,
      Double,
      Guid,
      Int16,
      Int32,
      Int64,
      String,
      Time,
      Xml
    }
    
    enum ParameterValueDirection 
    {
       Input,
       Output,
       InputOutput,
       ReturnValue
    }
    
    enum AdoCommandType 
    {
       Text,
       StoredProcedure
    }
    
    struct DBParameter {
      1: string DbParameterName,
      2: ParameterValueType DbType,
      3: string DbParameterValue,
      4: ParameterValueDirection DbDirection,
      5: i32 Size
    }
    
    struct ResultMessage {
      1: i32 IsSuccess,
      2: string ErrorMessage,
      3: string ReturnValue,
      4: list<DBParameter> ReturnParameter,
      5: list<string> DataSetColumnName,
      6: list<list<string>> DataSetRowValue
    }
    
    service DataAccessComponent {
      ResultMessage ExecuteDataset(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter), 
      ResultMessage ExecuteNonQuery(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter),
      ResultMessage ExecuteScalar(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter),
    }
    复制代码

    生成代码

    thrift -gen java ado.thrift
    thrift -gen php ado.thrift
    thrift -gen csharp ado.thrift

    根据语言把生成的语言放到相应的文件夹下。

    下面简单列一下调用代码

    1:PHP
        //设置IP,端口建立连接
        $socket = new TSocket('localhost', '9090');
        $transport = new TBufferedTransport($socket);
         
        //选定通信协议
        $protocol = new TBinaryProtocol($transport);
        $client = new DataAccessComponentClient($protocol); //生成的类
     
    2:c#
    server
                int port = 9090;
                DataAccess da = new DataAccess();
                // Processor
                DataAccessComponent.Processor pc = new DataAccessComponent.Processor(da);
     
                // Transport
                TServerSocket tServerSocket =
                  new TServerSocket(port);
     
                // Protocol factory
                TProtocolFactory tProtocolFactory =
                  new TBinaryProtocol.Factory();
     
                TServer serverEngine;
     
                // Simple Server
                serverEngine = new TSimpleServer(pc, tServerSocket);
     
                // ThreadPool Server
                //serverEngine = new TThreadPoolServer(pc,tServerSocket);
     
                // Run it
                serverEngine.Serve();
     
    client
                TTransport transport = new TSocket("localhost", 9090);
                TProtocol protocol = new TBinaryProtocol(transport);
                ThriftTest.Client client = new ThriftTest.Client(protocol);
                transport.Open();
                int val = client.test("1213");
                client.work();
                transport.Close();
                Console.WriteLine(val);
                Console.ReadLine();
    3:java
    server
            try {
                //private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());
                 
                //System.setProperty("log4j.configuration", "log4j.properties");
                 
                int port = 9090;
                  // Processor
                ThriftServer testHandler =
                    new ThriftServer();
                  ThriftTest.Processor testProcessor =
                    new ThriftTest.Processor(testHandler);
     
                  // Transport
                  TServerSocket tServerSocket =
                    new TServerSocket(port);
     
                  // Protocol factory
                  TProtocolFactory tProtocolFactory =
                    new TBinaryProtocol.Factory();
     
                  TServer serverEngine;
     
                  // Simple Server
                  //serverEngine = new TSimpleServer(new Args(tServerSocket).processor(testProcessor));
     
                  // ThreadPool Server
                  serverEngine = new TThreadPoolServer(new TThreadPoolServer.Args(tServerSocket).processor(testProcessor).protocolFactory(tProtocolFactory));
     
                  //Set server event handler
                  serverEngine.setServerEventHandler(new TestServerEventHandler());
     
                  // Run it
                  System.out.println("Starting the server on port " + port + "...");
                  serverEngine.serve();
                   
            } catch (Exception x) {
     
                x.printStackTrace();
     
            }
            System.out.println("done.");
     
    分类: 实战
  • 相关阅读:
    焦虑:都说程序员是青春饭,那么程序员老了何去何从呢?
    数据库查询语句优化,mysql优化,join语句优化附带YYC松鼠短视频系统详细demo效果
    IT行业:为什么大部分人都不认可php语言呢?
    拇指赚点赞无加密源码发布分享仅供学习
    区块鼠区块养殖系统源码无加密源码发布分享仅供学习
    3月1日晚突遭大量攻击,网站/APP突然遭遇黑客攻击时该如何应对?
    ThinkPHP内核全行业小程序运营管理系统源码免费分享下载
    2020年不管打工还是创业,居然还有人相信读书无用论?
    在IT界,应聘企业去上班如果老板一点不懂技术那绝对是作茧自缚
    看了这篇文章你还不懂傅里叶变换,那我就真没办法呢!
  • 原文地址:https://www.cnblogs.com/archoncap/p/5219361.html
Copyright © 2020-2023  润新知