alex-qiu/dubbo-php-client

1.0.5 2021-04-26 10:35 UTC

This package is auto-updated.

Last update: 2024-09-26 18:12:08 UTC


README

License

dubbo php客户端(以下为中文说明)

Dubbo是一个分布式服务框架,赋予应用高效率RPC远程服务调用的能力。

这仅是Dubbo PHP客户端的实现。目前仅支持jsonRPC。
你可以查看我之前写的dubbo-jsonRPC-demo示例。

注意

在使用前,你必须启动Dubbo和Zookeeper,并首先注册服务提供者。

安装

如果你尚未安装PHP的zookeeper扩展,那么

sudo apt-get install php-pear php5-dev make  
sudo pecl install zookeeper

在安装zookeeper扩展时可能会出现“zookeeper支持需要libzookeeper”的错误,这意味着你需要安装所需的libzookeeper库。

cd ${your zookeeper home dir}/src/c/
./configure
make
sudo make install

将zookeeper.so添加到你的php.ini文件中(/etc/php5/apache2/php.ini和/etc/php5/cli/php.ini)

extension="/usr/lib/php5/20121212/zookeeper.so"  
将dubbo-php-client包添加到你的项目中(composer)
composer require -vvv "quickj/dubbo-php-client:dev-master"

使用

use DubboPhp\Client\Client;

$options = [
    'registry_address' => '127.0.0.1:2181',
    'version' => '1.0.0',
    'group' =>null,
    'protocol' => 'jsonrpc'
];

try {
    $dubboCli = new Client($options);
    $testService = $dubboCli->getService("com.dubbo.demo.HelloService");
    $ret = $testService->hello("dubbo php client");
    var_dump($ret);
    $mapRet = $testService->mapEcho();
    var_dump($mapRet);

    $objectRet = $testService->objectEcho();
    var_dump($objectRet);

    /**
     * getService method support 2 way. If the forceVgp = true, It will assign the function parameter to service version,group and protocol. Default way is assign the $options configs to these.
     * getService支持两种方式调用。如果forceVgp=true, 该方法将使用传参来绑定服务的版本号,组和协议。默认方式是使用$options数组里的配置绑定。
     */
    $testServiceWithvgp = $dubboCli->getService("com.dubbo.demo.HelloService","1.0.0",null, $forceVgp = true);
    $vgpRet = $testServiceWithvgp->hello("this request from vgp");
    var_dump($vgpRet);
} catch (\DubboPhp\Client\DubboPhpException $e) {
    print($e->getMessage());
}

dubbo-php-client中文说明

DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架。
这是Dubbo的唯一PHP客户端,目前只支持jsonRPC协议,将来会支持多种协议。你可以查看我之前写的dubbo-jsonRPC-demo例子。

注意

在使用前,你必须安装和启动Dubbo、Zookeeper,并注册服务提供者。

安装

如果你还没有安装php的zookeeper扩展,那么

sudo apt-get install php-pear php5-dev make  
sudo pecl install zookeeper

在安装过程中可能会出现“zookeeper支持需要libzookeeper”的错误,这表明缺少libzookeeper库,你需要首先安装该库。

cd ${your zookeeper home dir}/src/c/
./configure
make
sudo make install

将zookeeper.so添加到你的php.ini文件中(/etc/php5/apache2/php.ini和/etc/php5/cli/php.ini)
添加以下行

extension="/usr/lib/php5/20121212/zookeeper.so"

按照Composer规范使用dubbo-php-client

composer require -vvv "quickj/dubbo-php-client:dev-master"

调用示例-直接类调用:

use DubboPhp\Client\Client;

$options = [
    'registry_address' => '127.0.0.1:2181',
    'version' => '1.0.0',
    'group' =>null,
    'protocol' => 'jsonrpc'
];

try {
    $dubboCli = new Client($options);
    $testService = $dubboCli->getService("com.dubbo.demo.HelloService");
    $ret = $testService->hello("dubbo php client");
    var_dump($ret);
    $mapRet = $testService->mapEcho();
    var_dump($mapRet);

    $objectRet = $testService->objectEcho();
    var_dump($objectRet);

    /**
     * getService method support 2 way. If the forceVgp = true, It will assign the function parameter to service version,group and protocol. Default way is assign the $options configs to these.
     * getService支持两种方式调用。如果forceVgp=true, 该方法将使用传参来绑定服务的版本号,组和协议。默认方式是使用$options数组里的配置绑定。
     */
    $testServiceWithvgp = $dubboCli->getService("com.dubbo.demo.HelloService","1.0.0",null, $forceVgp = true);
    $vgpRet = $testServiceWithvgp->hello("this request from vgp");
    var_dump($vgpRet);
} catch (\DubboPhp\Client\DubboPhpException $e) {
    print($e->getMessage());
}

Laravel组件模式安装

config/app.php的

providers数组中增加:

DubboPhp\Client\Providers\DubboServiceProvider::class

aliases别名数组中增加:

'DubboPhpClient'=>DubboPhp\Client\Facades\DubboPhpClient::class,

'DubboPhpClientFactory'=>DubboPhp\Client\Facades\DubboPhpClientFactory::class,

然后通过命令行将系统基本配置文件dubbo_cli.php发布到config路径:

php artisan vendor:publish --provider="DubboPhp\Client\DubboPhpClientServiceProvider"

基本安装配置完成,相关的配置在config('dubbo_cli.default')中设置,具体参考配置文件

Laravel中的使用:

单实例方式(配置读取config('dubbo_cli.default')):

$testService = DubboPhpClient::getService('com.dubbo.demo.HelloService');

$ret = $testService->hello("dubbo php client");
var_dump($ret);
    

多实例的方式(配置读取config('dubbo_cli.connections.xxx')):

$clientA = DubboPhpClientFactory::factory(config('dubbo_cli.connections.xxxA'));
$testServiceA = $clientA->getService('com.dubbo.demo.HelloService');
$retA = $testServiceA->hello("dubbo php client");
var_dump($retA);

$clientB = DubboPhpClientFactory::factory(config('dubbo_cli.connections.xxxB'));
$testServiceB = $clientB->getService('com.dubbo.demo.HelloService');
$retB = $testServiceB->hello("dubbo php client");
var_dump($retB);