Download the PHP package wuzhc/swrpc without Composer

On this page you can find all versions of the php package wuzhc/swrpc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package swrpc

简介

swrpc是一个基于swoole开发的高性能rpc包,swrpc提供了注册发现,链路追踪,中间件等等功能,可以很容易集成到第三方框架,如laravel,yii等等。

功能

安装

快速体验

假设我们User和School两个模块,为了获得用户学校名称,我们需要在User模块发起rpc请求调用School模块的服务。

School模块

School模块将作为rpc服务端,对外提供服务,启动如下:

将SchoolService添加到server,server会自动检索类中所有可用的public方法。

1615562843265

User模块

User模块作为客户端,调用School模块服务如下

注意:

多进程和协程模式

多进程或协程模式需要和swoole配置一致,具体参考swoole配置

多进程模式

创建10进程来处理请求

协程模式

目前swrpc协程模式是运行在单进程的

同步调用和异步调用

在客户端发起同步调用,客户端会一直等待服务端返回结果

在客户端发起异步调用,客户端会立马得到响应结果,请求将被swoole的task进程处理

自定义中间件

中间件允许程序可以对请求进行前置操作和后置操作,底层使用了责任链设计模式,所以为了执行下一个中间件,必须返回$next($request),如果想提前返回,则返回结果必须是Swrpc\Response类型

如果要提前中止中间件,可以提前在匿名函数或类方法中返回\Swrpc\Response对象,如下

服务端按类提供对外服务

从上面的例子中,我们把SchoolService整个类添加的server中,这样server就能对外提供SchoolService类所有public方法的功能。

客户端使用参考上面的快速体验

注册服务发现

如果服务端启动的时候有设置注册中心,则启动成功会自动向注册中心注册服务端地址。目前swrpc提供了Consul作为注册中心,使用如下

如上,使用Consul作为服务的注册中心,通过http://127.0.0.1:8500可以查看注册信息,如果想用etcd等其他注册中心,只要实现Swrpc\Middlewares\RegisterInterface接口即可,然后在通过$server->addRegister()添加到server

1615562878292

1615562927956

1615562975815

客户端负载均衡

如果服务端启动多个节点,例如School模块启动3个节点,并且注册到了注册中心,那么我们可以从注册中心获取所有服务端节点信息,然后做一些策略处理。

目前swrpc提供两种简单策略模式,\Swrpc\Client::STRATEGY_WEIGHT权重模式\Swrpc\Client::STRATEGY_RANDOM随机模式

链路追踪

当我们的服务非常多并且需要互相调用时候,如果其中某个调用失败,会导致我们得不到我们想要的结果,而要调试出是哪个环节出了问题也比较麻烦,可能你需要登录每台机器看下有没有错误日志,或者看返回的错误信息是哪个服务提供的。链路追踪记录了整个调用链过程,如果某个环节出错,我们可以快速从调用链得到调用中断地方。

1615563070157

如图,User_Module调用Class_Module,Class_Module又去调用School_Module

1615563170709

每个调用还记录响应结果

自定义日志处理器

默认使用Monolog/Logger作为日志处理器,日志信息会输出到控制台。可根据自己需求覆盖默认处理器,只要日志类 实现Psr\Log\LoggerInterface即可

序列化方式

默认使用固定头+包体来解决tcp粘包问题,默认配置为'package_length_type' => 'N','package_body_offset' => 4 默认序列化数据会使用serialize(),如果swoole版本在4.5以上的自动使用swoole_substr_unserialize(),可以实现的类来覆盖默认配置,只要实现src/Packer/PackerInterface即可,注意服务端和客户端需要使用一样的协议,否则解析不了。

安全证书配置

参考:https://wiki.swoole.com/#/server/setting?id=ssl_cert_file

服务端

注意:

测试

使用phpuint实现的简单测试案例,配置文件phpunit.xml,根据你的服务器配置ip地址

1615602809212

phpunit 测试报告


All versions of swrpc with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
psr/log Version ^1.0
monolog/monolog Version ^2.2
sensiolabs/consul-php-sdk Version ~4.0
openzipkin/zipkin Version ~2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wuzhc/swrpc contains the following files

Loading the files please wait ....