Compare commits

...

62 Commits
v1.0 ... main

Author SHA1 Message Date
xAmast
a3f0ba00b4 feat: set default timeout to 60s 2024-06-13 06:11:16 +08:00
xAmast
6bb15490de feat: remove repositories 2024-06-12 20:45:49 +08:00
候学杰
7e30cfa915 Merge branch 'master' of git.int.haowumc.com:composer/php-json-rpc
# Conflicts:
#	src/Client.php
2019-07-03 13:55:32 +08:00
候学杰
2341f068d3 更改一些实现方式,减少直接对lumen的依赖 2019-07-03 13:55:06 +08:00
dongwei
5930e7b9fc add x-request-id defautl if under no-http-request 2019-07-03 13:19:40 +08:00
候学杰
ac3f7bb737 Update Client.php 2019-04-23 15:17:14 +08:00
dongwei
9cbfd4850e fix bug 2019-03-18 18:11:54 +08:00
dongwei
f2ac2f4b38 fix bug 2019-03-18 18:10:53 +08:00
dongwei
ee2cdf45a3 fix bug 2019-03-18 18:08:23 +08:00
dongwei
4daf42ffd4 fix client merge config bug 2019-03-18 17:49:51 +08:00
dongwei
c9fa7757bd ip permison 2019-03-15 16:13:33 +08:00
dongwei
d959852a56 add X-Request-Id 2019-03-15 14:02:20 +08:00
dongwei
60bc5943ff add monitor switch & catch exception log to lumen. 2019-03-14 16:58:11 +08:00
董巍
d12d0486b9 Update TunnelMiddleware.php 2019-03-13 18:56:21 +08:00
dongwei
deb3dadbe4 fix bug 2019-03-13 16:15:07 +08:00
dongwei
438147dbd8 code 2019-03-13 15:42:45 +08:00
候学杰
47edd9968b Merge branch 'hotfix/insert-timeout-header' into 'master'
Hotfix/insert timeout header

See merge request composer/php-json-rpc!2
2019-03-11 16:07:04 +08:00
chujiu
52fac5a469 调用client 增加options 参数 2019-03-11 16:02:00 +08:00
chujiu
fde7194d8e 更改client,增加设置header头 2019-03-11 15:04:51 +08:00
dongwei
0d701a1167 remove content 2019-03-04 17:14:32 +08:00
dongwei
70c14f7f33 open tunnel 2019-02-20 15:45:08 +08:00
dongwei
12684966a6 change influxdb host 2019-02-20 15:44:34 +08:00
dongwei
c43325880b 增加超时时间和链接超时 2019-02-20 15:02:09 +08:00
dongwei
198318d9d0 close tunnel 2019-02-20 13:55:03 +08:00
dongwei
81b327c30d Merge branch 'master' of git.int.haowumc.com:composer/php-json-rpc 2019-02-19 15:33:08 +08:00
dongwei
481aa2b9a7 fix bug 2019-02-19 15:33:03 +08:00
董巍
ca37e45801 Update README.md 2019-02-19 15:18:13 +08:00
dongwei
4ccbf42052 update doc 2019-02-19 14:48:06 +08:00
dongwei
19ac4fe488 Merge branch 'master' of git.int.haowumc.com:composer/php-json-rpc 2019-02-19 14:35:47 +08:00
董巍
ebcfb60ea5 Merge branch 'feature/doc_tool' into 'master'
Feature/doc tool

See merge request composer/php-json-rpc!1
2019-02-19 14:35:35 +08:00
董巍
b3f5ec87b1 Merge branch 'feature/doc_tool' into 'master'
Feature/doc tool

See merge request composer/php-json-rpc!1
2019-02-15 18:45:07 +08:00
lisida
375e339439 push 2019-02-15 16:30:31 +08:00
张博轩
36651673d9 Merge branch 'feature/doc_tool' of git.int.haowumc.com:composer/php-json-rpc into feature/doc_tool 2019-02-15 14:33:56 +08:00
张博轩
ea6a900d1e 添加locoal 2019-02-15 14:33:47 +08:00
lisida
3710712ebb push 2019-02-15 14:29:16 +08:00
dongwei
2f9f47b96a update aliyun ip limit 2019-02-15 12:52:24 +08:00
dongwei
8ba0e9a430 add ip limit 2019-02-15 12:37:38 +08:00
lisida
306b6717ed push 2019-02-14 19:14:13 +08:00
lisida
d7164ebb8e no message 2019-02-14 19:11:41 +08:00
lisida
82034913bd push 2019-02-14 19:07:42 +08:00
lisida
853b20b6d4 push 2019-02-14 18:42:07 +08:00
张博轩
7a4b3b9be4 修改table 2019-02-14 18:34:22 +08:00
lisida
5a670b51d3 push 2019-02-14 18:15:22 +08:00
lisida
d5d06d3b84 push 2019-02-14 18:08:21 +08:00
lisida
e9ba9fae73 test 2019-02-14 17:41:52 +08:00
dongwei
c27fac903f status 生成value统计 2019-02-12 16:08:31 +08:00
dongwei
710313a1b3 生产环境不产生 2019-02-12 15:53:33 +08:00
dongwei
301271771e remove some logger 2019-02-12 15:11:47 +08:00
dongwei
54c80c06f3 add value 2019-02-12 14:35:02 +08:00
dongwei
777afdd1e0 open tunnel 2019-01-31 15:47:09 +08:00
dongwei
4e2a2a66be add log 2019-01-31 15:38:57 +08:00
dongwei
d28d9973c1 Merge branch 'feature/tunnel' 2019-01-31 15:06:57 +08:00
dongwei
8a180e0df7 push monitor to influxdb 2019-01-31 15:06:30 +08:00
候学杰
c4354676f9 ip网段 2019-01-30 17:42:36 +08:00
候学杰
50c3fb8e26 调试工具 2019-01-30 17:39:25 +08:00
候学杰
e198efa7ce Merge branch 'master' of git.int.haowumc.com:composer/php-json-rpc 2019-01-30 16:06:45 +08:00
候学杰
69e7fd3f5c 调试工具 2019-01-30 16:06:41 +08:00
dongwei
7b23b84083 cancel tunnel 2019-01-30 14:49:38 +08:00
dongwei
38f034aa34 include influxdb 2019-01-30 14:48:41 +08:00
候学杰
99f5b6c8f8 Merge branch 'master' of git.int.haowumc.com:composer/php-json-rpc 2019-01-30 12:56:52 +08:00
候学杰
dd5656e363 文档工具更改为私有地址 2019-01-30 12:56:40 +08:00
dongwei
4b07567d63 influxdb includ 2019-01-29 18:58:58 +08:00
18 changed files with 1029 additions and 361 deletions

View File

@ -12,6 +12,8 @@
执行
```bash
composer config repositories.php-rpc-doc vcs git@git.int.haowumc.com:composer/php-rpc-doc.git
composer require itxq/api-doc-php
composer config repositories.php-json-rpc vcs git@git.int.haowumc.com:composer/php-json-rpc.git
composer require paidian/json-rpc:dev-master
```

View File

@ -7,6 +7,7 @@
"illuminate/support": "^5.5",
"illuminate/http": "^5.5",
"monolog/monolog": "^1.24",
"influxdb/influxdb-php": "^1.14",
"itxq/api-doc-php": "^1.1"
},
"autoload": {

842
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -45,5 +45,9 @@ return [
'local' => true,
'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'),
],
],
];

View File

@ -4,10 +4,12 @@ namespace JsonRpc;
use GuzzleHttp\Exception\ServerException;
use JsonRpc\Exception\RpcServerException;
use JsonRpc\Server\JsonRpcBase;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
class Client
class Client extends JsonRpc
{
/**
* all configuration information
@ -26,6 +28,11 @@ class Client
*/
protected $http;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* which server rpc call choose
* @var array
@ -38,6 +45,11 @@ class Client
$this->id = 1;
}
public function setLogger($logger)
{
$this->logger = $logger;
}
/**
*
* @param $k
@ -48,8 +60,7 @@ class Client
$this->server_config = $this->config['client'][$k];
$default = [
'app' => $k,
'timeout' => 3,
'timeout' => 60,
'allow_redirects' => false,
];
@ -60,10 +71,11 @@ class Client
/**
* @param $name
* @param $arguments
* @param $options []
* @throws RpcServerException
* @return array
*/
public function call($name, $arguments)
public function call($name, $arguments, $options = [])
{
$payload = [
'jsonrpc' => '2.0',
@ -71,7 +83,7 @@ class Client
'params' => $arguments,
'id' => $this->id(),
];
return $this->post($payload);
return $this->post($payload, $options);
}
/**
@ -90,38 +102,51 @@ class Client
* @throws RpcServerException
* @return array
*/
protected function post($payload)
protected function post($payload, $options = [])
{
try {
$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' => $requestId,
];
app('rpc.logger')->info("client_request",array_merge($this->server_config, $payload));
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [
$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) {
throw new RpcServerException($e->getMessage(), $e->getCode());
$ex = new RpcServerException(self::ErrorMsg[JsonRpc::Rpc_Error_Internal_Error], JsonRpc::Rpc_Error_Internal_Error);
if (function_exists('env') && env("APP_DEBUG") == true) {
$ex->setResponse($e->getResponse());
}
throw $ex;
}
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', 500);
throw new RpcServerException('http response empty', JsonRpc::Rpc_Error_System_Error);
}
if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) {
$message = is_array($body['error']['message']) ? json_encode($body['error']['message']) : $body['error']['message'];
throw new RpcServerException($message, $body['error']['code']);
$e = new RpcServerException($message, $body['error']['code']);
throw $e;
}
return $body['result'];
} catch (\InvalidArgumentException $e) {
app('rpc.logger')->error('client_decode_error',array_merge($this->server_config, $payload));
throw new RpcServerException('json decode error', -32700);
$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 (function_exists('env') && env("APP_DEBUG") == true) {
$ex->setResponse($resp);
}
throw $ex;
}
}

View File

@ -4,4 +4,16 @@ namespace JsonRpc\Exception;
class RpcServerException extends \Exception
{
protected $response;
public function setResponse($resp)
{
$this->response = $resp;
}
public function getResponse()
{
return $this->response;
}
}

View File

@ -1,8 +1,8 @@
<?php
namespace JsonRpc\Server;
namespace JsonRpc;
class JsonRpcBase
class JsonRpc
{
const Rpc_Error_Parse_Error = -32700; //Parse error语法解析错误 服务端接收到无效的json。该错误发送于服务器尝试解析json文本
const Rpc_Error_Invalid_Request = -32600; //Invalid Request无效请求 发送的json不是一个有效的请求对象。

View File

@ -40,7 +40,7 @@ class Security
*/
private function isClientIPPermitted($ip)
{
if (!app()->environment('production', 'staging')) {
if (app()->environment('develop', 'local')) {
return true;
}

View File

@ -1,8 +1,13 @@
<?php
namespace JsonRpc\Middleware;
use Closure;
use GuzzleHttp\Client;
use Illuminate\Http\JsonResponse;
use InfluxDB\Database;
use InfluxDB\Exception;
use InfluxDB\Point;
/**
* Class TunnelMiddleware
@ -10,35 +15,52 @@ use Illuminate\Http\JsonResponse;
*/
class TunnelMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Pre-Middleware Action
$response = $next($request);
// Post-Middleware Action
return $response;
}
public function terminate($request, $response)
{
//过滤tool返回结果
if ($response instanceof JsonResponse)
{
$content = $response->getOriginalContent();
if (isset($content['error'])){
app('rpc.logger')->info('rpc tunnel', [$content['error']['code']]);
} else {
app('rpc.logger')->info('rpc tunnel', [200]);
}
}
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Pre-Middleware Action
$response = $next($request);
// Post-Middleware Action
return $response;
}
/**
* @param \Illuminate\Http\Request $request
* @param \Closure $response
*/
public function terminate($request, $response)
{
//过滤tool返回结果
if ($response instanceof JsonResponse) {
if (app()->environment('develop', 'production') && env('RPC_MONITOR_SWITCH') == 1) {
$content = $response->getOriginalContent();
$status = isset($content['error']) ? $content['error']['code'] : 200;
$client = new \InfluxDB\Client("influxdb-svc", 8086, '', '', false, false, 1, 1);
$database = $client->selectDB('rpc_monitor');
$points = array(
new Point(
'monitor',
1,
['app' => env('APP_NAME'), 'status' => $status, 'env' => app()->environment()],
['status_value' => $status == 200 ? $status : -$status]
)
);
try {
$database->writePoints($points, Database::PRECISION_SECONDS);
} catch (Exception $exception) {
app('log')->error('influxdb-write-wrong', ['code' => $exception->getCode(),'message' => $exception->getMessage()]);
}
}
}
}
}

View File

@ -11,42 +11,47 @@ use Monolog\Logger;
class BaseServiceProvider extends ServiceProvider
{
public function boot(){
protected $logger;
public function boot()
{
Request::setTrustedProxies([
//pod network
'172.20.0.0/16',
//vpc
'10.0.2.0/16',
'10.0.0.0/16',
//local
'127.0.0.1',
//北京办公区
'172.16.100.0/16'
'172.16.0.0/16',
//aliyun slb
'100.116.0.0/16',
], Request::HEADER_X_FORWARDED_ALL);
}
protected function setupConfig()
{
$source = realpath(__DIR__ . '/../../config/rpc.php');
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->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');
}
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);
}
}

View File

@ -16,18 +16,19 @@ class ClientServiceProvider extends BaseServiceProvider
public function register()
{
parent::register();
$this->app->configure('rpc');
$config = config('rpc');
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);
$client = new Client($config);
$client->setLogger($this->logger);
return $client;
});
foreach ($config['client'] as $k => $item) {
$this->app->singleton('rpc.' . $k, function () use ($k) {
return app('rpc')->endpoint($k);
$this->app->singleton('rpc.' . $k, function () use ($k, $config) {
return (new Client($config))->endpoint($k);
});
}
}

View File

@ -40,6 +40,7 @@ class LumenServerServiceProvider extends BaseServiceProvider
}
$callback = function () use ($config) {
$server = new JsonRpcServer($config);
$server->setLogger($this->logger);
return $server->handler();
};

View File

@ -4,8 +4,9 @@ namespace JsonRpc\Server;
use Illuminate\Http\JsonResponse;
use JsonRpc\Exception\RpcServerException;
use JsonRpc\JsonRpc;
class JsonRpcMethod extends JsonRpcBase
class JsonRpcMethod extends JsonRpc
{
protected $id;
protected $request;
@ -28,14 +29,24 @@ class JsonRpcMethod extends JsonRpcBase
public function error($code, $msg)
{
return [
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => is_array($msg) ? json_encode($msg) : $msg,
],
'id' => $this->id
];
return is_string($msg)
? [
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => $msg,
],
'id' => $this->id
]
: [
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => self::ErrorMsg[$code],
'data' => $msg
],
'id' => $this->id
];
}
}

View File

@ -5,20 +5,28 @@ namespace JsonRpc\Server;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use JsonRpc\JsonRpc;
use Psr\Log\LoggerInterface;
class JsonRpcServer extends JsonRpcBase
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;
@ -29,18 +37,22 @@ class JsonRpcServer extends JsonRpcBase
$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());
}
@ -54,9 +66,10 @@ class JsonRpcServer extends JsonRpcBase
if (!$this->isEnoughParameter($class, $function, $params)) {
return $this->error(self::Rpc_Error_Invalid_Params);
}
app('rpc.logger')->info('server', [$id, $class,$method, $params]);
$this->request->attributes->add(['tunnel_method' => $method, 'tunnel_params' => $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);
@ -65,6 +78,11 @@ class JsonRpcServer extends JsonRpcBase
}
}
/**
* 处理json rpc post body
* @param $data
* @return array
*/
protected function parseJson($data)
{
$data = \GuzzleHttp\json_decode($data, true);
@ -74,36 +92,23 @@ class JsonRpcServer extends JsonRpcBase
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);

View File

@ -4,6 +4,7 @@ namespace JsonRpc\Server;
use Illuminate\Http\Request;
use Illuminate\View\Factory;
use itxq\apidoc\BootstrapApiDoc;
use JsonRpc\Exception\RpcServerException;
use Monolog\Logger;
@ -35,21 +36,40 @@ class JsonRpcTool
*/
$view = view();
$params = json_decode($request->input('params',"[\r\n]"), true);
$params = json_decode($request->input('params', "[\r\n]"), true);
$method = $request->input('method');
if ($request->method() == Request::METHOD_POST) {
try {
$result = app('rpc.'.$this->config['name'])->call($method, $params);
$result = app('rpc.' . $this->config['name'])->call($method, $params);
$view->share('result', json_encode($result, JSON_PRETTY_PRINT));
} catch (RpcServerException $exception) {
$view->share('error', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
$resp = $exception->getResponse();
$view->share('error', [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'resp' => $resp]
);
}
}
$methods = [];
foreach ($this->config['map'] as $key => $item) {
if (!in_array($item[0], $methods)) {
$methods[] = $item[0];
}
}
$config = [
'class' => $methods,
'filter_method' => [],
];
$api = new BootstrapApiDoc($config);
$data = $api->getApiDocTmp();
$methods = $this->getMethods();
$view->share('method',$method);
$view->share('data',json_encode($data));
$view->share('endpoint', $this->getEndpoint());
$view->share('methods', $methods);
$view->share('params', json_encode($params,JSON_PRETTY_PRINT));
$view->share('method', $method);
$view->share('params', json_encode($params, JSON_PRETTY_PRINT));
foreach ($methods as $name => $class) {
$desc[$name] = $this->desc($class[0], $class[1]);
@ -83,6 +103,6 @@ class JsonRpcTool
$reflector = $this->classes[$class];
}
return str_replace("/**\n",'',$reflector->getMethod($method)->getDocComment());
return str_replace("/**\n", '', $reflector->getMethod($method)->getDocComment());
}
}

View File

@ -21,7 +21,7 @@
<nav class="col-md-3 d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<ul id="nav-content" class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" href="/rpc/doc.html">
<span data-feather="home"></span>
@ -29,10 +29,10 @@
</a>
</li>
{{--<li class="nav-item">--}}
{{--<a class="nav-link" href="#">--}}
{{--<span data-feather="file"></span>--}}
{{--abc--}}
{{--</a>--}}
{{--<a class="nav-link" href="#">--}}
{{--<span data-feather="file"></span>--}}
{{--abc--}}
{{--</a>--}}
{{--</li>--}}
</ul>
</div>
@ -55,7 +55,6 @@
{{--</div>--}}
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputAddress">Method</label>
<select class="form-control" id="method" name="method">
@ -64,22 +63,65 @@
@endforeach
</select>
</div>
<div class="table-item col-md-12">
<p class="table-title">
<span class="btn btn-xs btn-info">请求参数</span>
</p>
<table id="paramRequird" class="table">
<tr>
<td>参数</td>
<td>类型</td>
<td>描述</td>
<td>默认值</td>
<td>是否必须</td>
</tr>
</table>
</div>
<div class="table-item col-md-12">
<p class="table-title">
<span class="btn btn-xs btn-info">返回参数</span>
</p>
<table id="returnRequird" class="table">
<tr>
<td>参数</td>
<td>类型</td>
<td>描述</td>
<td>默认值</td>
<td>是否必须</td>
</tr>
</table>
</div>
<div class="table-item col-md-12">
<p class="table-title">
<span class="btn btn-xs btn-info">状态码说明</span>
</p>
<table id="codeRequird" class="table">
</table>
</div>
</div>
<div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputAddress">Paramsjson 数组)</label>
<div id="editor" style="height: 300px">{{$params}}</div>
<input type="hidden" name="params" id="params" value="{{$params}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Request</button>
<button id="submit-btn" type="submit" class="btn btn-primary">Request</button>
</form>
<div class="row col-md-12">
@if( !empty($error) )
<div id='alert' class="alert alert-danger" role="alert">
RpcServerException: {{$error['message']}} with code {{$error['code']}}
<div id='alert' class="alert alert-danger" role="alert" style="width: 100%">
code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: {{$error['code']}} <br>
message : {{$error['message']}}
</div>
@if($error['resp'])
<h5>返回内容:</h5>
<iframe style="width: 100%;height: 500px;border: none;"
srcdoc='{{$error['resp']->getBody()}}'></iframe>
<hr>
@endif
@endif
@if( !empty($result) )
<h5>Result:</h5>
@ -100,13 +142,106 @@
<script src="https://cdn.bootcss.com/highlight.js/9.13.1/highlight.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.13.1/languages/json.min.js"></script>
<script src="https://cdn.bootcss.com/ace/1.4.2/ace.js"></script>
<script>
<script type="text/javascript">
var editor = ace.edit("editor");
var params = <?php echo $params; ?>;
var data = <?php echo $data; ?>;
var error_empty = <?php echo isset($error) ? 0 : 1; ?>;
console.log(params)
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/json");
editor.on('change',function(e){
editor.on('change', function (e) {
$('#params').val(editor.getValue())
})
$(document).ready(function(){
intTable();
var valKey =$("#method option:first-child").text();
var data = <?php echo $data; ?>;
var methodArray = data[valKey];
changeTable(methodArray);
if (error_empty > 0) {
var storage = window.localStorage;
var valKey = $("#method").find("option:selected").text();
var d = JSON.stringify(params);
storage.setItem(valKey, d);
}
changeNavShow()
});
$('#method').on('change', function() {
var valKey = $("#method").find("option:selected").text();
var methodArray = data[valKey];
intTable()
changeTable(methodArray)
})
function intTable() {
$("#paramRequird").empty();
$("#returnRequird").empty();
$("#codeRequird").empty();
var html1 = "<tr><td>参数</td><td>类型</td><td>描述</td><td>默认值</td><td>是否必须</td></tr>";
var html2 = "<tr><td>参数</td><td>类型</td><td>描述</td>/tr>";
var html3 = "<tr><td>状态码</td><td>描述</td></tr>";
$(html1).appendTo("#paramRequird");
$(html2).appendTo("#returnRequird");
$(html3).appendTo("#codeRequird");
}
function changeTable(params) {
params.param.map(function (val, index) {
var $trTemp = $("<tr></tr>");
//往行里面追加 td单元格
$trTemp.append("<td>"+ val.param_name +"</td>");
$trTemp.append("<td>"+ val.param_type +"</td>");
$trTemp.append("<td>"+ val.param_title +"</td>");
$trTemp.append("<td>"+ val.param_default +"</td>");
$trTemp.append("<td>"+ val.param_require +"</td>");
$trTemp.appendTo("#paramRequird");
})
params.return.map(function (val, index) {
var $trTemp = $("<tr></tr>");
//往行里面追加 td单元格
$trTemp.append("<td>"+ val.return_name +"</td>");
$trTemp.append("<td>"+ val.return_type +"</td>");
$trTemp.append("<td>"+ val.return_title +"</td>");
$trTemp.appendTo("#returnRequird");
})
params.code.map(function (val, index) {
var $trTemp = $("<tr></tr>");
//往行里面追加 td单元格
$trTemp.append("<td>"+ val.code +"</td>");
$trTemp.append("<td>"+ val.content +"</td>");
$trTemp.appendTo("#codeRequird");
})
}
function changeLocoal(params) {
var storage=window.localStorage;
var valKey = $("#method").find("option:selected").text();
var data=editor.getValue();
var d=JSON.stringify(data).replaceAll("\r|\n|\\s", "");
storage.setItem(valKey,d);
}
function changeNavShow(){
var storage=window.localStorage;
for(var i=0;i<storage.length;i++){
var key=storage.key(i);
var $trTemp = $('<li class="nav-item"></li>');
$trTemp.append('<a class="nav-link">'+ key +'</a>');
$trTemp.appendTo("#nav-content");
$trTemp.attr('methond',key)
}
}
$('#nav-content').on('click','.nav-item', function(){
var activeKey = $(this).attr('methond');
var param = localStorage.getItem(activeKey)
$('.nav-item').removeClass('bg-info');
$(this).addClass('bg-info');
$("#method").val(activeKey);
var methodArray = data[activeKey];
intTable();
changeTable(methodArray);
$('#params').val(param);
editor.setValue(param)
})
</script>
<script>hljs.initHighlightingOnLoad();</script>
</body>

25
tests/publish.php Normal file
View 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());
}

View File

@ -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());
}