Skip to content

Commit

Permalink
1.fix bug with autoload protocol class
Browse files Browse the repository at this point in the history
2.show example communicating with map and object
  • Loading branch information
root committed Nov 25, 2016
1 parent 4b9ce74 commit 9f99423
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 115 deletions.
7 changes: 6 additions & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
$dubboCli = new dubboClient($options);
$testService = $dubboCli->getService("com.dubbo.demo.HelloService","1.0.0",null);
$ret = $testService->hello("dubbo php client");
echo $ret;
$mapRet = $testService->mapEcho();
$objectRet = $testService->objectEcho();

var_dump($ret);
var_dump($mapRet);
var_dump($objectRet);

?>
49 changes: 33 additions & 16 deletions src/dubboClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,60 @@
use \dubbo\invok\invokerDesc;
use \dubbo\invok\protocols;

/**
* Class dubboClient
* @package dubbo
*/
class dubboClient{
protected $register;
protected $protocols;
static $protocols;

public function __construct($options=array())
{
$this->register = new Register($options);
}

/**
* @param $serviceName (service name e.g. com.xx.serviceName)
* @param $version (service version e.g. 1.0)
* @param $group (service group)
* @param string $protocol (service protocol e.g. jsonrpc dubbo hessian)
* @return get| specific dubbo service with your params
*/
public function getService($serviceName, $version, $group, $protocol = "jsonrpc"){
$invokerDesc = new InvokerDesc($serviceName, $version, $group);
$invoker = $this->register->getInvoker($invokerDesc);
if(!$invoker){
//$invoker = new jsonrpc();
$invoker = $this->getInvokerByProtocol($protocol);
$invoker = $this->makeInvokerByProtocol($protocol);
$this->register->register($invokerDesc,$invoker);
}
return $invoker;
}

public function getInvokerByProtocol($protocol){

if(!in_array($protocol, $this->protocols)){
foreach( glob( "invok/protocols/*.php" ) as $filename ){
$protoName = basename($filename,".php");
array_push($this->protocols, $protoName);
require_once $filename;
}
/**
* @param $protocol
* @return get instance of specific protocol
*/
private function makeInvokerByProtocol($protocol){

if(array_key_exists(self::$protocols,$protocol)){
return self::$protocols[$protocol];
}

if(class_exists("dubbo\invok\protocols\\$protocol")){
$class = new \ReflectionClass("dubbo\invok\protocols\\$protocol");
$invoker = $class->newInstanceArgs(array());
return $invoker;
}else{
throw new \Exception("can't match the class according to this protocol $protocol");

foreach( glob( dirname(__FILE__)."/invok/protocols/*.php" ) as $filename ){
$protoName = basename($filename,".php");
require_once $filename;
if(class_exists("dubbo\invok\protocols\\$protoName")){
$class = new \ReflectionClass("dubbo\invok\protocols\\$protoName");
$invoker = $class->newInstanceArgs(array());
self::$protocols[$protoName] = $invoker;
}
}

return self::$protocols[$protocol];

}

}
Expand Down
222 changes: 124 additions & 98 deletions src/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,151 +10,177 @@

class Register{

public $config = array(
'registry_address' => '127.0.0.1:2181'
);
public $config = array(
'registry_address' => '127.0.0.1:2181',
'provider_timeout' => 5 //seconds
);

public $zookeeper = null;
public $zookeeper = null;

protected $ip;
protected $ip;

protected $providersCluster;
protected $providersCluster;

public static $ServiceMap = array();
public static $ServiceMap = array();

protected $acl = array(
array(
'perms' => \Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone' ) );
protected $acl = array(
array(
'perms' => \Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone' ) );

public function __construct($options = array())
{
$this->config = array_merge($this->config,$options);
$this->ip = $this->getRegisterIp();
$this->providersCluster = Cluster::getInstance();
$this->zookeeper= $this->makeZookeeper($this->config['registry_address']);
}

private function makeZookeeper($registry_address) {
return new \Zookeeper ($registry_address);
}

public function subscribe($invokDesc){
$desc = $invokDesc->toString();
$serviceName = $invokDesc->getService();

$path = $this->getSubscribePath($serviceName);
$children = $this->zookeeper->getChildren($path);
if(count($children) > 0){
foreach ($children as $key => $provider) {
$provider = urldecode($provider);
$this->methodChangeHandler($invokDesc, $provider);
}
$this->configurators();
}

}
public function __construct($options = array())
{
$this->config = array_merge($this->config,$options);
$this->ip = $this->achieveRegisterIp();
$this->providersCluster = Cluster::getInstance();
$this->zookeeper= $this->makeZookeeper($this->config['registry_address']);
}

public function register($invokDesc,$invoker){
$desc = $invokDesc->toString();
if(!array_key_exists($desc,self::$ServiceMap)){
self::$ServiceMap[$desc] = $invoker;
private function makeZookeeper($registry_address) {
return new \Zookeeper ($registry_address);
}
$this->subscribe($invokDesc);
$providerHost = $this->providersCluster->getProvider($invokDesc);
$invoker->setHost(Invoker::genDubboUrl($providerHost,$invokDesc));
$registerNode = $this->getRegistryNode($invokDesc->getService());
try {
$parts = explode('/', $registerNode);
$parts = array_filter($parts);
$subpath = '';
while (count($parts) > 1) {
$subpath .= '/' . array_shift($parts);
if (!$this->zookeeper->exists($subpath)) {
$this->zookeeper->create($subpath,'',$this->acl, null);

public function subscribe($invokDesc){
$desc = $invokDesc->toString();
$serviceName = $invokDesc->getService();

$path = $this->getSubscribePath($serviceName);
$children = $this->zookeeper->getChildren($path);
if(count($children) > 0){
foreach ($children as $key => $provider) {
$provider = urldecode($provider);
$this->methodChangeHandler($invokDesc, $provider);
}
$this->configurators();
}
}

/**
* @param $invokDesc
* @param $invoker
* @return bool
* Register consumer information node to the zookeeper.
*/
public function register($invokDesc, $invoker){
$desc = $invokDesc->toString();
if(!array_key_exists($desc,self::$ServiceMap)){
self::$ServiceMap[$desc] = $invoker;
}
if(!$this->zookeeper->exists($registerNode)) {
$this->zookeeper->create($registerNode, '', $this->acl, null);
$this->subscribe($invokDesc);
$providerHost = $this->providersCluster->getProvider($invokDesc);
$invoker->setHost(Invoker::genDubboUrl($providerHost,$invokDesc));
$registerNode = $this->makeRegistryNode($invokDesc->getService());
try {
$parts = explode('/', $registerNode);
$parts = array_filter($parts);
$subpath = '';
while (count($parts) > 1) {
$subpath .= '/' . array_shift($parts);
if (!$this->zookeeper->exists($subpath)) {
$this->zookeeper->create($subpath,'',$this->acl, null);
}
}
if(!$this->zookeeper->exists($registerNode)) {
$this->zookeeper->create($registerNode, '', $this->acl, null);
}
}catch (ZookeeperNoNodeException $ze){
error_log("This zookeeper node does not exsit.Please check the zookeeper node information.");
}
}catch (ZookeeperNoNodeException $ze){
error_log("This zookeeper node does not exsit.Please check the zookeeper node information.");
return true;
}
return true;
}


public function methodChangeHandler($invokerDesc, $provider){
$schemeInfo = parse_url($provider);
$providerConfig = array();
parse_str($schemeInfo['query'],$providerConfig);

if($invokerDesc->isMatch($providerConfig['group'],$providerConfig['version']))
{
$this->providersCluster->addProvider($invokerDesc,'http://'.$schemeInfo['host'].':'.$schemeInfo['port'],$schemeInfo['scheme']);
public function methodChangeHandler($invokerDesc, $provider){
$schemeInfo = parse_url($provider);
$providerConfig = array();
parse_str($schemeInfo['query'],$providerConfig);
if($invokerDesc->isMatch($providerConfig['group'],$providerConfig['version']))
{
$this->providersCluster->addProvider($invokerDesc,'http://'.$schemeInfo['host'].':'.$schemeInfo['port'],$schemeInfo['scheme']);
}
}
}


public function getInvoker($invokerDesc){
$desc = $invokerDesc->toString();
return self::$ServiceMap[$desc];
}
public function getInvoker($invokerDesc){
$desc = $invokerDesc->toString();
return self::$ServiceMap[$desc];
}



public function configurators(){
return true;
}
public function configurators(){
return true;
}



protected function getSubscribePath($serviceName){
return '/dubbo/' .$serviceName.'/providers';
}
protected function getSubscribePath($serviceName){
return '/dubbo/' .$serviceName.'/providers';
}

protected function getRegistryAddress() {
return $this->config['registry_address'];
}
protected function getRegistryAddress() {
return $this->config['registry_address'];
}


protected function getRegistryNode($serviceName, $application = array()){
/**
* @param $serviceName
* @param array $application
* @return string
* Make a dubbo consumer address for this node which want register itself under the dubbo service
*
*/
private function makeRegistryNode($serviceName, $application = array()){
$params = http_build_query($application);
$url = '/dubbo/'.$serviceName.'/consumers/'.urlencode('consumer://'.$this->ip.'/'.$serviceName.'?').$params;
return $url;
}

protected function getRegistryPath($serviceName, $application = array()){

/**
* @param $serviceName
* @param array $application
* @return string
*/
private function makeRegistryPath($serviceName, $application = array()){
$params = http_build_query($application);
$path = '/dubbo/'.$serviceName.'/consumers';
return $path;
}


protected function getConfiguratorsPath($serviceName){
return '/dubbo/'.$serviceName.'/configurators';
}

protected function getConfiguratorsPath($serviceName){
return '/dubbo/'.$serviceName.'/configurators';
}

protected function getProviderTimeout(){
return $this->config['providerTimeout'] * 1000;
}
protected function getProviderTimeout(){
return $this->config['provider_timeout'] * 1000;
}


public function zkinfo($invokerDesc){
echo $this->getRegistryPath($invokerDesc->getService());
var_dump($this->providersCluster->getProviders());
var_dump($this->providersCluster);
}
public function zkinfo($invokerDesc){
echo $this->getRegistryPath($invokerDesc->getService());
var_dump($this->providersCluster->getProviders());
var_dump($this->providersCluster);
}

private function getRegisterIp(){
/**
* @return stirn
* Get the consumer server local ip. If we can't get the ip from the environments,
* we will get from the command.
*/
private function achieveRegisterIp(){
try {
$registerIp = gethostbyaddr($_SERVER['SERVER_ADDR']);
if (empty($registerIp)) {
if (substr(strtolower(PHP_OS), 0, 3) != 'win') {
$ss = exec('/sbin/ifconfig | sed -n \'s/^ *.*addr:\\([0-9.]\\{7,\\}\\) .*$/\\1/p\'', $arr);
$registerIp = $arr[0];
}else{
/** @TODO
* @ implement the windows ip
**/
}
}
}catch (\Exception $e){
Expand Down

0 comments on commit 9f99423

Please sign in to comment.