lorik
3 years ago
25 changed files with 300 additions and 518 deletions
-
27bin/server
-
15cert/private_key.pem
-
6cert/pub_key.pem
-
5composer.json
-
118composer.lock
-
55examples/client.php
-
46examples/coroutine.php
-
8examples/index.php
-
41examples/jwt.php
-
52examples/rsa_test.php
-
21examples/server.php
-
50examples/test.php
-
83src/Client.php
-
13src/Controller.php
-
84src/RpcServer.php
-
87src/Serializer.php
-
23src/Yee.php
-
10src/exception/BadRequestRpcException.php
-
10src/exception/ErrorRpcException.php
-
10src/exception/ForbiddenRpcException.php
-
10src/exception/MethodNotAllowedRpcException.php
-
10src/exception/NotFoundRpcException.php
-
10src/exception/RpcException.php
-
10src/exception/UnauthorizedRpcException.php
-
14systemd/airpc.service
@ -1,27 +0,0 @@ |
|||
#!/usr/bin/env php |
|||
<?PHP |
|||
/* |
|||
* @Descripttion: |
|||
* @version: |
|||
* @Author: Blobt |
|||
* @Date: 2020-07-30 09:48:18 |
|||
* @LastEditors: Blobt |
|||
* @LastEditTime: 2020-07-30 14:20:19 |
|||
*/ |
|||
|
|||
include __DIR__."/../vendor/autoload.php"; |
|||
|
|||
use blobt\airpc\RpcServer; |
|||
|
|||
if(!isset($argv[1])) |
|||
die("Error: {$argv[0]} {your enter point}.\n"); |
|||
|
|||
if(!file_exists($argv[1])) |
|||
die("Error: enter point {$argv[1]} not found.\n"); |
|||
|
|||
$server = new RpcServer("0.0.0.0", 5188); |
|||
$server->reactorNum = 1; |
|||
$server->workerNum = 1; |
|||
$server->entryPoint = $argv[1]; |
|||
$server->run(); |
|||
?> |
@ -1,15 +0,0 @@ |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIICXgIBAAKBgQDsldu7vayXD7RE5HEMsvlz/tkq36gBgY1Yz+R9dkZWNDdQTTFm |
|||
O2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P84nixA+vfzQjqTIlQViO0vB/a |
|||
gWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdmlNSFDFB6+MVrC93dRQIDAQAB |
|||
AoGBALhxeJZcIDLA7oSLKEuDOJEjDQGszhkmfCDSKstZKyrHmJwFIk8WYe8DDbbH |
|||
meAybaDJfGfieQ46lhwP/tgq8VEM7m/bhkhksXHTuKmPGfr53uev9Rp1+TKG1vFW |
|||
2c8MFMrDg3ZNy8lAfZmhIWUP9ietU5o836d0/3vJ+nBeSZwhAkEA+0luT5ijhfh4 |
|||
Jim81h0yKJFOeg/gjdHtefAWyruAGHZEKz+yLNTPJN0bjPtQAOqSr/Hjt9MmxzPa |
|||
HJ0DuvvgbQJBAPEF1mYmqmpBZTDc2zjOTXzVRsNO/bBcx/q9GkQydn/7BV6Dqr9Y |
|||
cgJV49CWcbZBHLkCxyidKDvZBzTh0b/vWTkCQH3AsLianXLk9lO63WqVb82Hcit5 |
|||
mJnAbcRxFybbkG4teU3fcW4oDYXYNPpnphDfUwtD1P7gZ26XD1b0aLKBbD0CQQC3 |
|||
sCY9Rfyc7GgNfFTLD1e46uZ6VUo8zglPDC3niboMQahO5vQLzoFdTE5Km03gg/E7 |
|||
A2X7iwKt0fKPGAd+SxDpAkEA6OG77IL8cjrlZKk4ilTCz8rkF4CtZWbDLi1IF40i |
|||
jaGlSLvVowHDywtjT8tYfpIH7HI55KS3gj88qAaF38qfRA== |
|||
-----END RSA PRIVATE KEY----- |
@ -1,6 +0,0 @@ |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq |
|||
36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 |
|||
4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm |
|||
lNSFDFB6+MVrC93dRQIDAQAB |
|||
-----END PUBLIC KEY----- |
@ -1,118 +0,0 @@ |
|||
{ |
|||
"_readme": [ |
|||
"This file locks the dependencies of your project to a known state", |
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", |
|||
"This file is @generated automatically" |
|||
], |
|||
"content-hash": "be484b9b2956b9cbaf4eac46f8dd344a", |
|||
"packages": [ |
|||
{ |
|||
"name": "firebase/php-jwt", |
|||
"version": "dev-master", |
|||
"source": { |
|||
"type": "git", |
|||
"url": "https://github.com/firebase/php-jwt.git", |
|||
"reference": "feb0e820b8436873675fd3aca04f3728eb2185cb" |
|||
}, |
|||
"dist": { |
|||
"type": "zip", |
|||
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/feb0e820b8436873675fd3aca04f3728eb2185cb", |
|||
"reference": "feb0e820b8436873675fd3aca04f3728eb2185cb", |
|||
"shasum": "", |
|||
"mirrors": [ |
|||
{ |
|||
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", |
|||
"preferred": true |
|||
} |
|||
] |
|||
}, |
|||
"require": { |
|||
"php": ">=5.3.0" |
|||
}, |
|||
"require-dev": { |
|||
"phpunit/phpunit": ">=4.8 <=9" |
|||
}, |
|||
"type": "library", |
|||
"autoload": { |
|||
"psr-4": { |
|||
"Firebase\\JWT\\": "src" |
|||
} |
|||
}, |
|||
"notification-url": "https://packagist.org/downloads/", |
|||
"license": [ |
|||
"BSD-3-Clause" |
|||
], |
|||
"authors": [ |
|||
{ |
|||
"name": "Neuman Vong", |
|||
"email": "neuman+pear@twilio.com", |
|||
"role": "Developer" |
|||
}, |
|||
{ |
|||
"name": "Anant Narayanan", |
|||
"email": "anant@php.net", |
|||
"role": "Developer" |
|||
} |
|||
], |
|||
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", |
|||
"homepage": "https://github.com/firebase/php-jwt", |
|||
"keywords": [ |
|||
"jwt", |
|||
"php" |
|||
], |
|||
"time": "2020-03-25T18:49:23+00:00" |
|||
}, |
|||
{ |
|||
"name": "swoole/ide-helper", |
|||
"version": "dev-master", |
|||
"source": { |
|||
"type": "git", |
|||
"url": "https://github.com/swoole/ide-helper.git", |
|||
"reference": "9082983c671cd79f461fcb9e52687dccca993e9b" |
|||
}, |
|||
"dist": { |
|||
"type": "zip", |
|||
"url": "https://api.github.com/repos/swoole/ide-helper/zipball/9082983c671cd79f461fcb9e52687dccca993e9b", |
|||
"reference": "9082983c671cd79f461fcb9e52687dccca993e9b", |
|||
"shasum": "", |
|||
"mirrors": [ |
|||
{ |
|||
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", |
|||
"preferred": true |
|||
} |
|||
] |
|||
}, |
|||
"require-dev": { |
|||
"guzzlehttp/guzzle": "~6.5.0", |
|||
"laminas/laminas-code": "~3.4.0", |
|||
"squizlabs/php_codesniffer": "~3.5.0", |
|||
"symfony/filesystem": "~4.0" |
|||
}, |
|||
"type": "library", |
|||
"notification-url": "https://packagist.org/downloads/", |
|||
"license": [ |
|||
"Apache-2.0" |
|||
], |
|||
"authors": [ |
|||
{ |
|||
"name": "Team Swoole", |
|||
"email": "team@swoole.com" |
|||
} |
|||
], |
|||
"description": "IDE help files for Swoole.", |
|||
"time": "2020-08-05T18:18:55+00:00" |
|||
} |
|||
], |
|||
"packages-dev": [], |
|||
"aliases": [], |
|||
"minimum-stability": "dev", |
|||
"stability-flags": { |
|||
"swoole/ide-helper": 20, |
|||
"firebase/php-jwt": 20 |
|||
}, |
|||
"prefer-stable": false, |
|||
"prefer-lowest": false, |
|||
"platform": [], |
|||
"platform-dev": [], |
|||
"plugin-api-version": "1.1.0" |
|||
} |
@ -1,55 +0,0 @@ |
|||
<?PHP |
|||
|
|||
/** |
|||
* { |
|||
* unisgned char type; |
|||
* unsigned int time; |
|||
* unsigned int serid; |
|||
* unsigned int length; |
|||
* char body[length]; |
|||
* } |
|||
*/ |
|||
function remoteCall(string $host, $port, string $data) { |
|||
$length = strlen($data); |
|||
$pack = pack('CNNNa*', 1, time(), 0, $length, $data); |
|||
$client = new Swoole\Client(SWOOLE_SOCK_TCP); |
|||
|
|||
$client->set(array( |
|||
'open_length_check' => true, |
|||
'package_max_length' => 1024 * 1024 * 20, |
|||
'package_length_type' => 'N', |
|||
'package_length_offset' => 9, |
|||
'package_body_offset' => 13 |
|||
)); |
|||
|
|||
if (!$client->connect($host, $port, 0.5)) { |
|||
return false; |
|||
} |
|||
|
|||
$client->send($pack); |
|||
$pack = @$client->recv(); |
|||
$client->close(); |
|||
|
|||
if ($pack === false) { |
|||
//echo $client->errCode . "\n";
|
|||
//TODO 获取错误码并进行处理
|
|||
return false; |
|||
} else if (empty($pack)) { |
|||
echo "close by peer\n"; |
|||
return false; |
|||
} |
|||
|
|||
$ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); |
|||
|
|||
return $ret['data']; |
|||
} |
|||
|
|||
//$data = '{"controller":"site","methor":"test","params":{"id":1}}';
|
|||
$data = file_get_contents("/mnt/hgfs/wdev/data.txt"); |
|||
$ret = remoteCall("127.0.0.1", "5188", $data); |
|||
|
|||
//'{"controller":"site","methor":"test","params":{"id":"111"}}
|
|||
//'{"controller":"site","methor":"test","params":{"id":1,"status":0}}'
|
|||
echo "{$ret}\n"; |
|||
|
|||
?>
|
@ -1,46 +0,0 @@ |
|||
<?php |
|||
|
|||
$host = '127.0.0.1'; |
|||
$port = 5188; |
|||
$data = '{"controller":"site","methor":"test","params":{"id":1}}'; |
|||
|
|||
|
|||
$length = strlen($data); |
|||
$pack = pack('CNNNa*', 1, time(), 0, $length, $data); |
|||
|
|||
$ret = Swoole\Coroutine::create(function()use($host, $port, $pack) { |
|||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP); |
|||
$client->set(array( |
|||
'open_length_check' => true, |
|||
'package_max_length' => 81920, |
|||
'package_length_type' => 'N', |
|||
'package_length_offset' => 9, |
|||
'package_body_offset' => 13, |
|||
'connect_timeout' => 1.0, |
|||
'write_timeout' => 60.0, |
|||
'read_timeout' => 60.0 |
|||
)); |
|||
|
|||
if (!$client->connect($host, $port)) { |
|||
echo "connect failed.\n"; |
|||
return false; |
|||
} |
|||
|
|||
|
|||
@$client->send($pack); |
|||
$pack = @$client->recv(); |
|||
if ($pack === false) { |
|||
echo $client->errCode . "\n"; |
|||
return false; |
|||
} else if (empty($pack)) { |
|||
echo "close by peer.\n"; |
|||
return false; |
|||
} |
|||
|
|||
$ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); |
|||
$client->close(); |
|||
return $ret['data']; |
|||
}); |
|||
|
|||
|
|||
print_r($ret); |
@ -1,8 +0,0 @@ |
|||
<?php |
|||
|
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
|
|||
use blobt\airpc\Yee; |
|||
|
|||
|
|||
return Yee::getInstaance()->run(); |
@ -1,41 +0,0 @@ |
|||
<?php |
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
use \Firebase\JWT\JWT; |
|||
|
|||
$key = "example_key"; |
|||
$payload = array( |
|||
"iss" => "http://example.org", |
|||
"aud" => "http://example.com", |
|||
"iat" => 1356999524, |
|||
"nbf" => 1357000000 |
|||
); |
|||
|
|||
/** |
|||
* IMPORTANT: |
|||
* You must specify supported algorithms for your application. See |
|||
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40 |
|||
* for a list of spec-compliant algorithms. |
|||
*/ |
|||
echo $jwt = JWT::encode($payload, $key); |
|||
$decoded = JWT::decode($jwt, $key, array('HS256')); |
|||
|
|||
print_r($decoded); |
|||
|
|||
/* |
|||
NOTE: This will now be an object instead of an associative array. To get |
|||
an associative array, you will need to cast it as such: |
|||
*/ |
|||
|
|||
$decoded_array = (array) $decoded; |
|||
|
|||
/** |
|||
* You can add a leeway to account for when there is a clock skew times between |
|||
* the signing and verifying servers. It is recommended that this leeway should |
|||
* not be bigger than a few minutes. |
|||
* |
|||
* Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
|
|||
*/ |
|||
JWT::$leeway = 60; // $leeway in seconds
|
|||
$decoded = JWT::decode($jwt, $key, array('HS256')); |
|||
|
|||
?>
|
@ -1,52 +0,0 @@ |
|||
<?php |
|||
|
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
|
|||
use \Firebase\JWT\JWT; |
|||
|
|||
$key = "73937393"; |
|||
|
|||
$privateKey = <<<EOD |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIICXgIBAAKBgQDsldu7vayXD7RE5HEMsvlz/tkq36gBgY1Yz+R9dkZWNDdQTTFm |
|||
O2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P84nixA+vfzQjqTIlQViO0vB/a |
|||
gWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdmlNSFDFB6+MVrC93dRQIDAQAB |
|||
AoGBALhxeJZcIDLA7oSLKEuDOJEjDQGszhkmfCDSKstZKyrHmJwFIk8WYe8DDbbH |
|||
meAybaDJfGfieQ46lhwP/tgq8VEM7m/bhkhksXHTuKmPGfr53uev9Rp1+TKG1vFW |
|||
2c8MFMrDg3ZNy8lAfZmhIWUP9ietU5o836d0/3vJ+nBeSZwhAkEA+0luT5ijhfh4 |
|||
Jim81h0yKJFOeg/gjdHtefAWyruAGHZEKz+yLNTPJN0bjPtQAOqSr/Hjt9MmxzPa |
|||
HJ0DuvvgbQJBAPEF1mYmqmpBZTDc2zjOTXzVRsNO/bBcx/q9GkQydn/7BV6Dqr9Y |
|||
cgJV49CWcbZBHLkCxyidKDvZBzTh0b/vWTkCQH3AsLianXLk9lO63WqVb82Hcit5 |
|||
mJnAbcRxFybbkG4teU3fcW4oDYXYNPpnphDfUwtD1P7gZ26XD1b0aLKBbD0CQQC3 |
|||
sCY9Rfyc7GgNfFTLD1e46uZ6VUo8zglPDC3niboMQahO5vQLzoFdTE5Km03gg/E7 |
|||
A2X7iwKt0fKPGAd+SxDpAkEA6OG77IL8cjrlZKk4ilTCz8rkF4CtZWbDLi1IF40i |
|||
jaGlSLvVowHDywtjT8tYfpIH7HI55KS3gj88qAaF38qfRA== |
|||
-----END RSA PRIVATE KEY----- |
|||
EOD; |
|||
|
|||
|
|||
$publicKey = <<<EOD |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq |
|||
36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 |
|||
4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm |
|||
lNSFDFB6+MVrC93dRQIDAQAB |
|||
-----END PUBLIC KEY----- |
|||
EOD; |
|||
|
|||
|
|||
$payload = [ |
|||
"iss" => "deeplove.cn", |
|||
"name" => "nemo", |
|||
"age" => 29, |
|||
|
|||
]; |
|||
|
|||
$jwt = JWT::encode($payload, $privateKey, 'RS256'); |
|||
echo strlen($jwt) . "\n"; |
|||
echo "Encode:\n" . print_r($jwt, true) . "\n"; |
|||
|
|||
$decoded = JWT::decode($jwt, $publicKey, array('RS256')); |
|||
|
|||
$decoded_array = (array) $decoded; |
|||
echo "Decode:\n" . print_r($decoded_array, true) . "\n"; |
@ -1,21 +0,0 @@ |
|||
<?PHP |
|||
/* |
|||
* @Descripttion: |
|||
* @version: |
|||
* @Author: sueRimn |
|||
* @Date: 2020-07-30 09:48:18 |
|||
* @LastEditors: sueRimn |
|||
* @LastEditTime: 2020-07-30 14:20:19 |
|||
*/ |
|||
|
|||
include __DIR__."/../vendor/autoload.php"; |
|||
|
|||
use blobt\airpc\RpcServer; |
|||
|
|||
$server = new RpcServer("0.0.0.0", 5188); |
|||
$server->reactorNum = 2; |
|||
$server->workerNum = 6; |
|||
$server->daemonize = false; |
|||
$server->entryPoint = '/home/blobt/Documents/dev/php/yii2-app-rpc/rpc/index.php'; |
|||
$server->run(); |
|||
?>
|
@ -1,50 +0,0 @@ |
|||
<?php |
|||
|
|||
include __DIR__ . "/../vendor/autoload.php"; |
|||
|
|||
use \Firebase\JWT\JWT; |
|||
|
|||
$privateKey = <<<EOD |
|||
-----BEGIN RSA PRIVATE KEY----- |
|||
MIICXgIBAAKBgQDsldu7vayXD7RE5HEMsvlz/tkq36gBgY1Yz+R9dkZWNDdQTTFm |
|||
O2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P84nixA+vfzQjqTIlQViO0vB/a |
|||
gWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdmlNSFDFB6+MVrC93dRQIDAQAB |
|||
AoGBALhxeJZcIDLA7oSLKEuDOJEjDQGszhkmfCDSKstZKyrHmJwFIk8WYe8DDbbH |
|||
meAybaDJfGfieQ46lhwP/tgq8VEM7m/bhkhksXHTuKmPGfr53uev9Rp1+TKG1vFW |
|||
2c8MFMrDg3ZNy8lAfZmhIWUP9ietU5o836d0/3vJ+nBeSZwhAkEA+0luT5ijhfh4 |
|||
Jim81h0yKJFOeg/gjdHtefAWyruAGHZEKz+yLNTPJN0bjPtQAOqSr/Hjt9MmxzPa |
|||
HJ0DuvvgbQJBAPEF1mYmqmpBZTDc2zjOTXzVRsNO/bBcx/q9GkQydn/7BV6Dqr9Y |
|||
cgJV49CWcbZBHLkCxyidKDvZBzTh0b/vWTkCQH3AsLianXLk9lO63WqVb82Hcit5 |
|||
mJnAbcRxFybbkG4teU3fcW4oDYXYNPpnphDfUwtD1P7gZ26XD1b0aLKBbD0CQQC3 |
|||
sCY9Rfyc7GgNfFTLD1e46uZ6VUo8zglPDC3niboMQahO5vQLzoFdTE5Km03gg/E7 |
|||
A2X7iwKt0fKPGAd+SxDpAkEA6OG77IL8cjrlZKk4ilTCz8rkF4CtZWbDLi1IF40i |
|||
jaGlSLvVowHDywtjT8tYfpIH7HI55KS3gj88qAaF38qfRA== |
|||
-----END RSA PRIVATE KEY----- |
|||
EOD; |
|||
|
|||
|
|||
$publicKey = <<<EOD |
|||
-----BEGIN PUBLIC KEY----- |
|||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsldu7vayXD7RE5HEMsvlz/tkq |
|||
36gBgY1Yz+R9dkZWNDdQTTFmO2tWFwBvYRx5Nv/CtR1Nl2+pb/Nd6WrWzs9FR7P8 |
|||
4nixA+vfzQjqTIlQViO0vB/agWa2nk5N4e0dsQ7kDr3+7/w1DeFsMlRBDvSHMsdm |
|||
lNSFDFB6+MVrC93dRQIDAQAB |
|||
-----END PUBLIC KEY----- |
|||
EOD; |
|||
|
|||
$data = [ |
|||
'id' => 23, |
|||
'name' => 'nemo' |
|||
]; |
|||
|
|||
|
|||
|
|||
//$encode = JWT::encode($data, $privateKey, 'RS256');
|
|||
$encode = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eeyJpZCI6MjMsIm5hbWUiOiJuZW1vIn0.xJt8NHFJeQyiWSmAI1C_t2lX99QHChXpR7iNkTQ5oi7_87NPo_S4gbC2zh40SOmv9uk3EoRvsrzzRbtPSeh7D5oCbSVXcqvVwMq0zwYBgW6vyL5DMkuDGyQQpyVGuywy8xY9N9bwUtiL7tVj_S22L1jaYOw7SZtGU4eDa5vU_DM"; |
|||
echo "encode: " . $encode . "\n"; |
|||
|
|||
|
|||
$decoded = (array) JWT::decode($encode, $publicKey, array('RS256')); |
|||
|
|||
print_r($decoded); |
|||
?>
|
@ -0,0 +1,83 @@ |
|||
<?php |
|||
namespace blobt\airpc; |
|||
|
|||
use blobt\airpc\exception\ErrorRpcException; |
|||
use Yii; |
|||
|
|||
class Client |
|||
{ |
|||
/** |
|||
* 远程调用 |
|||
* @param $service |
|||
* @param $controller |
|||
* @param $method |
|||
* @param $params |
|||
* @return array |
|||
*/ |
|||
public static function remoteCall($service, $controller, $method, $params) : array |
|||
{ |
|||
try{ |
|||
//找到service对应服务器
|
|||
$serviceData = self::serviceData($service); |
|||
//打包数据
|
|||
$data = [ |
|||
'controller' => $controller, |
|||
'method' => $method, |
|||
'params' => $params |
|||
]; |
|||
$data = json_encode($data); |
|||
$length = strlen($data); |
|||
$pack = pack('CNNNa*', 1, time(), 0, $length, $data); |
|||
|
|||
//TPC调用
|
|||
$client = new \Swoole\Client(SWOOLE_SOCK_TCP); |
|||
$client->set(array( |
|||
'open_length_check' => true, |
|||
'package_max_length' => 20971520, |
|||
'package_length_type' => 'N', |
|||
'package_length_offset' => 9, |
|||
'package_body_offset' => 13 |
|||
)); |
|||
if (!$client->connect($serviceData['host'],$serviceData['port'], 10)) |
|||
throw new ErrorRpcException('swoole connect failed', 88500002); |
|||
$client->send($pack); |
|||
$pack = @$client->recv(); |
|||
$client->close(); |
|||
|
|||
//解析数据
|
|||
if ($pack === false) { |
|||
throw new ErrorRpcException('client recv false , error code : '.$client->errCode, 88500003); |
|||
} else if (empty($pack)) |
|||
throw new ErrorRpcException('close by peer', 88500004); |
|||
$ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); |
|||
$decodeData = json_decode($ret['data'],true); |
|||
if( $decodeData == false ) |
|||
throw new ErrorRpcException('remote call result json decode failed', 88500005); |
|||
|
|||
return $decodeData; |
|||
}catch(\Throwable $e){ |
|||
return ['statusCode'=>500,'errorCode'=>$e->getCode(),'msg'=>$e->getMessage()]; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取微服务节点数据 |
|||
* @param $service |
|||
* @return array |
|||
* @throws ErrorRpcException |
|||
*/ |
|||
private static function serviceData($service): array |
|||
{ |
|||
if( isset(Yii::$app->params['services']) ){ |
|||
$servicesList = Yii::$app->params['services']; |
|||
if( isset($servicesList[$service]) && !empty($servicesList[$service]) ){ |
|||
$randomKey = array_rand($servicesList[$service],1); //随机取一个节点
|
|||
return $servicesList[$service][$randomKey]; |
|||
}else{ |
|||
throw new ErrorRpcException('services information not found', 88500006); |
|||
} |
|||
}else{ |
|||
throw new ErrorRpcException('services information not found', 88500006); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,13 @@ |
|||
<?php |
|||
namespace blobt\airpc; |
|||
|
|||
class Controller extends \yii\base\Controller |
|||
{ |
|||
public function afterAction($action, $result) |
|||
{ |
|||
$result = parent::afterAction($action, $result); |
|||
//格式化输出
|
|||
$serializer = new Serializer(); |
|||
return $serializer->serialize($result); |
|||
} |
|||
} |
@ -0,0 +1,87 @@ |
|||
<?php |
|||
namespace blobt\airpc; |
|||
|
|||
use yii\base\Arrayable; |
|||
use yii\data\DataProviderInterface; |
|||
use yii\data\Pagination; |
|||
|
|||
/** |
|||
* 重载部分yii\rest\Serializer方法,以适应RPC数据返回格式化 |
|||
* Class Serializer |
|||
* @package blobt\airpc |
|||
*/ |
|||
class Serializer extends \yii\rest\Serializer |
|||
{ |
|||
/** |
|||
* Serializes a data provider. |
|||
* @param DataProviderInterface $dataProvider |
|||
* @return array the array representation of the data provider. |
|||
*/ |
|||
protected function serializeDataProvider($dataProvider) |
|||
{ |
|||
if ($this->preserveKeys) { |
|||
$models = $dataProvider->getModels(); |
|||
} else { |
|||
$models = array_values($dataProvider->getModels()); |
|||
} |
|||
$models = $this->serializeModels($models); |
|||
|
|||
$pagination = $dataProvider->getPagination(); |
|||
|
|||
//因为tcp传输,所以不用获取分页链接
|
|||
// if (($pagination = $dataProvider->getPagination()) !== false) {
|
|||
// $this->addPaginationHeaders($pagination);
|
|||
// }
|
|||
|
|||
// if ($this->request->getIsHead()) {
|
|||
// return null;
|
|||
// } else
|
|||
if ($this->collectionEnvelope === null) { |
|||
return $models; |
|||
} |
|||
|
|||
$result = [ |
|||
$this->collectionEnvelope => $models, |
|||
]; |
|||
if ($pagination !== false) { |
|||
return array_merge($result, $this->serializePagination($pagination)); |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
/** |
|||
* Serializes a pagination into an array. |
|||
* @param Pagination $pagination |
|||
* @return array the array representation of the pagination |
|||
* @see addPaginationHeaders() |
|||
*/ |
|||
protected function serializePagination($pagination) |
|||
{ |
|||
return [ |
|||
// $this->linksEnvelope => Link::serialize($pagination->getLinks(true)),
|
|||
$this->metaEnvelope => [ |
|||
'totalCount' => $pagination->totalCount, |
|||
'pageCount' => $pagination->getPageCount(), |
|||
'currentPage' => $pagination->getPage() + 1, |
|||
'perPage' => $pagination->getPageSize(), |
|||
], |
|||
]; |
|||
} |
|||
|
|||
/** |
|||
* Serializes a model object. |
|||
* @param Arrayable $model |
|||
* @return array the array representation of the model |
|||
*/ |
|||
protected function serializeModel($model) |
|||
{ |
|||
// if ($this->request->getIsHead()) {
|
|||
// return null;
|
|||
// }
|
|||
|
|||
// list($fields, $expand) = $this->getRequestedFields();
|
|||
$fields = [];$expand=[]; |
|||
return $model->toArray($fields, $expand); |
|||
} |
|||
} |
@ -1,23 +0,0 @@ |
|||
<?php |
|||
|
|||
namespace blobt\airpc; |
|||
|
|||
use yii\console\controllers\HelpController; |
|||
|
|||
class Yee { |
|||
|
|||
private static $instance; |
|||
|
|||
public static function getInstaance() { |
|||
if (empty(self::$instance)) { |
|||
self::$instance = new self(); |
|||
} |
|||
return self::$instance; |
|||
} |
|||
|
|||
public function run() { |
|||
$ret = "some data ".$_SERVER['data']; |
|||
return $ret; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class BadRequestRpcException extends RpcException |
|||
{ |
|||
public $statusCode = 400; |
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class ErrorRpcException extends RpcException |
|||
{ |
|||
public $statusCode = 500; |
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class ForbiddenRpcException extends RpcException |
|||
{ |
|||
public $statusCode = 403; |
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class MethodNotAllowedRpcException extends RpcException |
|||
{ |
|||
public $statusCode = 405; |
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class NotFoundRpcException extends RpcException |
|||
{ |
|||
public $statusCode = 404; |
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class RpcException extends \Exception |
|||
{ |
|||
public $statusCode = 500; |
|||
} |
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace blobt\airpc\exception; |
|||
|
|||
|
|||
class UnauthorizedRpcException extends RpcException |
|||
{ |
|||
public $statusCode = 401; |
|||
} |
@ -1,14 +0,0 @@ |
|||
[Unit] |
|||
Description=AI Rpc Server |
|||
After=network.target |
|||
After=syslog.target |
|||
|
|||
[Service] |
|||
Type=simple |
|||
LimitNOFILE=65535 |
|||
ExecStart=/usr/bin/php {your server path} |
|||
ExecReload=/bin/kill -USR1 $MAINPID |
|||
Restart=always |
|||
|
|||
[Install] |
|||
WantedBy=multi-user.target graphical.target |
Write
Preview
Loading…
Cancel
Save
Reference in new issue