Compare commits
No commits in common. "7e30cfa91511693a25012b87fe2eaa428eecdc8e" and "60bc5943ff71ea815057b8aeeca80e1e201a83e2" have entirely different histories.
7e30cfa915
...
60bc5943ff
771
composer.lock
generated
771
composer.lock
generated
File diff suppressed because it is too large
Load Diff
|
@ -45,9 +45,5 @@ return [
|
||||||
'local' => true,
|
'local' => true,
|
||||||
'base_uri' => env('RPC_PAYMENT_URI','http://payapi.dev.haowumc.com'),
|
'base_uri' => env('RPC_PAYMENT_URI','http://payapi.dev.haowumc.com'),
|
||||||
],
|
],
|
||||||
'wx-server' => [
|
|
||||||
'local' => true,
|
|
||||||
'base_uri' => env('RPC_WX_SERVER_URI', 'http://sapi.in.haowumc.com'),
|
|
||||||
],
|
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
|
@ -7,7 +7,6 @@ use JsonRpc\Exception\RpcServerException;
|
||||||
use JsonRpc\Server\JsonRpcBase;
|
use JsonRpc\Server\JsonRpcBase;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
|
|
||||||
class Client extends JsonRpc
|
class Client extends JsonRpc
|
||||||
{
|
{
|
||||||
|
@ -28,11 +27,6 @@ class Client extends JsonRpc
|
||||||
*/
|
*/
|
||||||
protected $http;
|
protected $http;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var LoggerInterface
|
|
||||||
*/
|
|
||||||
protected $logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* which server rpc call choose
|
* which server rpc call choose
|
||||||
* @var array
|
* @var array
|
||||||
|
@ -45,11 +39,6 @@ class Client extends JsonRpc
|
||||||
$this->id = 1;
|
$this->id = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setLogger($logger)
|
|
||||||
{
|
|
||||||
$this->logger = $logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param $k
|
* @param $k
|
||||||
|
@ -71,7 +60,6 @@ class Client extends JsonRpc
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $arguments
|
* @param $arguments
|
||||||
* @param $options []
|
|
||||||
* @throws RpcServerException
|
* @throws RpcServerException
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
@ -104,23 +92,19 @@ class Client extends JsonRpc
|
||||||
*/
|
*/
|
||||||
protected function post($payload, $options = [])
|
protected function post($payload, $options = [])
|
||||||
{
|
{
|
||||||
$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 {
|
try {
|
||||||
$headers = [
|
$headers = [
|
||||||
'X-Client-App' => $this->config['app'],
|
'X-Client-App' => $this->config['app'],
|
||||||
'X-Request-Id' => $requestId,
|
|
||||||
];
|
];
|
||||||
$this->logger && $this->logger->info("client_request", array_merge($this->server_config, $payload));
|
app('rpc.logger')->info("client_request", array_merge($this->server_config, $payload));
|
||||||
$resp = $this->http->request('POST', $uri, array_merge([
|
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', array_merge([
|
||||||
'headers' => $headers,
|
'headers' => $headers,
|
||||||
'json' => $payload,
|
'json' => $payload,
|
||||||
], $options));
|
], $options));
|
||||||
} catch (ServerException $e) {
|
} catch (ServerException $e) {
|
||||||
$ex = new RpcServerException(self::ErrorMsg[JsonRpc::Rpc_Error_Internal_Error], JsonRpc::Rpc_Error_Internal_Error);
|
$ex = new RpcServerException(self::ErrorMsg[JsonRpc::Rpc_Error_Internal_Error], JsonRpc::Rpc_Error_Internal_Error);
|
||||||
if (function_exists('env') && env("APP_DEBUG") == true) {
|
if (env("APP_DEBUG") == true) {
|
||||||
|
$resp = $e->getResponse();
|
||||||
$ex->setResponse($e->getResponse());
|
$ex->setResponse($e->getResponse());
|
||||||
}
|
}
|
||||||
throw $ex;
|
throw $ex;
|
||||||
|
@ -128,7 +112,7 @@ class Client extends JsonRpc
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$body = \GuzzleHttp\json_decode($resp->getBody(), true);
|
$body = \GuzzleHttp\json_decode($resp->getBody(), true);
|
||||||
$this->logger && $this->logger->info("client_response", $body);
|
app('rpc.logger')->info("client_response", $body);
|
||||||
if (empty($body)) {
|
if (empty($body)) {
|
||||||
throw new RpcServerException('http response empty', JsonRpc::Rpc_Error_System_Error);
|
throw new RpcServerException('http response empty', JsonRpc::Rpc_Error_System_Error);
|
||||||
}
|
}
|
||||||
|
@ -141,9 +125,9 @@ class Client extends JsonRpc
|
||||||
return $body['result'];
|
return $body['result'];
|
||||||
|
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (\InvalidArgumentException $e) {
|
||||||
$this->logger && $this->logger->error('client_decode_error', array_merge($this->server_config, $payload));
|
app('rpc.logger')->error('client_decode_error', array_merge($this->server_config, $payload));
|
||||||
$ex = new RpcServerException($e->getMessage(), JsonRpc::Rpc_Error_Parse_Error);
|
$ex = new RpcServerException($e->getMessage(), JsonRpc::Rpc_Error_Parse_Error);
|
||||||
if (function_exists('env') && env("APP_DEBUG") == true) {
|
if (env("APP_DEBUG") == true) {
|
||||||
$ex->setResponse($resp);
|
$ex->setResponse($resp);
|
||||||
}
|
}
|
||||||
throw $ex;
|
throw $ex;
|
||||||
|
|
|
@ -40,7 +40,7 @@ class Security
|
||||||
*/
|
*/
|
||||||
private function isClientIPPermitted($ip)
|
private function isClientIPPermitted($ip)
|
||||||
{
|
{
|
||||||
if (app()->environment('develop', 'local')) {
|
if (!app()->environment('production', 'staging')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,7 @@ use Monolog\Logger;
|
||||||
class BaseServiceProvider extends ServiceProvider
|
class BaseServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $logger;
|
public function boot(){
|
||||||
|
|
||||||
public function boot()
|
|
||||||
{
|
|
||||||
Request::setTrustedProxies([
|
Request::setTrustedProxies([
|
||||||
//pod network
|
//pod network
|
||||||
'172.20.0.0/16',
|
'172.20.0.0/16',
|
||||||
|
@ -32,26 +29,26 @@ class BaseServiceProvider extends ServiceProvider
|
||||||
protected function setupConfig()
|
protected function setupConfig()
|
||||||
{
|
{
|
||||||
$source = realpath(__DIR__ . '/../../config/rpc.php');
|
$source = realpath(__DIR__ . '/../../config/rpc.php');
|
||||||
$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) {
|
|
||||||
// $this->app->configure('rpc');
|
|
||||||
// }
|
|
||||||
// var_dump($this->app instanceof LumenApplication); // false
|
|
||||||
// exit();
|
|
||||||
$this->mergeConfigFrom($source, 'rpc');
|
|
||||||
|
|
||||||
|
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
|
||||||
|
$this->publishes([$source => config_path('rpc.php')], 'rpc');
|
||||||
|
} elseif ($this->app instanceof LumenApplication) {
|
||||||
|
$this->app->configure('rpc');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->mergeConfigFrom($source, 'rpc');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->setupConfig();
|
$this->setupConfig();
|
||||||
$this->logger = new Logger('RPC.LOGGER');
|
$this->app->singleton("rpc.logger", function () {
|
||||||
$config = config('rpc');
|
$config = config('rpc');
|
||||||
$stream = new StreamHandler($config['log_path']);
|
$stream = new StreamHandler($config['log_path']);
|
||||||
$stream->setFormatter(new $config['log_formatter']());
|
$stream->setFormatter(new $config['log_formatter']());
|
||||||
$this->logger->pushHandler($stream);
|
$logger = new Logger('RPC.LOGGER');
|
||||||
|
return $logger->pushHandler($stream);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -16,14 +16,13 @@ class ClientServiceProvider extends BaseServiceProvider
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
parent::register();
|
parent::register();
|
||||||
|
$this->app->configure('rpc');
|
||||||
$config = config('rpc');
|
$config = config('rpc');
|
||||||
if (!is_array($config)) {
|
if (!is_array($config)) {
|
||||||
throw new RpcServerException("Application's Rpc Client Config Undefind", 500);
|
throw new RpcServerException("Application's Rpc Client Config Undefind", 500);
|
||||||
}
|
}
|
||||||
$this->app->singleton('rpc', function () use ($config) {
|
$this->app->singleton('rpc', function () use ($config) {
|
||||||
$client = new Client($config);
|
return new Client($config);
|
||||||
$client->setLogger($this->logger);
|
|
||||||
return $client;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
foreach ($config['client'] as $k => $item) {
|
foreach ($config['client'] as $k => $item) {
|
||||||
|
|
|
@ -40,7 +40,6 @@ class LumenServerServiceProvider extends BaseServiceProvider
|
||||||
}
|
}
|
||||||
$callback = function () use ($config) {
|
$callback = function () use ($config) {
|
||||||
$server = new JsonRpcServer($config);
|
$server = new JsonRpcServer($config);
|
||||||
$server->setLogger($this->logger);
|
|
||||||
return $server->handler();
|
return $server->handler();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ namespace JsonRpc\Server;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use JsonRpc\JsonRpc;
|
use JsonRpc\JsonRpc;
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
|
|
||||||
class JsonRpcServer extends JsonRpc
|
class JsonRpcServer extends JsonRpc
|
||||||
{
|
{
|
||||||
|
@ -14,19 +13,13 @@ class JsonRpcServer extends JsonRpc
|
||||||
* @var Request
|
* @var Request
|
||||||
*/
|
*/
|
||||||
public $request;
|
public $request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var LoggerInterface
|
* @var config 配置
|
||||||
*/
|
|
||||||
protected $logger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array 配置
|
|
||||||
*/
|
*/
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array rpc.server.map rpc方法
|
* @var rpc.server.map rpc方法
|
||||||
*/
|
*/
|
||||||
protected $map;
|
protected $map;
|
||||||
|
|
||||||
|
@ -37,22 +30,18 @@ class JsonRpcServer extends JsonRpc
|
||||||
$this->map = $config['map'];
|
$this->map = $config['map'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setLogger($logger){
|
|
||||||
$this->logger = $logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function handler()
|
public function handler()
|
||||||
{
|
{
|
||||||
if ($this->request->getContentType() != 'json') {
|
// if ($this->request->getContentType() != 'json') {
|
||||||
return $this->error(self::Rpc_Error_Invalid_Request);
|
// return $this->error(self::Rpc_Error_Invalid_Request);
|
||||||
}
|
// }
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if ($this->request->method() == Request::METHOD_GET) {
|
if ($this->request->method() == Request::METHOD_GET) {
|
||||||
$method = $this->request->input('method');
|
$method = $this->request->input('method');
|
||||||
$id = $this->request->input('id');
|
$id = $this->request->input('id');
|
||||||
$params = \GuzzleHttp\json_decode($this->request->input('params'), true);
|
$params = \GuzzleHttp\json_decode($this->request->input('params'),true);
|
||||||
} else {
|
} else {
|
||||||
list($method, $params, $id) = $this->parseJson($this->request->getContent());
|
list($method, $params, $id) = $this->parseJson($this->request->getContent());
|
||||||
}
|
}
|
||||||
|
@ -67,9 +56,9 @@ class JsonRpcServer extends JsonRpc
|
||||||
return $this->error(self::Rpc_Error_Invalid_Params);
|
return $this->error(self::Rpc_Error_Invalid_Params);
|
||||||
}
|
}
|
||||||
$this->request->attributes->add(['tunnel_method' => $method, 'tunnel_params' => $params]);
|
$this->request->attributes->add(['tunnel_method' => $method, 'tunnel_params' => $params]);
|
||||||
$this->logger && $this->logger->info('server', [$id, $class, $method, $params]);
|
app('rpc.logger')->info('server', [$id, $class,$method, $params]);
|
||||||
$ret = call_user_func_array([(new $class($id, $this->request)), $function], $params);
|
$ret = call_user_func_array([(new $class($id, $this->request)), $function], $params);
|
||||||
$this->logger && $this->logger->info('server_result', [$id, $ret]);
|
app('rpc.logger')->info('server_result', [$id, $ret]);
|
||||||
|
|
||||||
return JsonResponse::create($ret);
|
return JsonResponse::create($ret);
|
||||||
|
|
||||||
|
@ -78,11 +67,6 @@ class JsonRpcServer extends JsonRpc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理json rpc post body
|
|
||||||
* @param $data
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function parseJson($data)
|
protected function parseJson($data)
|
||||||
{
|
{
|
||||||
$data = \GuzzleHttp\json_decode($data, true);
|
$data = \GuzzleHttp\json_decode($data, true);
|
||||||
|
@ -92,23 +76,36 @@ class JsonRpcServer extends JsonRpc
|
||||||
return [$method, $params, $id];
|
return [$method, $params, $id];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据method解析出对应的class
|
|
||||||
* @param $method
|
|
||||||
* @return array|mixed
|
|
||||||
*/
|
|
||||||
protected function parseMethodWithMap($method)
|
protected function parseMethodWithMap($method)
|
||||||
{
|
{
|
||||||
return isset($this->map[$method]) ? $this->map[$method] : ['', ''];
|
return isset($this->map[$method]) ? $this->map[$method] : ['', ''];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查调用方式是否足够
|
* thisis
|
||||||
* @param $class
|
* @param string $method 参数名称
|
||||||
* @param $method
|
* @return array 返回结果
|
||||||
* @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)
|
protected function isEnoughParameter($class, $method, $parameters)
|
||||||
{
|
{
|
||||||
$r = new \ReflectionMethod($class, $method);
|
$r = new \ReflectionMethod($class, $method);
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
<?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,19 +1,8 @@
|
||||||
<?php
|
<?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',
|
|
||||||
]
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$client->endpoint('default');
|
$res = $client->call('math.subtract',['12','23',500]);
|
||||||
|
|
||||||
try {
|
var_dump($res);
|
||||||
$res = $client->call('math.subtract', ['12', '23', 500]);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
var_dump($e->getMessage());
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user