安装
安装docker hyperf
安装 hyperf-rpc-server-v8 (服务端)
docker run --name hyperf-rpc-server-v8 \
-v /www/docker/hyperf-rpc-server:/data/project \
-w /data/project \
-p 9508:9501 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.1-alpine-v3.18-swoole
安装 hyperf-rpc-client-v8 (客户端)
docker run --name hyperf-rpc-server-v8 \
-v /www/docker/hyperf-rpc-client:/data/project \
-w /data/project \
-p 9509:9501 -it \
--privileged -u root \
--entrypoint /bin/sh \
hyperf/hyperf:8.1-alpine-v3.18-swoole
(一)创建配置中心服务
安装nacos配置中心
composer require hyperf/config-center
composer require hyperf/config-nacos
配置文件config/aoutload/config_center.php
注意:
这里的
'group_name' => 'DEFAULT_GROUP'
, 对应注册中心的 Group
'namespace_id' => 'public'
, 对应注册中心的 命名空间
public
<?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\ConfigCenter\Mode;
use function Hyperf\Support\env;
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' => [
'tenant' => 'public', // corresponding with service.namespaceId
'data_id' => 'config_user',
'group' => 'DEFAULT_GROUP',
],
'nacos_config.data' => [
'data_id' => 'config_user',
'group' => 'DEFAULT_GROUP',
'type' => 'json',
],
'nacos_config.member' => [
'data_id' => 'member',
'group' => 'DEFAULT_GROUP',
'type' => 'json',
],
],
'client' => [
// nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
// 'uri' => '',
'host' => '10.0.0.94',
'port' => 8848,
'username' => null,
'password' => null,
'guzzle' => [
'config' => null,
]
],
],
],
];
nacos配置文件
结果
控制器调用
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
$v = \Hyperf\Config\config('nacos_config');
var_dump($v);
return [
'method' => $method,
'message' => "Hello {$user}.",
];
}
(二)创建服务发现项目
composer create-project hyperf/hyperf-skeleton
服务端
安装json-rpc
如果没安装请先安装相关的插件
composer require hyperf/json-rpc
安装rpc-server
要使用 JSON RPC 服务端:
composer require hyperf/rpc-server
安装service-governance-nacos
composer require hyperf/service-governance-nacos
或指定版本
composer require hyperf/service-governance-nacos:v2.2.39
server.php配置文件
[
'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'],
],
'options' => [
// Whether to enable request lifecycle event
'enable_request_lifecycle' => false,
],
],
nacos注册中心,推服务到注册中心,servers.php
注意:
这里的
'group_name' => 'DEFAULT_GROUP'
, 对应注册中心的 Group
'namespace_id' => 'public'
, 对应注册中心的 命名空间
public
<?php
/**
* services.php
*
* Created on 2024-08-27-14:30
* Created by xxp 332410549@qq.com
*/
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' => [
'host' => '10.0.0.94',
'port' => 8848,
// The nacos account info
'username' => null,
'password' => null,
'guzzle' => [
'config' => null,
],
'group_name' => 'DEFAULT_GROUP',
'namespace_id' => 'public',
'heartbeat' => 5,
'ephemeral' => false, // 是否注册临时实例
],
],
];
查看结果
消费端
安装json-rpc
如果没安装请先安装相关的插件
composer require hyperf/json-rpc
安装rpc-client
要使用 JSON RPC 客户端:
composer require hyperf/rpc-client
安装service-governance-nacos
composer require hyperf/service-governance-nacos
或指定版本
composer require hyperf/service-governance-nacos:v2.2.39