您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
jsonrpc服务端实现(hyperf)
发布时间:2022-10-10 21:18:37编辑:雪饮阅读()
安装consul
wget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip
unzip consul_0.7.5_linux_amd64.zip
启动命令(需要提供一个数据存储目录(consul_data提前建立好))
./consul agent -data-dir=./consul_data
Hyperf配置
Config/autoload/server.php的servers配置如:
'servers' => [
[
'name' => 'jsonrpc-http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9600,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
],
],
],
创建一个service
<?php
declare(strict_types=1);
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* Class UserService
* @package App\JsonRpc
* @RpcService(name="UserService", protocol="jsonrpc-http", server="jsonrpc-http")
*/
class UserService{
/**
* @param string $name
* @param string $gender
* @return string
*/
public function createUser(string $name, int $gender)
{
return "name:".$name.",gender:".$gender;
}
}
安装rpc服务端
composer require hyperf/rpc-server
安装统一接入层
composer require hyperf/service-governance
安装consul服务注册适配器
composer require hyperf/service-governance-consul
启动hyperf
[root@localhost hyperf-skeleton]# /www/server/php/74/bin/php -c /www/server/php/74/etc/php.ini bin/hyperf.php start
Postman请求示例
url: http://192.168.31.80:9600
请求方式:post
请求参数类型:raw
请求参数结构:json
请求参数示例:
{
"jsonrpc":"2.0",
"method":"/user/createUser",
"params":{
"name":"xx",
"gender":3
},
"id":"1"
}
关键字词:jsonrpc,服务端,实现,hyperf
上一篇:hyperf实现grpc客户端