1.Hyperf RPC 服务搭建
创建服务提供者
composer create-project hyperf/hyperf-skeleton micro_server cd micro_server
安装注册中心Nacos 适配器
composer require hyperf/service-governance-nacos
配置文件
// 组件由 config/autoload/services.php 配置文件来驱动,如果没有改文件,自行创建: return [ 'enable' => [ // 开启服务发现 'discovery' => true, // 开启服务注册 'register' => true, ], // 服务消费者相关配置 'consumers' => [], // 服务提供者相关配置 'providers' => [], // 服务驱动相关配置 'drivers' => [ // consul 配置,当前未用到 'consul' => [ 'uri' => 'http://127.0.0.1:8500', 'token' => '', 'check' => [ 'deregister_critical_service_after' => '90m', 'interval' => '1s', ], ], // nacos 配置,当前使用 'nacos' => [ // The nacos host info 'host' => '127.0.0.1', 'port' => 8848, // nacos 账号密码信息 'username' => 'nacos', 'password' => 'nacos', 'guzzle' => [ 'config' => null, ], // 命名空间,public为默认系统空间 'group_name' => 'public', // 命名空间ID // 'namespace_id' => 'namespace_id', // 心跳检查秒数 'heartbeat' => 5, ], ], ];
安装配置中心Nacos 适配器
composer require hyperf/config-center composer require hyperf/config-nacos
<?php // 组件由 config/autoload/config_center.php 配置文件来驱动,如果没有改文件,自行创建: declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact [email protected] * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ use Hyperf\ConfigCenter\Mode; return [ 'enable' => (bool) env('CONFIG_CENTER_ENABLE', true), // 驱动 'driver' => env('CONFIG_CENTER_DRIVER', 'nacos'), 'mode' => env('CONFIG_CENTER_MODE', Mode::PROCESS), 'drivers' => [ 'nacos' => [ 'driver' => Hyperf\ConfigNacos\NacosDriver::class, 'merge_mode' => Hyperf\ConfigNacos\Constants::CONFIG_MERGE_OVERWRITE, 'interval' => 3, 'default_key' => 'nacos_config', 'listener_config' => [ // dataId, group, tenant, type, content // 配置中心的配置文件 'nacos_config' => [ // 命名空间/ID 'tenant' => 'public', // corresponding with service.namespaceId // DataID 'data_id' => 'hyperf-service-config', // 组名 'group' => 'DEFAULT_GROUP', // 数据类型 'type' => 'json', ], // 配置中心的配置文件 'nacos_config.data' => [ 'data_id' => 'hyperf-service-config-yml', 'group' => 'DEFAULT_GROUP', 'type' => 'json', ], ], 'client' => [ // 客户端 'host' => '127.0.0.1', 'port' => 8848, 'username' => 'nacos', 'password' => 'nacos', 'guzzle' => [ 'config' => null, ], ], ], ], ];
文件路径:app\JsonRpc\AdditionService.php 加法RPC方法
<?php declare(strict_types=1); namespace App\JsonRpc; use Hyperf\RpcServer\Annotation\RpcService; /** * 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo 属性 * @RpcService(name="AdditionService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="nacos") */ class AdditionService { public function add(int $a, int $b): int { // var_dump(config('nacos_config.data_id')); return $a + $b; } }
文件路径:app\JsonRpc\MultiplicationService.php 乘法RPC方法
<?php declare(strict_types=1); namespace App\JsonRpc; use Hyperf\RpcServer\Annotation\RpcService; /** * @RpcService(name="MultiplicationService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="nacos") */ class MultiplicationService { public function multiply(int $a, int $b): int { return $a * $b; } }
配置服务 文件路径: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 [email protected] * @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' => [ // 去掉HTTP服务,只提供RPC服务 // [ // 'name' => 'http', // 'type' => Server::SERVER_HTTP, // 'host' => '0.0.0.0', // 'port' => 9501, // 'sock_type' => SWOOLE_SOCK_TCP, // 'callbacks' => [ // Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'], // ], // ], // name 需跟上面方法写的一致 [ 'name' => 'jsonrpc-http', 'type' => Server::SERVER_HTTP, 'host' => '0.0.0.0', 'port' => 9504, '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'], ], ];
启动服务提供者
# 开启后即自动将rpc注册到nacos注册中心中,会自动把配置中心的配置拉下来映射到Config里面,直接从Config里获取配置中心的数据即可 php bin/hyperf.php start
访问Nacos 即可看见提供的两个服务
配置服务消费者
composer create-project hyperf/hyperf-skeleton micro_web cd micro_web
安装注册中心Nacos 适配器
composer require hyperf/service-governance-nacos
<?php // 这个是nacos服务的端口和地址 $registry = [ 'protocol' => 'nacos', 'address' => 'http://127.0.0.1:8848', ]; // 这里配置需要用的rpc服务 $services = [ 'AdditionService', 'MultiplicationService', ]; return [ // 此处省略了其它同层级的配置 'consumers' => value(function () use ($services, $registry) { // 循环生成rpc消费端 $consumers = []; foreach ($services as $name) { $consumers[] = [ 'name' => $name, 'registry' => $registry ]; } return $consumers; }), // Nacos服务驱动相关配置 'drivers' => [ 'nacos' => [ // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port // 'url' => '', // The nacos host info 'host' => '127.0.0.1', 'port' => 8848, // The nacos account info 'username' => 'nacos', 'password' => 'nacos', 'guzzle' => [ 'config' => null, ], 'group_name' => 'public', 'heartbeat' => 5, ], ], ];
文件路径:app\JsonRpc\AdditionService.php 调用加法RPC方法
<?php declare(strict_types=1); namespace App\JsonRpc; use Hyperf\RpcClient\AbstractServiceClient; /** * 服务消费者,类名可自定义,下方的服务名称需对应上方rpc服务的名称 */ class AdditionService extends AbstractServiceClient { /** * 定义对应服务提供者的服务名称 * @var string */ protected $serviceName = 'AdditionService'; /** * 定义对应服务提供者的服务协议 * @var string */ protected $protocol = 'jsonrpc-http'; public function add(int $a, int $b): int { return $this->__request(__FUNCTION__, compact('a', 'b')); } }
文件路径:app\JsonRpc\MultiplicationService.php 调用乘法RPC方法
<?php declare(strict_types=1); namespace App\JsonRpc; use Hyperf\RpcClient\AbstractServiceClient; /** * 服务消费者 */ class MultiplicationService extends AbstractServiceClient { /** * 定义对应服务提供者的服务名称 * @var string */ protected $serviceName = 'MultiplicationService'; /** * 定义对应服务提供者的服务协议 * @var string */ protected $protocol = 'jsonrpc-http'; public function multiply(int $a, int $b): int { return $this->__request(__FUNCTION__, compact('a', 'b')); } }
定义HTTP的方法接口
<?php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact [email protected] * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ namespace App\Controller; use App\JsonRpc\AdditionService; use App\JsonRpc\MultiplicationService; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; /** * @Controller() */ class IndexController extends AbstractController { public function index() { $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); return [ 'method' => $method, 'message' => "Hello {$user}.", ]; } /** * @RequestMapping(path="/add") * @param AdditionService $addition * @return array */ public function add(AdditionService $addition) { $a = (int)$this->request->input('a', 1); $b = (int)$this->request->input('b', 2); // $addition->add($a, $b) 调用ADD 方法 --> 对应RPC服务端的添加rpc方法 return [ 'a' => $a, 'b' => $b, 'add' => $addition->add($a, $b) ]; } /** * @RequestMapping(path="/mult") * @param MultiplicationService $multiplicaton * @return array */ public function multiply(MultiplicationService $multiplicaton) { $a = (int)$this->request->input('a', 1); $b = (int)$this->request->input('b', 2); return [ 'a' => $a, 'b' => $b, 'multiply' => $multiplicaton->multiply($a, $b) ]; } }
最后启动服务消费者
php bin/hyperf.php start
访问结果加法如下:
标签:hyper,Hyperf,OPTION,nacos,hyperf,注册,php,config From: https://www.cnblogs.com/fyiyy/p/16860286.html