在Hyperf里,目前仅支持Consul和Nacos为服务中心的组件。在Hyperf服务注册之consul这篇文章中介绍了如何使用consul注册、发现服务。而使用Nacos与consul没有太大区别。
同一个服务既可以是服务者也可以是消费者,为了简单清楚的演示,本文中使用docker 启动hyperf服务提供者服务、消费者服务、Nacos服务。
依赖的组件:
docker run --name hyperf-provider -p 9501:9501 -v /work/hyperf:/opt/hyperf -itd hyperf/hyperf 安装的时候选择JSON-RPC cd /opt composer create-project hyperf/hyperf-skeleton hyperf cd hyperf composer require hyperf/service-governance composer require hyperf/service-governance-nacos
进入到config/autoload/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'], ], ],
如下图所示:
创建接口类:App\JsonRpc\TestServiceInterface
interface TestServiceInterface { public function sum(int $a, int $b): int; public function diff(int $a, int $b): int; }
创建实现类: App\JsonRpc\TestService
/** * @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="nacos") * Class CalculatorService * @package App\JsonRpc */ class CalculatorService implements CalculatorServiceInterface { public function sum(int $a, int $b): int { return $a + $b; } public function diff(int $a, int $b): int { return $a - $b; } }
@RpcService 共有 4 个参数:
name 属性为定义该服务的名称,这里定义一个全局唯一的名字即可,Hyperf 会根据该属性生成对应的 ID 注册到服务中心去;
protocol 属性为定义该服务暴露的协议,目前仅支持 jsonrpc 和 jsonrpc-http,分别对应于 TCP 协议和 HTTP 协议下的两种协议,默认值为 jsonrpc-http,这里的值对应在 Hyperf\Rpc\ProtocolManager 里面注册的协议的 key,这两个本质上都是 JSON RPC 协议,区别在于数据格式化、数据打包、数据传输器等不同。
server 属性为绑定该服务类发布所要承载的 Server,默认值为 jsonrpc-http,该属性对应 config/autoload/server.php 文件内 servers 下所对应的 name,这里也就意味着我们需要定义一个对应的 Server;
publishTo 属性为定义该服务所要发布的服务中心,目前仅支持 consul、nacos 或为空,为空时代表不发布该服务到服务中心去,但也就意味着您需要手动处理服务发现的问题,要使用此功能需安装 hyperf/service-governance 组件及对应的驱动依赖;
此地址为服务中心的地址,在启动服务时,Hyperf 会自动地将 @RpcService 定义了 publishTo 属性为 consul 的服务注册到服务中心去。
config/autoload/services.php
// 服务驱动相关配置 'drivers' => [ 'nacos' => [ // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port // 'url' => '', // The nacos host info 'host' => env("NACOS_HOST", '127.0.0.1'), 'port' => env("NACOS_PORT", 8848), // The nacos account info 'username' => env("NACOS_USERNAME", "nacos"), 'password' => env("NACOS_PASSWORD", "nacos"), 'guzzle' => [ 'config' => null, ], 'group_name' => "DEFAULT_GROUP", 'namespace_id' => env("NACOS_CONFIG_DATA_TENANT"), 'heartbeat' => 5, 'ephemeral' => false, // 是否注册临时实例 ], ]
# 此处和启动服务提供者一样,启动容器,安装依赖 docker run --name hyperf-customer -p 9500:9500 -v /work/hyperf-customer:/opt/hyperf -itd hyperf/hyperf 安装的时候选择JSON-RPC cd /opt composer create-project hyperf/hyperf-skeleton hyperf cd hyperf composer require hyperf/service-governance composer require hyperf/service-governance-nacos
进入到config/autoload/server.php。添加下面配置
[ 'name' => 'http', 'type' => Server::SERVER_HTTP, 'host' => '0.0.0.0', 'port' => 9500, 'sock_type' => SWOOLE_SOCK_TCP, 'callbacks' => [ Event::ON_REQUEST => [Hyperf\HttpServer::class, 'onRequest'], ], ],
如下图所示:
config/autoload/services.php
$registry = [ 'protocol' => 'nacos', 'address' => 'http://'.env("NACOS_HOST").":".env("NACOS_PORT"), ]; return [ 'enable' => [ // 开启服务发现 'discovery' => true, // 开启服务注册 'register' => true, ], // 服务消费者相关配置 'consumers' => value(function () use($registry) { $consumers = []; $services = [ 'TestInterface' => TestInterface::class, ]; foreach ($services as $name => $interface) { $consumers[] = [ 'protocol' => 'jsonrpc-http', 'name' => $name, 'load_balancer' => 'random', 'service' => $interface, 'registry' => $registry ]; } return $consumers; }), ]
app/JsonRpc/TestServiceInterface.php
namespace App\JsonRpc; interface TestInterface { public function sum(int $a, int $b): int; public function diff(int $a, int $b): int; }
/** * @Inject * @var TestInterface */ private $test; /** * @RequestMapping(path="index", methods="get") */ public function index() { $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); $diff = $this->test->diff(12, 3); return [ 'method' => $method, 'message' => "Hello {$user}.", 'diff' => $diff ]; }
/** * @RequestMapping(path="index", methods="get") */ public function index() { $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); $container = ApplicationContext::getContainer()->get(TestServiceInterface:class); $diff = $container->diff(12, 3); return [ 'method' => $method, 'message' => "Hello {$user}.", 'diff' => $diff ]; }
docker pull nacos/nacos-server docker run --name nacos-server -p 8848:8848-itd nacos/nacos-server:latest
访问: http://127.0.0.1:8848/nacos/#/login
cd opt/hyperf php bin/hyperf.php start
服务会注册到nacos。
访问nacos,进入服务管理中,就会看到注册的服务了。
访问接口
http://127.0.0.1:9500/index/index
返回结果:
{ method: "GET", message: "Hello Hyperf.", diff: 9 }