hporse-laravel-rpc(http,tpc)

laravel 安装 hprose

1安装hprose-php

2laravel http tcp服务启动

3supervisord后台常驻运行

4微服务调用

我这里用的是docker 地址有内部地址请注意替换

1 laravel 安装 hprose

composer require hprose/hprose

php artisan rpc:server:start

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Service\RpcGoodsSerivce;
use Hprose\Socket\Server;

class RpcServerCommand extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rpc:server:start';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'rpc服务启动';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @throws \App\Wechat\WechatException
     */
    public function handle()
    {
        $server = new Server("tcp://0.0.0.0:1314");
        $server->setErrorTypes(E_ALL);
        $server->setDebugEnabled();
        $server->addFunction(function ($a) use ($server) {
            $promise = (new RpcGoodsSerivce())->test($a);
            return $promise;
        }, 'test');
//        $server->addMethod('test', $this);
        $server->addMethod('testb', $this);
        $server->start();
    }

    public function testb($str)
    {
        return (new RpcGoodsSerivce())->testb($str);
    }

}
app/Console/Kernel.php 中添加这类
RpcServerCommand::class,

RpcGoodsSerivce

<?php

namespace App\Service;

use Hprose\Http\Server;

class RpcGoodsSerivce
{
    public function init()
    {
        $server = new Server();
        $server->setCrossDomainEnabled();
        $server->addAccessControlAllowOrigin('*');
        $server->addMethod('test', $this);
        $server->addMethod('testa', $this);
        $server->addMethod('testb', $this);
        $server->addMethod('testdddddd', $this);
        $server->start();
    }

    /**
     * @param $str
     * @return string
     * var client = hprose.Client.create("http://127.0.0.1:8087/api/v1/rpc/wms",["test"]);
     * client.test(d"World!")
     * .then(function(result) {
     * console.log(result);
     * }, function(err) {
     * console.log(err);
     * });
     */
    public function test($str)
    {
        return 'hello' . rand(0, 1000) . $str;
    }

    public function testa($str, array $json)
    {
        return 'helloa' . $str . \GuzzleHttp\json_encode($json);
    }

    public function testb($str)
    {
        return 'hellob' . $str;
    }

    public function testc($str)
    {
        return 'hellov';
    }

    public function testdddddd($str)
    {
        return 'hellovadcasdcasd';
    }


}

routes/api.php

$router->post('rpc/wms', 'RpcController@rpcTest');
$router->post('rpc/gethttp', 'RpcController@rpcGetHttp');
$router->post('rpc/gettcp', 'RpcController@rpcGetTcp');

app/Http/Controllers/API/RpcController.php

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Service\RpcGoodsSerivce;
use Hprose\Http\Client as HttpClient;
use Hprose\Client as TcpClient;
use Illuminate\Http\Request;

class RpcController extends Controller
{
    /**
     * http 启动
     * @param Request $request
     */
    public function rpcTest(Request $request)
    {
        $server = new RpcGoodsSerivce();
        $server->init();   //开启服务
    }

    public function rpcGetHttp(Request $request)
    {
        $user = new HttpClient('http://mynginx:8087/api/v1/rpc/wms', false);
        $res[] = $user->isIdempotent();

        $res[] = $user->testa('aaa', array('aaa' => 'ccccc'));
        $res[] = $user->testb('aaa');
        $res[] = $user->testdddddd('aaa');
        return $this->response($res);
    }

    public function rpcGetTcp(Request $request)
    {
        //TODO php artisan rpc:server:start
        $user = TcpClient::create('tcp://127.0.0.1:1314/', false);
        $res[] = $user->test('aaaaaa');
        $res[] = $user->testb('bbbbb');
        return $this->response($res);
    }

}

2后台常驻运行 supervisor

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install -U pip
pip install supervisor
apt-get install supervisor

3 后台常驻运行 supervisor 配置文件

配置文件 /etc/supervisord/conf.d/rpc.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /usr/share/nginx/www/artisan rpc:server:start
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/supervisor/laravel-queue.log

启动命令

cd /etc/supervisord supervisord -c supervisor.conf

如果启动错误 删除 /var/run/supervisor.sock

常用命令

supervisorctl update
supervisorctl reload
supervisorctl
supervisorctl stop all

4 查看效果

转载于:https://my.oschina.net/Alexmyj/blog/2875914

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值