zoukankan      html  css  js  c++  java
  • Swoft2.x 小白学习笔记 (四) --- RPC

    介绍 swoft 中 RPC使用:搭建访问服务端和客户端

       

    RPC服务端:

    一、配置,在文件 /app/bean.php中添加

    return [
        'rpcServer'  => [
            'class' => ServiceServer::class,
            'port' => 18308,
        ],
    ]
    
    Http server 启动中集成 RPC 服务:
    return [
        'httpServer' => [
            'class'    => HttpServer::class,
            'port'     => 18306,
            'listener' => [
                'rpc' => bean('rpcServer')
            ],
    
            // ...
        ],
    ]

    二、使用

       1、定义接口,服务提供方定义好接口格式,存放到公共的lib库里面,服务调用方,加载lib库,就能使用接口服务,接口定义和普通接口完全一致。

     在/app/Rpc/Lib/ 文件夹下添加文件DemoInterface.php:

    <?php
    
    namespace AppRpcLib;
    
    /**
     * Interface DemoInterface
     */
    interface DemoInterface{
        /**
         * @return array
         * @param int $id
         */
        public function getLists(int $id): array ;
    
        /**
         * @return string
         */
        public function getBig():string ;
    }
    View Code

      2、接口实现,在文件夹 /app/Rpc/Service/ 下添加文件DemoService.php

    <?php
    namespace AppRpcService;
    
    use AppRpcLibDemoInterface;
    use SwoftRpcServerAnnotationMappingService;
    
    /**
     * Class DemoService
     *
     * @Service(version="1.0") //定义版本
     *
     */
    class DemoService implements DemoInterface{
        /**
         * @param int $id
         * @return array
         */public function getLists(int $id): array
            {
                // TODO: Implement getLists() method.
    
                return ["id" => $id];
            }
    
        /**
         * @return string
         */
        public function getBig(): string
        {
            // TODO: Implement getBig() method.
    
            return "ddddddd";
        }
    }
    View Code

         定义版本2,在文件夹 /app/Rpc/Service/ 下添加文件DemoServiceV2.php

    <?php
    namespace AppRpcService;
    
    use AppRpcLibDemoInterface;
    use SwoftRpcServerAnnotationMappingService;
    
    /**
     * Class DemoService
     *
     * @Service(version="1.2") //定义版本1.2
     *
     */
    class DemoServiceV2 implements DemoInterface{
    
        /**
         * @param int $id
         * @return array
         */public function getLists(int $id): array
        {
            // TODO: Implement getLists() method.
    
            return ["id" => $id, "ver" => "1.2"];
        }
    
        /**
         * @return string
         */
        public function getBig(): string
        {
            // TODO: Implement getBig() method.
    
            return "dddd_V2";
        }
    }
    View Code
    不同的地方在  @Service(version="1.2") ,定义不同版本

      3、启动访问:

    //单独启动rpc
    php bin/swoft rpc:start
    
    //启动http、伴随启动RPC
    php bin/swoft http:start

    RPC客户端:服务调用方法,通过使用服务提供方法,提供的lib接口,调用接口实现服务,不需要了解实现细节

      一:配置,客户端也需要安装swoft,在其/app/bean.php 中添加

     //定义RPC客户端连接,TCP方式,端口为RPC服务器端口
     return [
      
    'user' => [ 'class' => ServiceClient::class, 'host' => '127.0.0.1', //服务端IP 'port' => 18307, //服务端RPC的端口 'setting' => [ 'timeout' => 0.5, 'connect_timeout' => 1.0, 'write_timeout' => 10.0, 'read_timeout' => 0.5 ], 'packet' => bean('rpcClientPacket') ], 'user.pool' => [ 'class' => ServicePool::class, 'client' => bean('user'), ],
    ]

      二:使用:

        (1) : 拷贝服务端的 /app/Rpc/Lib/ 文件夹到客户端的 /app/Rpc/Lib/

        (2) :在客户端的 /app/Http/Controller/ 文件夹下添加控制器 RpcClientController.php

    <?php
    namespace AppHttpController;
    
    use AppRpcLibDemoInterface;
    use Exception;
    use SwoftHttpServerAnnotationMappingController;
    use SwoftHttpServerAnnotationMappingRequestMapping;
    use SwoftRpcClientAnnotationMappingReference;
    
    /**
     * Class RpcClientController
     *
     * @Controller(prefix="/rpcApi") //定义路由
     *
     */
    class RpcClientController{
        /**
         * @Reference(pool="user.pool")   //pool 指定使用那个服务的连接池(使用那个服务),version 指定服务的版本
         *
         * @var DemoInterface
         */
        private $userSer;
    
        /**
         * @Reference(pool="user.pool", version="1.2") //pool 指定使用那个服务的连接池(使用那个服务),version 指定服务的版本
         *
         * @var DemoInterface
         */
        private $userSerV2;
        
        /**
         * @RequestMapping("rpcV1")   //访问路由 /rpcApi/recV1/
         */
        public function getRpcApiV1(): array {
            $result = $this->userSer->getLists(21);   //调用1.0版本接口
            $resultV2 = $this->userSerV2->getLists(33);  //调用1.2版本接口
    
            return [$result,$resultV2];
        }
    
        /**
         * @return array
         * @RequestMapping("rpcV2")
         */
        public function getBigString(): array {
            $bigV1 = $this->userSer->getBig();
            $bigV2 = $this->userSerV2->getBig();
    
            return [strlen($bigV1),strlen($bigV2)];
        }
    }

    使用非swoft客户端框架访问RPC ,参考官方文档 : https://www.swoft.org/docs/2.x/zh-CN/rpc-client/usage.html

    参考文档:https://www.swoft.org/docs/2.x/zh-CN/rpc-server/index.html

           https://www.swoft.org/docs/2.x/zh-CN/rpc-client/index.html

         与Swoft RPC Server通信的Client扩展:  https://www.ctolib.com/article/compares/91157

     
  • 相关阅读:
    StringBuffer
    判断java标识符
    基本数据类型和String相互转换
    使用String
    Properties属性文件
    Map排序
    java集合之三映射:HashMap、Hashtable、LinkedHashMap、TreeMap
    列表、集合与数组之间相互转换
    自定义列表排序
    ML-支持向量:SVM、SVC、SVR、SMO原理推导及实现
  • 原文地址:https://www.cnblogs.com/cj8988/p/11607607.html
Copyright © 2011-2022 走看看