Hyperf jsonrpc
依赖的 composer 包
composer require hyperf/json-rpc
composer require hyperf/rpc-server
composer require hyperf/rpc-client
composer require hyperf/service-governance
composer require hyperf/service-governance-consul
composer require hyperf/service-governance-nacos
`
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo属性 值为consul, protocol是协议,默认是jsonrpc-http, 这里演示TCP协议 故使用 jsonrpc
* @RpcService(name="FooService ", protocol="jsonrpc", server="jsonrpc-tcp", publishTo="consul")
*/
class FooService implements FooServiceInterface
{
/**
* @inheritDoc
*/
public function hello()
{
return 'This is Foo JsonRpc Service.';
}
}
配置hyperf服务启动 `/conifg/autoload/server.php` 这里演示TCP协议
。。。
[
'name' => 'jsonrpc-tcp',
'type' => \Hyperf\Server\Server::SERVER_BASE,
'host' => '0.0.0.0',
'port' => 9503,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
\Hyperf\Server\Event::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
],
'settings' => [
'open_eof_split' => true,
'package_eof' => "\r\n",
'package_max_length' => 1024 * 1024 * 2,
],
],
配置注册中心 /conifg/autoload/services.php
return [
'enable' => [
// 开启服务发现
'discovery' => true,
// 开启服务注册
'register' => true,
],
// 服务消费者相关配置
'consumers' => [],
// 服务提供者相关配置
'providers' => [],
// 服务驱动相关配置
'drivers' => [
'consul' => [
],
'nacos' => [
],
],
];