安裝 Hyperf,使用Docker運行
# 環境win10,CMD命令行窗口,執行如下命令
# 這個容器我們用來運行Hyperf(多映射幾個端口,方便后面使用)
docker run --name hyperf_rpc -v E:\phpstudy_pro\WWW\docker\workspace\skeleton:/data/project -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -p 9505:9505 -p 9506:9506 -p 9507:9507 -p 9508:9508 -p 9509:9509 -it --privileged -u root --entrypoint sh hyperf/hyperf:7.4-alpine-v3.11-swoole
# 設置國內鏡像
/ # composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
# 進入到我們映射的主機目錄
/ # cd /data/project/
# 安裝 Hyperf(所有選項全部選擇默認,直接回車到底)
/data/project # composer create-project hyperf/hyperf-skeleton hyperf-rpc-client
# 安裝JSON RPC 服務組件
/data/project # cd hyperf-rpc-client/
/data/project/hyperf-rpc-client # composer require hyperf/json-rpc
/data/project/hyperf-rpc-client # composer require hyperf/rpc-server
/data/project/hyperf-rpc-client # composer require hyperf/rpc-client
/data/project/hyperf-rpc-client # composer require hyperf/service-governance
# 運行hyperf試試(可以看到啟動成功,端口9501,并且訪問成功http://127.0.0.1:9501/)
/data/project/hyperf-rpc-client # php bin/hyperf.php start
編寫一個服務提供者,映射9503端口
基于上面的hyperf-rpc-client,復制一個出來,并且命名為hyperf-rpc-service-user,作為“用戶模塊”的一個微服務,下面的修改,在這個代碼的基礎上修改
修改配置如下 config/autoload/server.php
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;
return [
'mode' => SWOOLE_PROCESS,
'servers' => [
[
'name' => 'jsonrpc-http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9503,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
],
],
],
'settings' => [
Constant::OPTION_ENABLE_COROUTINE => true,
Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
Constant::OPTION_OPEN_TCP_NODELAY => true,
Constant::OPTION_MAX_COROUTINE => 100000,
Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
Constant::OPTION_MAX_REQUEST => 100000,
Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,
Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
],
'callbacks' => [
Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
],
];
創建服務提供者
在/app目錄下新建
JsonRpc
目錄,并在此目錄下創建UserService.php、UserServiceInterface.php文件,文件內容如下:
UserService.php
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Contract\ConfigInterface;
/**
* 注意,如希望通過服務中心來管理服務,需在注解內增加 publishTo 屬性
* @RpcService(name="UserService", protocol="jsonrpc-http", server="jsonrpc-http")
*/
class UserService implements UserServiceInterface
{
// 獲取用戶信息
public function getUserInfo(int $id)
{
return ['id' => $id, 'name' => '黃翠剛'];
}
}
UserServiceInterface.php
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
interface UserServiceInterface
{
public function getUserInfo(int $id);
}
啟動
/data/project # cd /data/project/hyperf-rpc-service-user
/data/project/hyperf-rpc-service-user # php bin/hyperf.php start
修改hyperf-rpc-client代碼
創建services.php、UserController.php、UserServiceInterface.php文件,文件內容如下:
config/autoload/services.php
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
return [
'consumers' => [
[
// 用戶服務
'name' => 'UserService',
// 服務接口名,可選,默認值等于 name 配置的值,如果 name 直接定義為接口類則可忽略此行配置,如 name 為字符串則需要配置 service 對應到接口類
'service' => \App\JsonRpc\UserServiceInterface::class,
// 服務提供者的服務協議,可選,默認值為 jsonrpc-http
'protocol' => 'jsonrpc-http',
'nodes' => [
// Provide the host and port of the service provider.
['host' => '127.0.0.1', 'port' => 9503],
],
]
];
UserController.php
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\JsonRpc\UserServiceInterface;
class UserController
{
public $userService;
public function __construct(UserServiceInterface $userService)
{
$this->userService = $userService;
}
// 獲取用戶信息
public function getUserInfo(RequestInterface $request)
{
// 從請求中獲得 id 參數
$id = $request->input('id', 1);
$ret = $this->userService->getUserInfo((int)$id);
return $ret;
}
}
UserServiceInterface.php
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
interface UserServiceInterface
{
public function getUserInfo(int $id);
}
修改config/routes.php為如下:
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\HttpServer\Router\Router;
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
Router::addRoute(['GET', 'POST', 'HEAD'], '/user/getUserInfo', 'App\Controller\UserController@getUserInfo');
Router::get('/favicon.ico', function () {
return '';
});
訪問OK
http://127.0.0.1:9501/user/getUserInfo?id=5
輸出:{"id":5,"name":"黃翠剛"}