流程图

image.png

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 中间服务处理

路由

  1. Router::addGroup('/Account',function (){
  2. Router::get('/Users/User/{id}','App\Controller\Account\Users\UserController::info');
  3. Router::post('/Users/User','App\Controller\Account\Users\UserController@add');
  4. });

3 中间发起Rpc 请求Account 账户服务端

  1. // 查询详情
  2. public function info(int $id){
  3. $client = ApplicationContext::getContainer()->get(UserServiceInterface::class);
  4. $value = $client->info($id);
  5. return $value;
  6. }
  7. // 添加内容
  8. public function add(){
  9. $data = $this->request->all();
  10. $client = ApplicationContext::getContainer()->get(UserServiceInterface::class);
  11. $value = $client->add($data);
  12. return $value;
  13. }

4 账户服务端

UserService.php

  1. <?php
  2. namespace App\JsonRpc;
  3. use Hyperf\RpcServer\Annotation\RpcService;
  4. /**
  5. * Class UserService
  6. * @RpcService(name="UserService", protocol="jsonrpc", server="jsonrpc")
  7. */
  8. class UserService implements UserServiceInterface
  9. {
  10. // 查询详情
  11. public function info($id){
  12. $data = [
  13. 'id' => $id,
  14. 'name' => '名称'
  15. ];
  16. $res = [
  17. "result" => 1,
  18. "message" => "查询成功",
  19. "data" => $data,
  20. "status_code" => 0
  21. ];
  22. return $res;
  23. }
  24. // 添加用户
  25. public function add($data){
  26. $res = [
  27. "result" => 1,
  28. "message" => "添加成功",
  29. "data" => $data,
  30. "status_code" => 0
  31. ];
  32. return $res;
  33. }
  34. }

5 返回结果

POST 返回结果

  1. {
  2. "result": 1,
  3. "message": "添加成功",
  4. "data": {
  5. "username": "admin",
  6. "password": "admin"
  7. },
  8. "status_code": 0
  9. }

GET 返回结果

  1. {
  2. "result": 1,
  3. "message": "查询成功",
  4. "data": {
  5. "id": 12,
  6. "name": "名称"
  7. },
  8. "status_code": 0
  9. }