`
acme_ltt
  • 浏览: 52767 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

CPP服务端++thrift++erl客户端

阅读更多

需求:

        在CPP服务端输入int,在ERL客户端显示。

实现:

1、编写thrift文件,一个命名为tutorial.thrift,一个命名为shared.thrift

tutorial.thrift

 

include "shared.thrift"

namespace cpp tutorial
namespace java tutorial
namespace php tutorial
namespace perl tutorial
namespace smalltalk.category Thrift.Tutorial

typedef i32 MyInteger
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}


service Calculator extends shared.SharedService
{
   void ping(),
   i32 add(),
   oneway void zip()
}

 

 shared.thrift

 

namespace cpp shared
namespace java shared
namespace perl shared

struct SharedStruct {
  1: i32 key
  2: string value
}

service SharedService {
  SharedStruct getStruct(1: i32 key)
}

 

 2、编写CPP服务器端代码

 

#include <concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <server/TThreadPoolServer.h>
#include <server/TThreadedServer.h>
#include <transport/TServerSocket.h>
#include <transport/TTransportUtils.h>

#include <iostream>
#include <stdexcept>
#include <sstream>

#include "../gen-cpp/Calculator.h"

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;

using namespace tutorial;
using namespace shared;

using namespace boost;

class CalculatorHandler : public CalculatorIf 
{
 public:
  CalculatorHandler() {}

  void ping() 
  {
    printf("ping()\n");
  }

  int32_t add() 
  {
  	int32_t n;
  	cin>>n;
    return n;
  }
  void getStruct(SharedStruct &ret, const int32_t logid) {
    printf("getStruct(%d)\n", logid);
    ret = log[logid];
  }

  void zip() {
    printf("zip()\n");
  }

protected:
  map<int32_t, SharedStruct> log;

};

int main(int argc, char **argv) {

  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
  shared_ptr<CalculatorHandler> handler(new CalculatorHandler());
  shared_ptr<TProcessor> processor(new CalculatorProcessor(handler));
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());

  TSimpleServer server(processor,
                       serverTransport,
                       transportFactory,
                       protocolFactory);


  /**
   * Or you could do one of these

  shared_ptr<ThreadManager> threadManager =
    ThreadManager::newSimpleThreadManager(workerCount);
  shared_ptr<PosixThreadFactory> threadFactory =
    shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
  threadManager->threadFactory(threadFactory);
  threadManager->start();
  TThreadPoolServer server(processor,
                           serverTransport,
                           transportFactory,
                           protocolFactory,
                           threadManager);

  TThreadedServer server(processor,
                         serverTransport,
                         transportFactory,
                         protocolFactory);

  */

  printf("Starting the server...\n");
  server.serve();
  printf("done.\n");
  return 0;
}

 

 3、编写客户端ERL

 

-module(client).

-include("calculator_thrift.hrl").

-export([t/0]).

p(X) ->
    io:format("~p~n", [X]),
    ok.

t() ->
    Port = 9090,
    
    {ok, Client} = thrift_client:start_link("127.0.0.1",
                                            Port,
                                            calculator_thrift),

    {ok, Sum} = thrift_client:call(Client, add,  []),
    io:format("the information form cpp server is : ~p~n", [Sum]),


    ok = thrift_client:close(Client),
    ok.

 存在问题:

1、ERL客户端会出现接收超时问题;

      在thrift中,提供了三种服务器类型:

     TSimpleServer:简单的单线程服务器,主要用于测试

    TThreadPoolServer:使用标准阻塞式IO的多线程服务器
    TNonblockingServer:使用非阻塞式IO的多线程服务器,TFramedTransport必须使用该类型的       server

    但是,在ERL中,并没有发现这三种服务类型的设置,在CPP、C#、JAVA等中,都有这三种服务类型的设置。

 

分享到:
评论

相关推荐

    基于thrift开发的客户端和服务端

    在这个"基于thrift开发的客户端和服务端"的示例中,我们将深入理解如何使用Thrift来构建跨平台的服务通信。 首先,Thrift通过定义一个接口描述语言(IDL)来创建服务接口。这个文件通常以`.thrift`为扩展名,它包含...

    p4环境配置安装包(behavioral-model+gmock-1.7.0+p4c+protobuf-3.2.0+thrift-0.9.2)

    p4环境配置安装包(behavioral-model+gmock-1.7.0+p4c+protobuf-3.2.0+thrift-0.9.2),配置教程链接:https://blog.csdn.net/qq_34039018/article/details/88843937

    Thrift RPC客户端的服务化框架代码

    在`mmxf.rpc`这个压缩包中,可能包含了Thrift服务的客户端和服务端实现代码,以及相关的配置文件。解压并分析这些文件,可以帮助我们更好地理解和使用这个服务化框架。在实际应用中,可能还需要考虑线程安全、错误...

    win10+VS2010+thrift0.9.3 改进

    3. 使用Thrift的代码生成器(`thrift.exe`)从.thrift IDL文件生成客户端和服务端的代码,这些代码会处理通信协议的具体实现。 4. 编译并运行你的应用,确保链接了Thrift库,并正确配置了其他依赖项。 通过遵循上述...

    zk+thrift demo

    通过这个 "zk+thrift demo",开发者可以学习如何配置和启动 ZooKeeper 服务,定义 Thrift 服务,生成并集成客户端和服务端代码,最后进行服务调用的测试。这个过程涵盖了分布式系统中服务发现、注册与通信的关键环节...

    ThriftDemo实现客户端服务端通讯.zip

    ThriftDemo实现客户端服务端通讯.zip是一个包含使用RPC框架Thrift进行客户端与服务端通信的示例项目。RPC(Remote Procedure Call)是一种分布式计算技术,它允许程序在不同的网络节点上像调用本地函数一样调用远程...

    Thrift java服务端、php客户端

    在Java服务端,开发者首先会定义一个Thrift IDL(接口定义语言)文件,这个文件中包含了服务的接口和数据结构。例如,可能有一个`Calculator`服务,包含加减乘除等操作。通过Thrift的编译器,这个IDL文件会被转换成...

    thrift官方代码+与dubbo集成支持原生thrift协议

    Thrift通过定义一种中间表示(IDL,Interface Definition Language)来描述服务,允许开发者在不同的语言环境下构建客户端和服务端应用程序。它将接口描述文件编译成特定语言的代码,使得服务提供者和消费者可以使用...

    Apache Thrift Java实战源码,包含了客户端和服务端源码

    在这个实战源码中,我们有两个主要的部分:`thrift-server` 和 `thrift-client`,分别代表服务端和客户端的实现。 服务端(thrift-server): 1. **Thrift IDL (接口定义语言)**:首先,你需要定义服务的接口和数据...

    win10+VS2010+thrift0.9.3

    3. **生成代码**:使用Thrift编译器将IDL文件转换为目标语言(如C++、Java、Python等)的客户端和服务器端代码。 4. **编写业务逻辑**:在生成的代码基础上实现具体的业务逻辑。 5. **部署和运行**:将服务部署到...

    python thrift搭建服务端和客户端测试程序

    Python Thrift 框架详解:构建服务端与客户端测试程序 Thrift 是一个高效的跨语言服务开发框架,由 Facebook 开发并贡献给了 Apache 基金会。它允许开发者定义数据类型和服务接口,然后自动生成多种编程语言的代码...

    Thrift--JSClient

    在这个场景中,"JSClient"指的是JavaScript客户端库,允许JavaScript应用程序与Thrift服务端进行交互。 描述中提到的博客链接可能提供了关于如何在JavaScript中使用Thrift客户端的教程或实践案例。虽然没有具体的...

    ThriftDemo实现客户端服务端通讯_C#_源码.zip

    在这个"ThriftDemo实现客户端服务端通讯_C#_源码.zip"压缩包中,我们很显然会找到一个使用C#实现的Thrift客户端和服务端通信的示例。 1. **Thrift IDL (接口描述语言)** Thrift IDL类似于SOAP的WSDL或Java的RMI-...

    php使用thrift客户端访问服务器测试

    php 使用thrift客户端访问服务器测试 下载后 放到站点根目录,设置 thrift_test.php里的 $socket = new TSocket('10.200.28.43', 10001); 修改为默认服务器和端口 后 直接访问 http://localhost/thrift_test.php

    thrift服务端和客户端实现Nifty.zip

    Nifty是facebook公司开源的,基于netty的thrift服务端和客户端实现。 然后使用此包就可以快速发布出基于netty的高效的服务端和客户端代码。 示例: public void startServer() { // Create the handler ...

    使用thrift、websocket在javascript和cpp之间建立rpc调用

    使用thrift、websocket在javascript和c++之间建立rpc调用机制。 如果要使用js+html来写界面,cpp来写底层业务逻辑,这就非常有用了。 当然,如果底层不用cpp来写,也可以参考本代码的js对thrift的rpc包装。

    Thrift Java 服务器 客户端通信

    在客户端,我们需要创建一个Thrift客户端实例,连接到服务器并调用服务方法: ```java TTransport transport = new TSocket("localhost", 9090); TProtocol protocol = new TBinaryProtocol(transport); MyService....

    编译后的thrift客户端

    编译后的thrift客户端,已经经过公司师父同意分享。 1. cp /Users/dxm/Desktop/thrift /usr/local/bin/ 2. echo $PATH 3. thrift 4. chmod +x /usr/local/bin/thrift 5. thrift 6. thrift -version

    qt 实现thrift的一个例子

    在这个例子中,我们将探讨如何使用Qt来实现一个基于Thrift的C++客户端和服务端的RPC(远程过程调用)连接。 首先,Thrift IDL文件(.thrift)是整个服务的核心,它定义了服务接口和数据结构。例如,我们可能会有一...

Global site tag (gtag.js) - Google Analytics