美文网首页
使用服务中心consul

使用服务中心consul

作者: 黄刚刚 | 来源:发表于2021-12-20 11:17 被阅读0次

安装consul服务

hyperf文档中并没有说明如何安装 Consul 服务,以下对 Consul 做一个简单的安装

docker pull consul

docker run --name consul1 -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600 consul agent -server -bootstrap-expect=1 -ui -bind=0.0.0.0 -client=0.0.0.0


访问页面:http://127.0.0.1:8500/ui/dc1/services

hyperf-rpc-service-user项目和hyperf-rpc-client项目都加载依赖

composer require hyperf/service-governance-consul

修改代码hyperf-rpc-service-user 项目

config/autoload/services.php

<?php
return [
    'enable' => [
        // 开启服务发现
        'discovery' => true,
        // 开启服务注册
        'register' => true,
    ],
    // 服务消费者相关配置
    'consumers' => [],
    // 服务提供者相关配置
    'providers' => [],
    // 服务驱动相关配置
    'drivers' => [
        'consul' => [
            'uri' => 'http://172.17.0.3:8500',
            'token' => '',
            'check' => [
                'deregister_critical_service_after' => '90m',
                'interval' => '1s',
            ],
        ],
    ],
];

config/autoload/consul.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    'uri' => 'http://172.17.0.3:8500',
    'token' => '',
];

app/JsonRpc/UserService.php

<?php

namespace App\JsonRpc;

use Hyperf\RpcServer\Annotation\RpcService;
use Hyperf\Contract\ConfigInterface;

/**
 * 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo 属性
 * @RpcService(name="UserService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="consul")
 */
class UserService implements UserServiceInterface
{
    // 获取用户信息
    public function getUserInfo(int $id)
    {
        return ['id' => $id, 'name' => '黄翠刚'];
    }
}

启动服务: php bin/hyperf.php start

可以发现consul中已经多了一个服务了


修改代码hyperf-rpc-client 项目

config/autoload/services.php

<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    // 此处省略了其它同层级的配置
    'consumers' => value(function () {
        $consumers = [];
        // 这里自动创建代理消费者类的配置形式,顾存在 name 和 service 两个配置项,这里的做法不是唯一的,仅说明可以通过 PHP 代码来生成配置
        $services = [
            'UserService' => \App\JsonRpc\UserServiceInterface::class,
        ];
        foreach ($services as $name => $interface) {
            $consumers[] = [
                // 服务名
                'name' => $name,
                // 服务接口名,可选,默认值等于 name 配置的值,如果 name 直接定义为接口类则可忽略此行配置,如 name 为字符串则需要配置 service 对应到接口类
                'service' => $interface,
                // 服务提供者的服务协议,可选,默认值为 jsonrpc-http
                'protocol' => 'jsonrpc-http',
                // 这个消费者要从哪个服务中心获取节点信息,如不配置则不会从服务中心获取节点信息
                'registry' => [
                    'protocol' => 'consul',
                    'address' => 'http://172.17.0.3:8500',
                ],
            ];
        }
        return $consumers;
    }),
];

启动服务: php bin/hyperf.php start

访问:http://127.0.0.1:9501/user/getUserInfo?id=5

相关文章

网友评论

      本文标题:使用服务中心consul

      本文链接:https://www.haomeiwen.com/subject/mgotqrtx.html