版权声明:转载请注明来源 https://blog.csdn.net/u013702678/article/details/82766627
通过前面几篇zk注册流程的解析,可以看到注册到zk时都是通过封装为FsofUrl对象来操作的,这个类主要实现了dubbo协议的封装,dubbo协议采用的是url协议,类似http url的方式,大家也可以提前了解下dubbo协议格式,dubbo在zk中存储中provider的信息时,路径的格式为:/dubbo/***/providers
,其中****是服务的地址(如com.foo.BarService),现在我们看下框架的协议拼接逻辑,协议拼接逻辑在:dubbo-php-framework-master/provider/core/server/FSOFRegistry.php文件中。
protected function ServiceSerialize()
{
try
{
unset($this->fsofUrlList);
if (!empty($this->serverProviders))
{
if ($this->ephemeral)
{
//注册时间
//$this->config["service_properties"]["timestamp"] = (int)(microtime(true) * 1000);
$this->config["service_properties"]["dynamic"] = "true";
}
else
{
$this->config["service_properties"]["dynamic"] = "false";
}
$services = $this->serverProviders;
foreach ($services as $interface => $serviceInfo)
{
//合并全局配置
if (isset($this->config["service_properties"]))
{
//接口配置优先级高于全局配置,所以$serviceInfo放后面
$serviceInfo = array_merge($this->config["service_properties"], $serviceInfo);
}
//不用上报的信息去掉
unset($serviceInfo['service']);
unset($serviceInfo['p2p_mode']);
if (empty($serviceInfo["version"]))
{
$serviceInfo['version'] = FSOFConstants::FSOF_SERVICE_VERSION_DEFAULT;
}
//与dubbo兼容处理
$serviceInfo['interface'] = $interface;//dubbo_admin需要使用
//序列化方式
$serviceInfo['serialization']= "fastjson";
ksort($serviceInfo);//参数排序,与dubbo兼容
$urlPara = array(
'scheme' => 'dubbo',
'host' => $this->localIp,
'port' => $this->port,
'path' => '/' . $interface,
//http_build_query会进行urlencode导致query参数被多编码一次,使用urldecode抵消
'query' => urldecode(http_build_query($serviceInfo)),
);
$this->logger->debug("serviceInfo:" . json_encode($serviceInfo) . "|urlPara:" . json_encode($urlPara));
try
{
$fsofUrl = new FSOFUrl($urlPara);
}
catch (\Exception $e)
{
$this->logger->error('init url failed|app:' . $this->appName . '|urlPara:' . json_encode($urlPara));
}
$this->fsofUrlList[] = $fsofUrl;
}
}
}
catch(\Exception $e)
{
$errMsg = $e->getMessage();
$this->logger->error('ServiceSerialize:'.$errMsg, $e);
}
}
这个逻辑就是遍历Provider提供出来的service信息,生成FsofUrl对象,最终生成fsofUrlList的结合,而单个FsofUrl对象生成时,传入的参数信息如下:schema为dubbo,host为当前Provider运行的服务器地址,port为Provider监听的端口信息,path为Provider的配置文件提供出来的服务地址信息(在框架提供出来的demo中,其interface信息为com.fenqile.example.DemoService),query字段是由多个字段通过调用PHP的http_build_query函数生成的。