美文网首页phphyperf
hyperf JsonRpc demo

hyperf JsonRpc demo

作者: yichen_china | 来源:发表于2020-05-04 14:43 被阅读0次

    1 服务端 provider.php

    composer.json文件

    "require": {
            "php": ">=7.2",
            "ext-swoole": ">=4.4",
            "hyperf/cache": "~1.1.0",
            "hyperf/command": "~1.1.0",
            "hyperf/config": "~1.1.0",
            "hyperf/contract": "~1.1.0",
            "hyperf/database": "~1.1.0",
            "hyperf/db-connection": "~1.1.0",
            "hyperf/devtool": "~1.1.0",
            "hyperf/di": "~1.1.0",
            "hyperf/dispatcher": "~1.1.0",
            "hyperf/event": "~1.1.0",
            "hyperf/exception-handler": "~1.1.0",
            "hyperf/framework": "~1.1.0",
            "hyperf/guzzle": "~1.1.0",
            "hyperf/http-server": "~1.1.0",
            "hyperf/logger": "~1.1.0",
            "hyperf/memory": "~1.1.0",
            "hyperf/paginator": "~1.1.0",
            "hyperf/pool": "~1.1.0",
            "hyperf/process": "~1.1.0",
            "hyperf/redis": "~1.1.0",
            "hyperf/utils": "~1.1.0",
            "hyperf/json-rpc": "~1.1.0",
            "hyperf/rpc": "~1.1.0",
            "hyperf/rpc-client": "~1.1.0",
            "hyperf/rpc-server": "~1.1.0",
            "hyperf/service-governance": "~1.1.0",
            "hyperf/config-apollo": "~1.1.0",
            "hyperf/constants": "~1.1.0",
            "hyperf/async-queue": "~1.1.0",
            "hyperf/amqp": "~1.1.0",
            "hyperf/model-cache": "~1.1.0",
            "hyperf/elasticsearch": "~1.1.0",
            "hyperf/tracer": "~1.1.0",
            "hyperf/consul": "^1.1",
            "hyperf/circuit-breaker": "^1.1",
            "hyperf/rate-limit": "^1.1"
        },
    

    consul.php

    <?php
    
    return [
        'uri' => 'http://127.0.0.1:8500',
    ];
    

    dependencies.php

    <?php declare(strict_types=1);
    
    /**
     * This file is part of Hyperf.
     *
     * @link     https://www.hyperf.io
     * @document https://doc.hyperf.io
     * @contact  group@hyperf.io
     * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
     */
    
    use Hyperf\JsonRpc\JsonRpcPoolTransporter;
    use Hyperf\JsonRpc\JsonRpcTransporter;
    
    return [
        JsonRpcTransporter::class => function () {
            return make(JsonRpcPoolTransporter::class);
        },
    ];
    

    server.php

    'servers' => [
            [
                'name' => 'http',
                'type' => Server::SERVER_HTTP,
                'host' => '0.0.0.0',
                'port' => 9601,
                'sock_type' => SWOOLE_SOCK_TCP,
                'callbacks' => [
                    SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
                ],
            ],
            [
                'name' => 'jsonrpc-http',
                'type' => Server::SERVER_HTTP,
                'host' => '0.0.0.0',
                'port' => 9604,
                'sock_type' => SWOOLE_SOCK_TCP,
                'callbacks' => [
                    SwooleEvent::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
                ],
            ],
            [
                'name' => 'jsonrpc',
                'type' => Server::SERVER_BASE,
                'host' => '0.0.0.0',
                'port' => 9603,
                'sock_type' => SWOOLE_SOCK_TCP,
                'callbacks' => [
                    SwooleEvent::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
                ],
                'settings' => [
                    'open_eof_split' => true,
                    'package_eof' => "\r\n",
                ],
            ],
        ],
    

    新建服务

    CalculatorService.php

    <?php
    
    namespace App\JsonRpc;
    
    use Hyperf\RpcServer\Annotation\RpcService;
    
    /**
     * 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo 属性
     * @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http",publishTo="consul")
     */
    class CalculatorService implements CalculatorServiceInterface
    {
        // 实现一个加法方法,这里简单的认为参数都是 int 类型
        public function add(int $a, int $b): int
        {
            $add =  $a + $b;
            var_dump($add);
            // 这里是服务方法的具体实现
            return $add;
        }
    }
    

    CalculatorServiceInterface.php

    <?php
    
    namespace App\JsonRpc;
    
    
    /**
     * Class CalculatorService
     * @package App\JsonRpc
     */
    interface CalculatorServiceInterface
    {
        public function add(int $a, int $b): int;
    }
    

    2 消费端

    consul.php

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

    dependencies.php

    <?php declare(strict_types=1);
    
    /**
     * This file is part of Hyperf.
     *
     * @link     https://www.hyperf.io
     * @document https://doc.hyperf.io
     * @contact  group@hyperf.io
     * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
     */
    
    use Hyperf\JsonRpc\JsonRpcPoolTransporter;
    use Hyperf\JsonRpc\JsonRpcTransporter;
    
    return [
        JsonRpcTransporter::class => function () {
            return make(JsonRpcPoolTransporter::class);
        },
    ];
    

    server.php

    'servers' => [
            [
                'name' => 'http',
                'type' => Server::SERVER_HTTP,
                'host' => '0.0.0.0',
                'port' => 9501,
                'sock_type' => SWOOLE_SOCK_TCP,
                'callbacks' => [
                    SwooleEvent::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' => [
                    SwooleEvent::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' => [
                    SwooleEvent::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
                ],
                'settings' => [
                    'open_eof_split' => true,
                    'package_eof' => "\r\n",
                ],
            ],
        ],
    

    services.php

    <?php
    
    use Hyperf\Server\Server;
    use Hyperf\Server\SwooleEvent;
    
    return [
        'consumers' => [
            [
                // name 需与服务提供者的 name 属性相同
                'name' => 'CalculatorService',
                // 服务接口名,可选,默认值等于 name 配置的值,如果 name 直接定义为接口类则可忽略此行配置,如 name 为字符串则需要配置 service 对应到接口类
                'service' => \App\JsonRpc\CalculatorServiceInterface::class,
                // 对应容器对象 ID,可选,默认值等于 service 配置的值,用来定义依赖注入的 key
                'id' => \App\JsonRpc\CalculatorServiceInterface::class,
                // 服务提供者的服务协议,可选,默认值为 jsonrpc-http
                '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' => 9604],
                ],
            ]
        ],
    ];
    

    消费服务

    <?php
    
    declare(strict_types=1);
    /**
     * This file is part of Hyperf.
     *
     * @link     https://www.hyperf.io
     * @document https://doc.hyperf.io
     * @contact  group@hyperf.io
     * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
     */
    
    namespace App\Controller;
    
    use Hyperf\Utils\ApplicationContext;
    use App\JsonRpc\CalculatorServiceInterface;
    
    class IndexController extends AbstractController
    {
    
        public function index()
        {
            $user = $this->request->input('user', 'Hyperf');
            $method = $this->request->getMethod();
    
            $client = ApplicationContext::getContainer()->get(CalculatorServiceInterface::class);
            $result = $client->add(1, 2);
    
            var_dump($result);
            return [
                'method' => $method,
                'message' => "Hello {$user}.",
            ];
        }
    }
    

    启动服务 启动消费 启动consul
    完成

    相关文章

      网友评论

        本文标题:hyperf JsonRpc demo

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