栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

laravel apache thrift 学习

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

laravel apache thrift 学习


首先需要在电脑上安装thrift,这里下载的thrift-0.10.0.tar.gz包,可以在github上查到。

https://github.com/apache/thrift.git

Laravel 安装扩展

composer require 

"apache/thrift": "~0.10.0",

然后需要创建一个thrift文件,我这里创建了一个用户相关的文件rpc.thrift,具体内容如下:

namespace php rpc

service Bbs{

string getThreadList(1:i64 id,2:i16 classId,3:i64 userId,4:i16 status,5:i16 isStick,6:i16 isStickGlobal,7:i16 isDigest,8:string title,9:i16 order,10:i16 page,11:i16 pageSize,12:string startTime,13:string endTime,14:i16 flash);
  • 基本类型:

    • bool:布尔值,true 或 false,对应 Java 的 boolean

    • byte:8 位有符号整数,对应 Java 的 byte

    • i16:16 位有符号整数,对应 Java 的 short

    • i32:32 位有符号整数,对应 Java 的 int

    • i64:64 位有符号整数,对应 Java 的 long

    • double:64 位浮点数,对应 Java 的 double

    • string:utf-8编码的字符串,对应 Java 的 String

  • 结构体类型:

    • struct:定义公共的对象,类似于 C 语言中的结构体定义,在 Java 中是一个 JavaBean

  • 容器类型:

    • list:对应 Java 的 ArrayList

    • set:对应 Java 的 HashSet

    • map:对应 Java 的 HashMap

  • 异常类型:

    • exception:对应 Java 的 Exception

  • 服务类型:

    • service:对应服务的类



然后根据该thrift文件生成相应的service、types文件


进入创建thrift 文件夹生成命令 

thrift -gen php:server rpc.thrift

如果php的客户端与服务端分开部署的话,需要执行以下命令:

thrift -gen php rpc.thrift

我这里执行的是第一句命令,具体会在gen-php目录下生成三个文件:

gen-php 下会自动生成


然后先创建服务端文件,userserver.php,内容如下:

 

namespace UserModulephp;

error_reporting(E_ALL);

require_once __DIR__.'/PHP/Thrift/ClassLoader/ThriftClassLoader.php';

use ThriftClassLoaderThriftClassLoader;

 

 

$GEN_DIR = __DIR__.'/ThriftGen/gen-php';

$loader = new ThriftClassLoader();

$loader->registerNamespace('Thrift', __DIR__ . '/PHP');

$loader->registerDefinition('UserModule', $GEN_DIR);

$loader->register();

 

if (php_sapi_name() == 'cli') {

  ini_set("display_errors", "stderr");

}

 

use ThriftProtocolTBinaryProtocol;

use ThriftTransportTPhpStream;

use ThriftTransportTBufferedTransport;

use ThriftTMultiplexedProcessor;

use UserModuleUser;

use UserModuleInvalidException;

 

class LoginServiceHandler implements UserModuleLoginServiceIf 

{

    public function Login($username, $pwd) 

    {

 

    }

 

    public function Register($username, $pwd)

    {

        $objUser = new User();

        if($username=='wangzhongwei') {

          throw new InvalidException(array('code'=>111,'message'=>'该用户已经注册!'));

        }else {

           $objUser->username = $username;

            $objUser->password = $pwd;

            $objUser->id = 1;

            $objUser->age = 33;

            $objUser->email = 'areyouok@163.com';

            $objUser->tel = '13716857451';

        }

       

        return $objUser;

    }

 

    public function getCheckCode($sessionid)

    {

 

    }

 

    public function verifyCheckCode($sessionid, $checkcode)

    {

 

    }

}

 

class UserServiceHandler implements UserModuleUserServiceIf

{

    public function Detail($id)

    {        

        $objUser = new User();

        if($id == 1) {

          $objUser->username = 'wangxiaoxiao';

          $objUser->password = 'howareyouok';

          $objUser->id = 1;

          $objUser->age = 34;

          $objUser->email = 'areyouok@163.com';

          $objUser->tel = '13716857451';

        }        

        return $objUser;

    }

    public function Update(UserModuleUser $user)

    {

        $objUser = new User();

        if($user->id == 1) {

          $objUser->id = $user->id;

          $objUser->username = $user->username;

          $objUser->age = $user->age;

          $objUser->email = $user->email;

          $objUser->tel = $user->tel;

        }        

        return $objUser;

    }

    public function search(UserModuleInParamsUser $inParams)

    {

        $result = new UserModuleOutputParamsUser();

        $result->page = $inParams->page;

        $result->pageSize = $inParams->pageSize;

        $result->totalNum = 1;

        $arrUsers = [];

        $objUser = new User();

        $objUser->username = 'wangxiaoxiao';

        $objUser->password = 'howareyouok';

        $objUser->id = 1;

        $objUser->age = 34;

        $objUser->email = 'areyouok@163.com';

        $objUser->tel = '13716857451';

 

        $arrUsers[] = $objUser;

 

        $objUser1 = clone $objUser;

        $objUser1->username = 'whatareyoudoing';

        $objUser1->tel = '13855254475';

        $arrUsers[] = $objUser1;

 

        $result->records = $arrUsers;

 

        return $result;

    }

    public function getAllStatus()

    {

        return [0=>'新创建',1=>'已认证',2=>'已删除'];

    }

}

 

 

header('Content-Type', 'application/x-thrift');

if (php_sapi_name() == 'cli') {

  echo "n";

}

 

$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));

$protocol = new TBinaryProtocol($transport, true, true);

$tMultiplexedProcessor = new TMultiplexedProcessor();

 

$loginService = new LoginServiceHandler();

$loginServiceProcessor = new UserModuleLoginServiceProcessor($loginService);

$tMultiplexedProcessor->registerProcessor('loginService', $loginServiceProcessor);

 

$userService = new UserServiceHandler();

$userServiceProcessor = new UserModuleUserServiceProcessor($userService);

$tMultiplexedProcessor->registerProcessor('userService', $userServiceProcessor);

 

$transport->open();

 

try{

  $tMultiplexedProcessor->process($protocol, $protocol);  

} catch(TException $e) {

  $transport->close();

  throw $e;

}

 

$transport->close();

 

 

 

 

客户端文件: userclient.php,代码如下

namespace UserModulephp;

error_reporting(E_ALL);

require_once __DIR__.'/PHP/Thrift/ClassLoader/ThriftClassLoader.php';

 

use ThriftClassLoaderThriftClassLoader;

 

$GEN_DIR = __DIR__.'/ThriftGen/gen-php';

$loader = new ThriftClassLoader();

$loader->registerNamespace('Thrift', __DIR__ . '/PHP');

$loader->registerDefinition('UserModule', $GEN_DIR);

$loader->register();

 

use ThriftProtocolTBinaryProtocol;

use ThriftProtocolTMultiplexedProtocol;

use ThriftTransportTHttpClient;

use ThriftTransportTBufferedTransport;

use ThriftExceptionTException;

use UserModuleInvalidException;

use UserModuleUser;

 

try {

  $socket = new THttpClient('localhost', 80, '/thrift/userserver.php');

  $transport = new TBufferedTransport($socket);

  $protocol = new TBinaryProtocol($transport);

 

  $loginProtocol = new TMultiplexedProtocol($protocol, 'loginService');

  $loginService = new UserModuleLoginServiceClient($loginProtocol);

  $result = $loginService->Register('wangzhongwei','areyouok');

  var_dump($result);

  echo '
';

 

  // $result = $loginService->login('wangzhongwei','areyouok');

  // var_dump($result);

  // echo '
';

 

  $userProtocol = new TMultiplexedProtocol($protocol, 'userService');

  $userService = new UserModuleUserServiceClient($userProtocol);

  $result = $userService->Detail(1);

  var_dump($result);

  echo '
';

 

  $result->age = $result->age+3;

  $result = $userService->Update($result);

  var_dump($result);

  echo '
';

 

  $params = new UserModuleInParamsUser();

  $params->page = 1;

  $params->pageSize = 10;

  $result = $userService->search($params);

  var_dump($result);

  echo '
';

 

  $result = $userService->getAllStatus();

  var_dump($result);

  echo '
';

 

 

 

 

 

  $transport->close();

  

} catch (InvalidException $tx) {

  print 'TException: '.$tx->getCode().' '.$tx->getMessage()."n";

}

 

输入地址栏URL地址:http://127.0.0.1/thrift/userclient.php,没有报错就ok.

本例程序还是以http协议的方式对外提供,也可以通过服务的方式提供接口。


如果需要将该代码以服务的形式对外提供,大致步骤如下:


服务端编写的一般步骤:

1. 创建Handler

2. 基于Handler创建Processor

3. 创建Transport(通信方式)

4. 创建Protocol方式(设定传输格式)

5. 基于Processor, Transport和Protocol创建Server

6. 运行Server

 

 

客户端编写的一般步骤:

1. 创建Transport

2. 创建Protocol方式

3. 基于Transport和Protocol创建Client

4. 运行Client的方法


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/227871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号