更改一些实现方式,减少直接对lumen的依赖
This commit is contained in:
parent
ac3f7bb737
commit
2341f068d3
783
composer.lock
generated
783
composer.lock
generated
File diff suppressed because it is too large
Load Diff
|
@ -7,6 +7,7 @@ use JsonRpc\Exception\RpcServerException;
|
|||
use JsonRpc\Server\JsonRpcBase;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Client extends JsonRpc
|
||||
{
|
||||
|
@ -27,6 +28,11 @@ class Client extends JsonRpc
|
|||
*/
|
||||
protected $http;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* which server rpc call choose
|
||||
* @var array
|
||||
|
@ -39,6 +45,11 @@ class Client extends JsonRpc
|
|||
$this->id = 1;
|
||||
}
|
||||
|
||||
public function setLogger($logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $k
|
||||
|
@ -60,6 +71,7 @@ class Client extends JsonRpc
|
|||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @param $options []
|
||||
* @throws RpcServerException
|
||||
* @return array
|
||||
*/
|
||||
|
@ -92,21 +104,23 @@ class Client extends JsonRpc
|
|||
*/
|
||||
protected function post($payload, $options = [])
|
||||
{
|
||||
$uri = 'rpc/json-rpc-v2.json?app='.$this->config['app'];
|
||||
$uri = 'rpc/json-rpc-v2.json?app=' . $this->config['app'];
|
||||
|
||||
$requestId = isset($_SERVER['HTTP_X_REQUEST_ID']) ? $_SERVER['HTTP_X_REQUEST_ID'] : 'nginx-config-err';
|
||||
|
||||
try {
|
||||
$headers = [
|
||||
'X-Client-App' => $this->config['app'],
|
||||
'X-Request-Id' => app('request')->header('X-Request-Id')
|
||||
'X-Request-Id' => $requestId,
|
||||
];
|
||||
app('rpc.logger')->info("client_request", array_merge($this->server_config, $payload));
|
||||
$this->logger && $this->logger->info("client_request", array_merge($this->server_config, $payload));
|
||||
$resp = $this->http->request('POST', $uri, array_merge([
|
||||
'headers' => $headers,
|
||||
'json' => $payload,
|
||||
], $options));
|
||||
} catch (ServerException $e) {
|
||||
$ex = new RpcServerException(self::ErrorMsg[JsonRpc::Rpc_Error_Internal_Error], JsonRpc::Rpc_Error_Internal_Error);
|
||||
if (env("APP_DEBUG") == true) {
|
||||
$resp = $e->getResponse();
|
||||
if (function_exists('env') && env("APP_DEBUG") == true) {
|
||||
$ex->setResponse($e->getResponse());
|
||||
}
|
||||
throw $ex;
|
||||
|
@ -114,7 +128,7 @@ class Client extends JsonRpc
|
|||
|
||||
try {
|
||||
$body = \GuzzleHttp\json_decode($resp->getBody(), true);
|
||||
app('rpc.logger')->info("client_response", $body);
|
||||
$this->logger && $this->logger->info("client_response", $body);
|
||||
if (empty($body)) {
|
||||
throw new RpcServerException('http response empty', JsonRpc::Rpc_Error_System_Error);
|
||||
}
|
||||
|
@ -127,9 +141,9 @@ class Client extends JsonRpc
|
|||
return $body['result'];
|
||||
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
app('rpc.logger')->error('client_decode_error', array_merge($this->server_config, $payload));
|
||||
$this->logger && $this->logger->error('client_decode_error', array_merge($this->server_config, $payload));
|
||||
$ex = new RpcServerException($e->getMessage(), JsonRpc::Rpc_Error_Parse_Error);
|
||||
if (env("APP_DEBUG") == true) {
|
||||
if (function_exists('env') && env("APP_DEBUG") == true) {
|
||||
$ex->setResponse($resp);
|
||||
}
|
||||
throw $ex;
|
||||
|
|
|
@ -11,7 +11,10 @@ use Monolog\Logger;
|
|||
class BaseServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
public function boot(){
|
||||
protected $logger;
|
||||
|
||||
public function boot()
|
||||
{
|
||||
Request::setTrustedProxies([
|
||||
//pod network
|
||||
'172.20.0.0/16',
|
||||
|
@ -29,7 +32,7 @@ class BaseServiceProvider extends ServiceProvider
|
|||
protected function setupConfig()
|
||||
{
|
||||
$source = realpath(__DIR__ . '/../../config/rpc.php');
|
||||
$this->app->configure('rpc');
|
||||
$this->app->configure('rpc');
|
||||
// if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
|
||||
// $this->publishes([$source => config_path('rpc.php')], 'rpc');
|
||||
// } elseif ($this->app instanceof LumenApplication) {
|
||||
|
@ -44,13 +47,11 @@ class BaseServiceProvider extends ServiceProvider
|
|||
public function register()
|
||||
{
|
||||
$this->setupConfig();
|
||||
$this->app->singleton("rpc.logger", function () {
|
||||
$config = config('rpc');
|
||||
$stream = new StreamHandler($config['log_path']);
|
||||
$stream->setFormatter(new $config['log_formatter']());
|
||||
$logger = new Logger('RPC.LOGGER');
|
||||
return $logger->pushHandler($stream);
|
||||
});
|
||||
$this->logger = new Logger('RPC.LOGGER');
|
||||
$config = config('rpc');
|
||||
$stream = new StreamHandler($config['log_path']);
|
||||
$stream->setFormatter(new $config['log_formatter']());
|
||||
$this->logger->pushHandler($stream);
|
||||
}
|
||||
|
||||
}
|
|
@ -20,8 +20,10 @@ class ClientServiceProvider extends BaseServiceProvider
|
|||
if (!is_array($config)) {
|
||||
throw new RpcServerException("Application's Rpc Client Config Undefind", 500);
|
||||
}
|
||||
$this->app->singleton('rpc', function () use ($config) {
|
||||
return new Client($config);
|
||||
$this->app->singleton('rpc', function () use ($config) {
|
||||
$client = new Client($config);
|
||||
$client->setLogger($this->logger);
|
||||
return $client;
|
||||
});
|
||||
|
||||
foreach ($config['client'] as $k => $item) {
|
||||
|
|
|
@ -40,6 +40,7 @@ class LumenServerServiceProvider extends BaseServiceProvider
|
|||
}
|
||||
$callback = function () use ($config) {
|
||||
$server = new JsonRpcServer($config);
|
||||
$server->setLogger($this->logger);
|
||||
return $server->handler();
|
||||
};
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace JsonRpc\Server;
|
|||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use JsonRpc\JsonRpc;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class JsonRpcServer extends JsonRpc
|
||||
{
|
||||
|
@ -13,13 +14,19 @@ class JsonRpcServer extends JsonRpc
|
|||
* @var Request
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* @var config 配置
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var array 配置
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var rpc.server.map rpc方法
|
||||
* @var array rpc.server.map rpc方法
|
||||
*/
|
||||
protected $map;
|
||||
|
||||
|
@ -30,18 +37,22 @@ class JsonRpcServer extends JsonRpc
|
|||
$this->map = $config['map'];
|
||||
}
|
||||
|
||||
public function setLogger($logger){
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function handler()
|
||||
{
|
||||
// if ($this->request->getContentType() != 'json') {
|
||||
// return $this->error(self::Rpc_Error_Invalid_Request);
|
||||
// }
|
||||
if ($this->request->getContentType() != 'json') {
|
||||
return $this->error(self::Rpc_Error_Invalid_Request);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
if ($this->request->method() == Request::METHOD_GET) {
|
||||
$method = $this->request->input('method');
|
||||
$id = $this->request->input('id');
|
||||
$params = \GuzzleHttp\json_decode($this->request->input('params'),true);
|
||||
$params = \GuzzleHttp\json_decode($this->request->input('params'), true);
|
||||
} else {
|
||||
list($method, $params, $id) = $this->parseJson($this->request->getContent());
|
||||
}
|
||||
|
@ -56,9 +67,9 @@ class JsonRpcServer extends JsonRpc
|
|||
return $this->error(self::Rpc_Error_Invalid_Params);
|
||||
}
|
||||
$this->request->attributes->add(['tunnel_method' => $method, 'tunnel_params' => $params]);
|
||||
app('rpc.logger')->info('server', [$id, $class,$method, $params]);
|
||||
$this->logger && $this->logger->info('server', [$id, $class, $method, $params]);
|
||||
$ret = call_user_func_array([(new $class($id, $this->request)), $function], $params);
|
||||
app('rpc.logger')->info('server_result', [$id, $ret]);
|
||||
$this->logger && $this->logger->info('server_result', [$id, $ret]);
|
||||
|
||||
return JsonResponse::create($ret);
|
||||
|
||||
|
@ -67,6 +78,11 @@ class JsonRpcServer extends JsonRpc
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理json rpc post body
|
||||
* @param $data
|
||||
* @return array
|
||||
*/
|
||||
protected function parseJson($data)
|
||||
{
|
||||
$data = \GuzzleHttp\json_decode($data, true);
|
||||
|
@ -76,36 +92,23 @@ class JsonRpcServer extends JsonRpc
|
|||
return [$method, $params, $id];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据method解析出对应的class
|
||||
* @param $method
|
||||
* @return array|mixed
|
||||
*/
|
||||
protected function parseMethodWithMap($method)
|
||||
{
|
||||
return isset($this->map[$method]) ? $this->map[$method] : ['', ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* thisis
|
||||
* @param string $method 参数名称
|
||||
* @return array 返回结果
|
||||
* 检查调用方式是否足够
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param $parameters
|
||||
* @return bool
|
||||
*/
|
||||
// protected function parseMethod($method)
|
||||
// {
|
||||
// $method = explode('.', $method);
|
||||
//
|
||||
// if (count($method) < 2) {
|
||||
// return ['', ''];
|
||||
// }
|
||||
//
|
||||
// $function = array_pop($method);
|
||||
// $class = 'Rpc' . ucwords(array_pop($method));
|
||||
//
|
||||
// foreach ($method as $one) {
|
||||
// $class = ucwords($one) . '\\' . $class;
|
||||
// }
|
||||
//
|
||||
// $class = "App\Rpc\\$class";
|
||||
// return [$class, $function];
|
||||
// }
|
||||
|
||||
|
||||
protected function isEnoughParameter($class, $method, $parameters)
|
||||
{
|
||||
$r = new \ReflectionMethod($class, $method);
|
||||
|
|
25
tests/publish.php
Normal file
25
tests/publish.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
include_once dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
$client = new \JsonRpc\Client([
|
||||
'app' => 'abc',
|
||||
'client' => [
|
||||
'default' => [
|
||||
'base_uri' => 'http://localhost:8080',
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
$client->endpoint('default');
|
||||
|
||||
$json = json_encode([
|
||||
'order_id' => '123',
|
||||
'user_id' => '456',
|
||||
]);
|
||||
|
||||
try {
|
||||
$res = $client->call('topic.produce', ['abc', $json]);
|
||||
var_dump($res);
|
||||
} catch (Exception $e) {
|
||||
var_dump($e->getCode(),$e->getMessage());
|
||||
}
|
|
@ -1,8 +1,19 @@
|
|||
<?php
|
||||
include_once dirname(__DIR__).'/vendor/autoload.php';
|
||||
include_once dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
$client = new \JsonRpc\Client();
|
||||
$client = new \JsonRpc\Client([
|
||||
'app' => 'abc',
|
||||
'client' => [
|
||||
'default' => [
|
||||
'base_uri' => 'http://localhost:8080',
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
$res = $client->call('math.subtract',['12','23',500]);
|
||||
$client->endpoint('default');
|
||||
|
||||
var_dump($res);
|
||||
try {
|
||||
$res = $client->call('math.subtract', ['12', '23', 500]);
|
||||
} catch (Exception $e) {
|
||||
var_dump($e->getMessage());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user