标签:name nts odi 执行 配置 soc ann server back
<?php namespace App\Rpc; use Hyperf\RpcServer\Annotation\RpcService; /** * @RpcService(name="CalculatorService",protocol="jsonrpc-http",server="jsonrpc-http") */ class CalculatorService implements CalculatorServiceInterface { public function add(int $a, int $b): int { return $a + $b; }
public function minus(int $a, int $b): int { return $a - $b; } }
<?php namespace App\Rpc; interface CalculatorServiceInterface { public function add(int $a, int $b): int; public function minus(int $a, int $b): int; }
‘servers‘ => [ ... [ ‘name‘ => ‘jsonrpc-http‘, ‘type‘ => Server::SERVER_HTTP, ‘host‘ => ‘0.0.0.0‘, ‘port‘ => 9802, ‘sock_type‘ => SWOOLE_SOCK_TCP, ‘callbacks‘ => [ SwooleEvent::ON_REQUEST => [Hyperf\JsonRpc\HttpServer::class, ‘onRequest‘], ], ], ],
<?php
namespace App\Rpc;
interface CalculatorServiceInterface
{
public function add(int $a, int $b): int;
public function minus(int $a, int $b): int;
}
<?php namespace App\Controller; use App\Rpc\CalculatorServiceInterface; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\AutoController; /** * @AutoController() */ class IndexController extends AbstractController { /** * @Inject() * @var CalculatorServiceInterface */ private $calculatorService; public function rpc() { return $this->calculatorService->minus(10,2); } }
如果需要使用 consul来管理服务,则需要做如下操作
/** * @RpcService(name="CalculatorService",protocol="jsonrpc-http",server="jsonrpc-http",publishTo="consul")
*/
php bin/hyperf.php vendor:publish hyperf/consul
执行完毕后会形成一个配置文件 consul.php
3).Conusmer修改配置
return [ ‘consumers‘ => [ [ ‘name‘ => ‘CalculatorService‘, ‘service‘ => \App\Rpc\CalculatorServiceInterface::class,
registry‘ => [
‘protocol‘ => ‘consul‘,
‘address‘ => ‘http://127.0.0.1:8500‘,//对应Provider 中 consul.php配置项 ]
],
],
];
标签:name nts odi 执行 配置 soc ann server back
原文地址:https://www.cnblogs.com/bobobobobo/p/13093794.html