Hyperf微服务搭建

一、安装 consul 服务

docker pull consul
docker run --name=consul -p 8500:8500 -e CONSUL_BIND_INTERFACE=eth0 -d consul

 

 安装并启动成功后访问:http://本机IP:8500/

二、服务提供者

2.1、启动服务提供者容器

// 本地 D:\phpstudy_pro\WWW 目录创建一个 hyperf 项目作为服务提供者
composer create-project hyperf/hyperf-skeleton hyperf-db

// 基于hyperf镜像生成容器
docker run --name hyperf -v D:\phpstudy_pro\WWW\hyperf-db:/data/project/hyperf-db -p 9501:9501 -it --privileged -u root --entrypoint /bin/sh hyperf/hyperf:7.4-alpine-v3.11-swoole

cd data/project/hyperf-db

// 安装统一接入层
composer require hyperf/service-governance

// 选择安装对应的适配器(这里用consul)
composer require hyperf/service-governance-consul
composer require hyperf/service-governance-nacos

// 安装 JSON RPC 服务
composer require hyperf/json-rpc

// 安装 JSON RPC 服务端
composer require hyperf/rpc-server

// 安装 JSON RPC 客户端
composer require hyperf/rpc-client

// 安装 consul 组件
composer require hyperf/consul
// 发布 consul 组件配置到 app
php bin/hyperf.php vendor:publish hyperf/consul

2.2、定义 JSON RPC Server 

'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'jsonrpc-http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9504,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'jsonrpc',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 9503,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
            ],
            'settings' => [
                'open_length_check' => true,
                'package_length_type' => 'N',
                'package_length_offset' => 0,
                'package_body_offset' => 4,
                'package_max_length' => 1024 * 1024 * 2,
            ],
        ],
    ],

2.3、定义接口

2.4、定义服务提供者

 2.5、发布到服务中心

在 config/autoload/services.php 配置文件内配置 drivers.consul 配置即可,注意 127.0.0.1 需要更换本机 ip 地址,否则会报错。

return [
    'enable' => [
        'discovery' => true,
        'register' => true,
    ],
    'providers' => [],
    'drivers' => [
        'consul' => [
            'uri' => 'http://127.0.0.1:8500',
            'token' => '',
            'check' => [
                'deregister_critical_service_after' => '90m',
                'interval' => '1s',
            ],
        ],
        'nacos' => [
            // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
            // 'url' => '',
            // The nacos host info
            'host' => '127.0.0.1',
            'port' => 8848,
            // The nacos account info
            'username' => null,
            'password' => null,
            'guzzle' => [
                'config' => null,
            ],
            'group_name' => 'api',
            'namespace_id' => 'namespace_id',
            'heartbeat' => 5,
            'ephemeral' => false,
        ],
    ],
];

 三、服务消费者

3.1、启动服务消费者容器

// 本地 D:\phpstudy_pro\WWW 目录创建一个 hyperf 项目作为服务消费者
composer create-project hyperf/hyperf-skeleton hyperf-api

// 基于hyperf镜像生成容器
docker run --name hyperf-api -v D:\phpstudy_pro\WWW\hyperf-api:/data/project/hyperf-api -p 9502:9502 -it --privileged -u root --entrypoint /bin/sh hyperf/hyperf:7.4-alpine-v3.11-swoole

cd data/project/hyperf-api

服务消费者也可以作为服务提供者提供服务,所以按照之前的步骤把相关组件安装一下

3.2、修改 server 配置

// config/autoload/server.php
'servers' => [
    [
        'name' => 'http',
        'type' => Server::SERVER_HTTP,
        'host' => '0.0.0.0',
        'port' => 9502,
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
        ],
    ],
],

// config/autoload/services.php
'consumers' => [
    [
        // name 需与服务提供者的 name 属性相同
        'name' => 'CalculatorService',
        // 服务接口名,可选,默认值等于 name 配置的值,如果 name 直接定义为接口类则可忽略此行配置,如 name 为字符串则需要配置 service 对应到接口类
        'service' => \App\JsonRpc\CalculatorServiceInterface::class,
        // 对应容器对象 ID,可选,默认值等于 service 配置的值,用来定义依赖注入的 key
        'id' => \App\JsonRpc\CalculatorServiceInterface::class,
        // 服务提供者的服务协议,可选,默认值为 jsonrpc-http
        // 可选 jsonrpc-http jsonrpc jsonrpc-tcp-length-check
        'protocol' => 'jsonrpc-http',
        // 负载均衡算法,可选,默认值为 random
        'load_balancer' => 'random',
        // 这个消费者要从哪个服务中心获取节点信息,如不配置则不会从服务中心获取节点信息
        'registry' => [
            'protocol' => 'consul',
            'address' => 'http://127.0.0.1:8500',
        ],
        // 如果没有指定上面的 registry 配置,即为直接对指定的节点进行消费,通过下面的 nodes 参数来配置服务提供者的节点信息
        'nodes' => [
            ['host' => '127.0.0.1', 'port' => 9504],
        ],
        // 配置项,会影响到 Packer 和 Transporter
        'options' => [
            'connect_timeout' => 5.0,
            'recv_timeout' => 5.0,
            'settings' => [
                // 根据协议不同,区分配置
                'open_eof_split' => true,
                'package_eof' => "\r\n",
                // 'open_length_check' => true,
                // 'package_length_type' => 'N',
                // 'package_length_offset' => 0,
                // 'package_body_offset' => 4,
            ],
            // 重试次数,默认值为 2,收包超时不进行重试。暂只支持 JsonRpcPoolTransporter
            'retry_count' => 2,
            // 重试间隔,毫秒
            'retry_interval' => 100,
            // 使用多路复用 RPC 时的心跳间隔,null 为不触发心跳
            'heartbeat' => 30,
            // 当使用 JsonRpcPoolTransporter 时会用到以下配置
            'pool' => [
                'min_connections' => 1,
                'max_connections' => 32,
                'connect_timeout' => 10.0,
                'wait_timeout' => 3.0,
                'heartbeat' => -1,
                'max_idle_time' => 60.0,
            ],
        ],
    ]
],
'drivers' => [
    'consul' => [
        'uri' => 'http://127.0.0.1:8500',
        'token' => '',
        'check' => [
            'deregister_critical_service_after' => '90m',
            'interval' => '1s',
        ],
    ],
    'nacos' => [
        // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
        // 'url' => '',
        // The nacos host info
        'host' => '127.0.0.1',
        'port' => 8848,
        // The nacos account info
        'username' => null,
        'password' => null,
        'guzzle' => [
            'config' => null,
        ],
        'group_name' => 'api',
        'namespace_id' => 'namespace_id',
        'heartbeat' => 5,
        'ephemeral' => false,
    ],
],

3.3、定义接口,同服务端保持一致

 

 3.4、定义路由

Router::get('/testRpc', 'App\Controller\IndexController::testRpc');

3.5、控制器中调用

public function testRpc()
    {
        $client = ApplicationContext::getContainer()->get(CalculatorServiceInterface::class);
        $res = $client->add(1,2);

        return ['result'=>$res];
    }

你可能感兴趣的:(微服务,java,docker)