介绍 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 App\Rpc\Lib;

/**
* Interface DemoInterface
*/
interface DemoInterface{
/**
* @return array
* @param int $id
*/
public function getLists(int $id): array ; /**
* @return string
*/
public function getBig():string ;
}

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

<?php
namespace App\Rpc\Service; use App\Rpc\Lib\DemoInterface;
use Swoft\Rpc\Server\Annotation\Mapping\Service; /**
* 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";
}
}

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

<?php
namespace App\Rpc\Service; use App\Rpc\Lib\DemoInterface;
use Swoft\Rpc\Server\Annotation\Mapping\Service; /**
* 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";
}
}
不同的地方在  @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 App\Http\Controller; use App\Rpc\Lib\DemoInterface;
use Exception;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Rpc\Client\Annotation\Mapping\Reference; /**
* 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

 

Swoft2.x 小白学习笔记 (四) --- RPC的更多相关文章

  1. Swoft2.x 小白学习笔记 (一) ---控制器

    Swoft通过官方文档进行学习,这里不做介绍,直接上手. 涉及到Swoft方面:(配置.注意的坑) 1.控制器(路由.验证器.中间件) 2.mysql  (Model使用).Redis配置及通用池 3 ...

  2. Swoft2.x 小白学习笔记 (二) --- mysql、redis

    介绍swoft中 1.mysql. 2.Redis 一.mysql使用: 1.配置,在 app\bean.php文件中 'db' => [ 'class' => Database::cla ...

  3. Swoft2.x 小白学习笔记 (三) --- Task、协程

    介绍swoft中 1.Task 2.协程 一:Task任务: 1.配置,在 app/bean.php文件中加入 'httpServer' => [ // ... 'on' => [ Swo ...

  4. go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)

    目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...

  5. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  6. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  7. java之jvm学习笔记四(安全管理器)

    java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...

  8. Learning ROS for Robotics Programming Second Edition学习笔记(四) indigo devices

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  9. Typescript 学习笔记四:回忆ES5 中的类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

随机推荐

  1. ie中兼容性问题

    由于项目要要兼容到ie8原本没有问题的代码一但用ie8打开js的报错找不到对象就都来了,其实总结起来就是ie越老的版本就越多方法名识别不到,那就少什么方法添加什么,比如说我的项目就要引入<scr ...

  2. 焦虑的 BAT、不安的编程语言,揭秘程序员技术圈生存现状!

    [程序人生编者按]在迭代不休的技术圈中,仅在过去的一个月期间,我们见证了有史以来第一张黑洞照片的诞生:经历了为让人义愤填膺的 996:思考了作为程序员的年龄之槛:膜拜了技术大神的成长历程:追逐了如编程 ...

  3. antd源码分析之——栅格(Grid)

    官方文档 https://ant.design/components/grid-cn/ 目录 一.antd中的Grid 代码目录 1.整体思路 2.less文件结构图(♦♦♦重要) 3.less实现逻 ...

  4. 我现在A函数开启事务,然后调用B函数,B函数中也开启了事务

    有一点要知道,就是MYSQL不支持事务嵌套.所以PHP再包装,也是一个事务 laravel的事务嵌套,就是一个栈.事务A开启事务(真实开启)事务B开启事务(只是标记,并非真实开启了事务)事务B提交事务 ...

  5. 笔记:YSmart: Yet Another SQL-to-MapReduce Translator

    http://web.cse.ohio-state.edu/hpcs/WWW/HTML/publications/papers/TR-11-7.pdf  Introduce样例sql语句:" ...

  6. svg简单的应用

    1.可以直接在html内写svg (1)width宽度,height高度 (2)xmlns svg的规则 <svg xmlns="http://www.w3.org/2000/svg& ...

  7. springboot多个数据源

    1.启动两个mysql,可以按照如下操作使用docker来部署mysql容器,比较简单 https://www.cnblogs.com/qq931399960/p/11527222.html 2.如果 ...

  8. SpringCloud(七)之SpringCloud的链路追踪组件Sleuth实战,以及 zipkin 的部署和使用

    一.前言 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案 ,并且兼容了zipkin,提供了REST API接口来辅助我们查询跟踪数据以实现对分布式系统的监控程序 . ...

  9. python之scrapy模拟登陆人人网

    1.settings.py主要配置信息,包括USER_AGENT等 # -*- coding: utf-8 -*- # Scrapy settings for renren project # # F ...

  10. vue3.x版本安装vue-cli建项目

    vue-cli版本在3以上 全局安装vue-cli npm install -g @vue/cli 建立项目工程,假设项目建在e:\vueProject\vue-cli3.0+目录下: 先进入此目录: ...