zoukankan      html  css  js  c++  java
  • 手把手编写hyperf JsonRpc demo

    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 HyperfJsonRpcJsonRpcPoolTransporter;
    use HyperfJsonRpcJsonRpcTransporter;
    
    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 => [HyperfHttpServerServer::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 => [HyperfJsonRpcHttpServer::class, 'onRequest'],
                ],
            ],
            [
                'name' => 'jsonrpc',
                'type' => Server::SERVER_BASE,
                'host' => '0.0.0.0',
                'port' => 9603,
                'sock_type' => SWOOLE_SOCK_TCP,
                'callbacks' => [
                    SwooleEvent::ON_RECEIVE => [HyperfJsonRpcTcpServer::class, 'onReceive'],
                ],
                'settings' => [
                    'open_eof_split' => true,
                    'package_eof' => "
    ",
                ],
            ],
        ],

    新建服务

    CalculatorService.php
    <?php
    
    namespace AppJsonRpc;
    
    use HyperfRpcServerAnnotationRpcService;
    
    /**
     * 注意,如希望通过服务中心来管理服务,需在注解内增加 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 AppJsonRpc;
    
    
    /**
     * Class CalculatorService
     * @package AppJsonRpc
     */
    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 HyperfJsonRpcJsonRpcPoolTransporter;
    use HyperfJsonRpcJsonRpcTransporter;
    
    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 => [HyperfHttpServerServer::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 => [HyperfJsonRpcHttpServer::class, 'onRequest'],
                ],
            ],
            [
                'name' => 'jsonrpc',
                'type' => Server::SERVER_BASE,
                'host' => '0.0.0.0',
                'port' => 9503,
                'sock_type' => SWOOLE_SOCK_TCP,
                'callbacks' => [
                    SwooleEvent::ON_RECEIVE => [HyperfJsonRpcTcpServer::class, 'onReceive'],
                ],
                'settings' => [
                    'open_eof_split' => true,
                    'package_eof' => "
    ",
                ],
            ],
        ],

    services.php

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

    完成

     
  • 相关阅读:
    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用
    分享几个.NET WinForm开源组件,纪念逐渐远去的WinForm。。。
    小议主子表INT自增主键插入记录的方法SQL server]教程
    微软工程师主讲的SqlServer2005视频教程
    11 个重要的数据库设计规则
    sql数据库设计学习---数据库设计规范化的五个要求
    YY淘宝商品数据库设计
    ASP.NET MVC的客户端验证:jQuery的验证
    ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现
    UNIX标准化及实现之POSIX标准可选头文件
  • 原文地址:https://www.cnblogs.com/guokefa/p/12174000.html
Copyright © 2011-2022 走看看