Skip to content
Jinxi Wang edited this page Nov 29, 2019 · 31 revisions

快速索引

使用教程

composer require crazyxman/dubbo-php-framework:dev-master

依赖扩展: swoole,zookeeper,bcmath,yaml。

配置文件说明(模板:dubbo-php-framework/src/agent/Config/AgentConfig.yaml)

字段 实例值 默认值 是否必须 说明
application.name dubbo-agent agent名称,会显示在进程名中,不会存在同一名称的多个进程
application.log_dir ./log 日志存放目录
application.log_level INFO INFO 指定日志级别,可选级别:DEBUG,INFO,WARN,ERROR
application.consumer_config_file config/ConsumerConfig.yaml agent会对consumer_config_file文件中reference字段指定的服务进行订阅
registry.protocol zookeeper 注册中心类型
registry.address 127.0.0.1:2181 注册中心地址
server.host 127.0.0.1 127.0.0.1 agent对外提供的服务地址
server.port 9091 9091 agent对外提供的服务端口
server.daemonize 0 0 是否以守护进程运行
server.unixsocket /var/run/dubbo-php-framework/dubbo-agent.sock 是否提供unixsocket方式访问
server.pid_file /var/run/dubbo-php-framework/dubbo-agent.pid agent pid防止同名称的agent重复启动
swoole_table.size 1000 1000 可以订阅的服务数量
swoole_table.column_size 10240 10240 每个服务的所有提供者会被放进数组后进行json_encode然后进行存储,此值就是序列化后的大小,当提供者较多时可适当调大此值
watch_nodes[] 此值是一个数组,除consumer_config_file外要订阅的服务

启动agent:

php dubbo-php-framework/bin/DubboAgent.php -y AgentConfig.yaml

注意: 无论php调php还是php调java,都需要先启动Agent
consumer可工作在php-fpm,cli
配置文件说明(模板:dubbo-php-framework/src/rpc/Config/ConsumerConfig.yaml)

字段 实例值 默认值 是否必须 说明
discoverer.host 127.0.0.1 127.0.0.1 agent host
discoverer.port 9091 9091 agent port
discoverer.retry 1 0 重试次数
discoverer.unixsocket /var/run/dubbo-php-framework/dubbo-agent.sock unixsocket路径
application.name simple-demo 提供者名,会显示在进程名中,启多个提供者时不可重复
application.environment develop 当前环境,暂无用到
application.log_dir ./logs ./logs 日志路径
application.log_level INFO INFO 指定日志级别,可选级别:DEBUG,INFO,WARN,ERROR
application.pid_file /var/run/dubbo-php-framework/simple-monitor.pid 进程pid存放路径,用于stop,reload,防止同名进程多起
parameter.version 1.0.0 默认要使用的provider version
parameter.group * 默认要使用的provider group
parameter.retry 1 0 默认要重试的次数,调用provider 连接超时,io超时后要重试的次数
parameter.timeout 1 10 默认超时设置,包含连接超时,io超时,指定时间内未接受完数据也会超时,单位秒
parameter.client_ip 172.168.81.53 swoole_get_local_ip() 向monitor发送数据时需要用到,如果未设置默认使用swoole_get_local_ip()获取
reference.[].service_name php.dubbo.demo.DemoService 要使用的service
reference.[].url dubbo://127.0.0.1:20880/php.dubbo.demo.DemoService?version=1.0.0 直连provider
reference.[].retry 1 0 指定重试的次数
reference.[].timeout 指定超时时间
reference.[].query[].group * 指定要使用的provider version
reference.[].query[].version 1.0.0 指定要使用的provider group
//php调php或者php调java 下述代码是通用的 
include "vendor/autoload.php";
use Dubbo\Consumer\DubboConsumer;
use Dubbo\Common\Protocol\Dubbo\DubboParam;

$consumerConfig = 'ConsumerConfig.yaml';
//第二个参数可选,如果设置会将解析后的yaml结果缓存到指定文件,下次调用将直接读取缓存,更改配置文件后一定要删除缓存文件!
$instance = DubboConsumer::getInstance($consumerConfig,'/tmp/ConsumerConfigCache.php'); 
$service = $instance->loadService('php.dubbo.demo.DemoService');//同一service只需加载一次
$res = $service->invoke('sayHello', 100, "hello"); //多个参数

/*

// When the argument is an Integer
$service = $instance->loadService('com.imooc.springboot.dubbo.demo.IntegerDemoService');
$res = $service->invoke('sayHello', 20880);

// When the argument is an String
$service = $instance->loadService('com.imooc.springboot.dubbo.demo.StringDemoService');
$res = $service->invoke('sayHello', "hello");

// When the argument is an Map
$service = $instance->loadService('com.imooc.springboot.dubbo.demo.MapDemoService');
$res = $service->invoke('sayHello', ['a'=>'b']);

// When the argument is an ArrayList
$service = $instance->loadService('com.imooc.springboot.dubbo.demo.ArrayListDemoService');
$res = $service->invoke('sayHello', [2,3,4]);

// When the argument is an LinkedList
$service = $instance->loadService('com.imooc.springboot.dubbo.demo.LinkedListDemoService');
$res = $service->invoke('sayHello', DubboParam::Type('java.util.LinkedList', ['a', 'b']));

// When the argument is an object
$service = $instance->loadService('com.imooc.springboot.dubbo.demo.ObjectDemoService');
$res = $service->invoke('sayHello',
    DubboParam::object(
        'com.imooc.springboot.dubbo.demo.dto.TestObjectDemo',
        [
            "name" => "Tom",
            "age" => 30,
            'bigDecimal' => DubboParam::object('java.lang.Object', ['value' => 15.6])
        ])
);

 */
  • Provider 根据配置文件中提供的 service.namespace 命名空间用来指定服务范围。

  • 服务初始化:命名空间下的DubboBootstrap.php 文件在服务启动之前会被加载,可以用来设置swoole_server的一些回调函数。

  • 注册服务:自动查找该命名空间下的所有文件,根据文件中的注解将服务注册到Zookeeper,所有被注测的服务方法均以入口方法dubboIngress() 进行调用。详见demo/Service/Demo.php。

启动服务:

#启动服务
php dubbo-php-framework/bin/DubboManager.php -y ProviderConfig.yaml
#停止服务
php dubbo-php-framework/bin/DubboManager.php -y ProviderConfig.yaml -s stop
#平滑重启,重启只对已注册的服务生效,如要取消已注册的服务需要停止后启动
php dubbo-php-framework/bin/DubboManager.php -y ProviderConfig.yaml -s reload

配置文件说明(模板:dubbo-php-framework/src/rpc/Config/ProviderConfig.yaml)

字段 实例值 默认值 是否必须 说明
swoole_settings swoole_server的配置选项,选项名称一一对应
parameter.dubbo_version 2.5.3 2.5.3 dubbo版本,会显示在provider url中
application.name simple-demo 服务名称,会在进程名中出现,必须唯一
application.environment develop 当前环境,暂无使用
application.log_dir ./logs ./logs 日志路径
application.log_level INFO INFO 指定日志级别,可选级别:DEBUG,INFO,WARN,ERROR
application.pid_file /var/run/dubbo-php-framework/simple-demo.pid server pid防止同名称的server重复启动,文件名必须唯一
protocol.host 127.0.0.1 对外暴露的地址,显示在provider url中
protocol.port 20880 对外暴露端口
protocol.serialization hessian2 hessian2 对外提供的序列化方式,支持fastjson,hessian2
registry.protocol zookeeper 注册中心类型,目前只支持zookeeper
registry.address 127.0.0.1:2181,127.0.0.1:2182 zookeeper地址
monitor.protocol registry 监控服务发现方式,只支持从注册中心获取
monitor.service com.alibaba.dubbo.monitor.MonitorService com.alibaba.dubbo.monitor.MonitorService monitor服务名称
provider.group * 默认暴露服务的group,如果在类注解中指定,此设置无效
provider.version 1.0.0 默认暴露服务的version,如果在类注解中指定,此设置无效
service.namespace DubboDemo\\Service\\  此命名空间下被注解过的类将对外提供服务

服务类加载规则遵循PSR-4规范。
服务类示例(/demo/Service/Demo.php):

namespace DubboDemo\Service;

use Dubbo\Provider\Annotations\DubboClassAnnotation;
use Dubbo\Provider\Annotations\DubboMethodAnnotation;

/**
 *
 * @DubboClassAnnotation(serviceAlias="php.dubbo.demo.DemoService", version="", group="")
 */
class Demo
{

    /**
     * @DubboMethodAnnotation
     */
    public function sayHello($args)
    {
        print_r($args);
        return "Dubbo sayHello!";
    }

    public static function dubboIngress($method, $args, $server, $fd, $reactor_id)
    {
        $_self = new self();
        return call_user_func_array([$_self, $method], $args);
    }
}

@DubboClassAnnotation : 标识此类作为一个服务,serviceAlias对外暴露的服务名称,version要暴露的版本号,group要暴露的组。
@DubboMethodAnnotation : 标识服务对外暴露的方法。
dubboIngress() : 作为对此服务的所有方法的调用的入口。

目前只对provider做了监控数据的收集。
开启monitor只需在ProviderConfig.yaml配置文件中添加以下配置即可:

monitor:
  protocol: "registry"
  service: "com.alibaba.dubbo.monitor.MonitorService"

设置自定义Logger
自定义的Logger需要实现LoggerInterface接口,通过LoggerFacade::setLogger()设置,就可以使用了
LoggerFacade::setLogger() 一定要在DubboConsumer::getInstance() 之前调用

include "vendor/autoload.php";
use Dubbo\Consumer\DubboConsumer;
use Dubbo\Common\Protocol\Dubbo\DubboParam;
use Dubbo\Common\Logger\LoggerInterface;
use Dubbo\Common\Logger\LoggerFacade;

//自定义logger
class CustomLogger implements LoggerInterface {}
LoggerFacade::setLogger(new CustomLogger()); 

$consumerConfig = 'ConsumerConfig.yaml';
$instance = DubboConsumer::getInstance($consumerConfig);
$service = $instance->loadService('php.dubbo.demo.DemoService');//同一service只需加载一次
$res = $service->invoke('sayHello', 100, "hello"); //多个参数

java调php服务时使用泛化调用即可

ApplicationConfig application = new ApplicationConfig();
application.setName("simple-demo");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");
application.setRegistry(registry);
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setInterface("php.dubbo.demo.DemoService");
reference.setGeneric(true);
reference.setApplication(application);
GenericService genericService = reference.get();
String a = (String)genericService.$invoke("sayHello", new String[]{String.class.getName()}, new Object[]{"hello"});
System.out.println(a);
#step.1
#修改consumer,provider,agent配置文件中的注册中心地址,确保log_dir,unixsocket,pid_file所在位置有可写权限,以及consumer_config_file所指consumer配置文件的位置。

cd vendor/crazyxman/dubbo-php-framework/bin;
#step.2
php DubboManager.php -y ../src/rpc/Config/ProviderConfig.yaml

#step.3
php DubboAgent.php -y ../src/agent/Config/AgentConfig.yaml

#step.4
cd vendor/crazyxman/dubbo-php-framework
php demo/Consumer.php

Clone this wiki locally