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