流程图
1 发起Http
Account 代表账户服务端 —-> 账户下的Users用户功能模块 ——-> User 操作用户方法, 查询, 删除, 更新 等…POST http://192.168.99.100:9500/Account/Users/User?username=admin&password=admin
获取用户id为12 的详情信息GET http://192.168.99.100:9500/Account/Users/User/12
2 中间服务处理
路由
Router::addGroup('/Account',function (){
Router::get('/Users/User/{id}','App\Controller\Account\Users\UserController::info');
Router::post('/Users/User','App\Controller\Account\Users\UserController@add');
});
3 中间发起Rpc 请求Account 账户服务端
// 查询详情
public function info(int $id){
$client = ApplicationContext::getContainer()->get(UserServiceInterface::class);
$value = $client->info($id);
return $value;
}
// 添加内容
public function add(){
$data = $this->request->all();
$client = ApplicationContext::getContainer()->get(UserServiceInterface::class);
$value = $client->add($data);
return $value;
}
4 账户服务端
UserService.php
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* Class UserService
* @RpcService(name="UserService", protocol="jsonrpc", server="jsonrpc")
*/
class UserService implements UserServiceInterface
{
// 查询详情
public function info($id){
$data = [
'id' => $id,
'name' => '名称'
];
$res = [
"result" => 1,
"message" => "查询成功",
"data" => $data,
"status_code" => 0
];
return $res;
}
// 添加用户
public function add($data){
$res = [
"result" => 1,
"message" => "添加成功",
"data" => $data,
"status_code" => 0
];
return $res;
}
}
5 返回结果
POST 返回结果
{
"result": 1,
"message": "添加成功",
"data": {
"username": "admin",
"password": "admin"
},
"status_code": 0
}
GET 返回结果
{
"result": 1,
"message": "查询成功",
"data": {
"id": 12,
"name": "名称"
},
"status_code": 0
}