hyperf-consul 服务注册示例
- 先看hyperf文档,对hyperf有一定的了解 hyperf传送门
- 安装组件
#
conposer install
composer require hyperf/consul hyperf/json-rpc hyperf/rpc hyperf/rpc-client hyperf/rpc-server hyperf/service-governance hyperf/service-governance-consul
- 服务注册和服务消费的代码可以放一给项目也可以单独项目
- consul集群环境 docker-consul
# server.php
[
'name' => 'jsonrpc-http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9605,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
],
],
# consul.php consul集群服务的地址
return [
'uri' => 'http://192.168.240.120:8500',
'token' => '',
];
# services.php 里面
'drivers' => [
'consul' => [
'uri' => 'http://192.168.240.120:8500',
'token' => '',
'check' => [
'deregister_critical_service_after' => '90m',
'interval' => '1s',
],
],
],
#FooService.php里面注解里面@RpcService把服务注册到服务集群中
FooService.php FooServiceInterface.php
# services.php
'consumers' => value(function (){
$consumers = [];
$services = [
//多个服务
'FooService' => App\JsonRpc\Consumer\FooServiceConsumerInterface::class,
];
foreach ($services as $name => $interface) {
$consumers[] = [
'name' => $name,
'service' => $interface,
'id' => $interface,
'protocol' => 'jsonrpc-http',
'load_balancer' => 'random',
'registry' => [
'protocol' => 'consul',
'address' => 'http://192.168.240.120:8500',
],
//If `registry` is missing, then you should provide the nodes configs.
'nodes' => [
['host' => '192.168.240.120', 'port' => 9605],
],
];
}
return $consumers;
}),
#
FooServiceConsumerInterface.php
TestConsulController.php
/**
* @Inject()
* @var FooServiceConsumerInterface
*/
protected FooServiceConsumerInterface $fooService;
//consul
public function test_consul(){
$a = 100;
$b = 27;
$cu = $this->fooService->sum($a,$b);
$di = $this->fooService->diff($a,$b);
$data = [
'code' => 1,
'msg' => 'consul成功',
"consul:{$a}+{$b}" => $cu,
"consul:{$a}-{$b}" => $di,
];
LogService::info('consul成功',$data);
return $this->response->json($data);
}