PHP code example of wuzhc / swrpc

1. Go to this page and download the library: Download wuzhc/swrpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wuzhc / swrpc example snippets



use Swrpc\LogicService;
class SchoolService extends LogicService 
{
    public function getUserSchool($userID) {
        $name = $userID == 123 ? '火星' : '水星';
        return $name.'学校';
    }    
}


namespace SwrpcTests;
use Swrpc\Server;

$basePath = dirname(dirname(__FILE__));
rpc.pid',
];
$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);
$server->addService(\SwrpcTests\services\SchoolService::class); 
$server->start();


namespace SwrpcTests;
use Swrpc\Request;
use Swrpc\LogicService;
use Swrpc\Client;

class UserService extends LogicService
{
    public function getUserSchoolName()
    {
        $userID = 123;
        $module = 'School_Module'; //请求目标模块名称,需要和服务端定义的一致
        $client = Client::create($module, '127.0.0.1', 9501);
        return $client->send(Request::create('\SwrpcTests\services\SchoolService_getUserSchool', [$userID]));
    }
}

//调用
echo UserService::factory()->getUserSchoolName();

$options = [
    'worker_num'       => 10
    'pid_file'         => __DIR__ . '/swrpc.pid',
];
$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);

$options = [
    'enable_coroutine' => true,
    'pid_file'         => __DIR__ . '/swrpc.pid',
];
$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);

$client = \Swrpc\Client::create($module, '127.0.0.1', 9501);
return $client->send(SyncRequest::create('SchoolService_getUserSchool', [$userID]));

$client = \Swrpc\Client::create($module, '127.0.0.1', 9501);
return $client->send(AsyncRequest::create('SchoolService_getUserSchool', [$userID]));

//中间件除了用匿名函数定义,还可以用实现Swrpc\Middlewares\MiddlewareInterface接口的类
$middleware = function (\Swrpc\Request $request, Closure $next) {
    $start = microtime(true); //前置操作,记录请求开始时间
    $result = $next($request);
    echo '耗时:'.(microtime(true) - $start).PHP_EOL; //后置操作,记录请求结束时间,从而计算请求耗时
    return $result; //继续下个中间件的处理
};
$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);
$server->addService(SchoolService::class); 
$server->addMiddleware($middleware); //添加中间件
$server->start();

$middleware = function (\Swrpc\Request $request, Closure $next) {
    if (empty($request->getParams())) {
        return \Swrpc\Response::error('参数不能为空'); //提前返回,必须是Response类型
    }   
    return $next($request); 
};

$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);
$server->addService(SchoolService::class); //提供SchoolService所有public方法功能
$server->addService(AreaService::class); //提供AreaService所有public方法功能
$server->start();

$server = new Server('School_Module', '127.0.0.1', 9501, 1, $options);
$server->addRegister(new Consul());
$server->addService(SchoolService::class); 
$server->start();

$register = new Consul();
$client = \Swrpc\Client::createBalancer('School_Module', $register, \Swrpc\Client::STRATEGY_WEIGHT);
$result = $client->send(Request::create('SchoolService_getUserSchool', [$userID]);

class UserService extends LogicService
{
    public function getUserSchoolName()
    {
        $userID = 123;
        $module = 'School_Module'; //请求目标模块名称,需要和服务端定义的一致
        $client = Client::create($module, '127.0.0.1', 9501);
        return $client->send(Request::create('SchoolService_getUserSchool', [$userID], $this->getTracerContext(__FUNCTION__))); //getTracerContext()用于提供追踪上下文
    }
}

$users = UserService::factory()
    ->setModule('User_Module') //当前模块,用于调用链的起点
    ->setTracerUrl('http://127.0.0.1:9411/api/v2/spans') //zipkin链路追踪地址
    ->getUserSchoolName();

use Swrpc\Server;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('swrpc');
$logger->pushHandler(new StreamHandler(fopen('xxxx.log','w+'), Logger::DEBUG));

$server = new Server('127.0.0.1', 9501, ['enable_coroutine'=>true]);
$server->addService(UserService::class);
$server->addLogger($logger); //覆盖默认日志处理器
$server->start();

use Swrpc\Server;

$packer = new \Swrpc\Packer\SerializeLengthPacker();
$server = new Server('127.0.0.1', 9501, ['enable_coroutine'=>true]);
$server->addService(UserService::class); 
$server->addPacker($packer); //覆盖默认值

$options = [
    'ssl_cert_file' => __DIR__.'/config/ssl.crt',
    'ssl_key_file'  => __DIR__.'/config/ssl.key',
    'pid_file'      => __DIR__ . '/swrpc.pid',
];
$server = new Server('School_Module', '127.0.0.1', 9501, $options, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$server->addService(SchoolService::class); 
$server->start();
bash
php composer.phar 
bash
php phpunit.phar tests --debug