http服务调用tcp服务

参考 https://github.com/TarsCloud/TarsDocs/blob/master/hello-world/tarsphp.md#chapter-3

现在有了一个HTTP服务,还有一个Tars的TCP服务,如何用HTTP服务里调用TCP服务呢?

将Tars服务的TCPServer.tars 复制到 Http服务的tars目录里

在Http服务里创建 tars/tarsclient.proto.php

<?php
/**
 * Created by PhpStorm.
 * User: liangchen
 * Date: 2018/2/24
 * Time: 下午3:43.
 */

return array(
    'appName' => 'HelloWorld',
    'serverName' => 'TCPServer',
    'objName' => 'obj',
    'withServant' => false, //决定是服务端,还是客户端的自动生成
    'tarsFiles' => array(
        './TCPServer.tars',
    ),
    'dstPath' => '../src/servant',
    'namespacePrefix' => 'HttpServer\servant',
);

执行cd scripts && ./tars2php.sh生成客户端调用代码

会生成对应的servant代码

在IndexController 新增方法

 public function actionTestGreeting()
    {
        $config = new \Tars\client\CommunicatorConfig();
        $config->setLocator(\Tars\App::$tarsConfig['tars']['application']['client']['locator']);

        $userService = new \HttpServer\servant\HelloWorld\TCPServer\obj\TestTafServiceServant($config);
        $greeting = '';
        $return = $userService->sayHelloWorld('Frank Lee', $greeting);

        $this->sendRaw(json_encode(compact('return', 'greeting')));
    }

重新发布http服务 访问此方法 调用成功

posted @ 2020-08-22 22:37  lizcao  阅读(750)  评论(0编辑  收藏  举报