Skip to content
Permalink
Browse files
update
  • Loading branch information
wangjinxi committed Jun 24, 2019
1 parent b1479a6 commit c7268757122341c3b667d1e4d94f3ef9105a90ef
Showing 8 changed files with 97 additions and 410,951 deletions.

This file was deleted.

This file was deleted.

@@ -29,6 +29,13 @@ class FSOFConstants
//monitor定时监控时间,暂定5分钟
const FSOF_MONITOR_TIMER = 300000;

//所使用的redis端口号
//默认所使用的redis端口号
const FSOF_SERVICE_REDIS_PORT =6379;
//默认所使用的redis地址
const FSOF_SERVICE_REDIS_HOST ='127.0.0.1';

const FSOF_SERVICE_REDIS_CONNECT_TYPE_TCP = 'TCP';

const FSOF_SERVICE_REDIS_CONNECT_TYPE_SOCK = 'SOCK';

}
@@ -29,14 +29,24 @@ class FSOFRedis
private $m_redis = null;

private $logger;

private $timeout = self::REDIS_TIME_OUT;

private $retry_count = 1;

private $connect_type = FSOFConstants::FSOF_SERVICE_REDIS_CONNECT_TYPE_TCP;

private $hosts = [
[FSOFConstants::FSOF_SERVICE_REDIS_HOST, FSOFConstants::FSOF_SERVICE_REDIS_PORT],
];

public static function instance()
public static function instance($config)
{
if (extension_loaded('redis'))
{
if (!isset(FSOFRedis::$_instance))
{
FSOFRedis::$_instance = new FSOFRedis();
FSOFRedis::$_instance = new FSOFRedis($config);
}
return FSOFRedis::$_instance;
}
@@ -47,41 +57,73 @@ public static function instance()
return NULL;
}

public function __construct()
public function __construct($config)
{
$this->logger = \Logger::getLogger(__CLASS__);
if($config['redis_hosts'])
{
$this->hosts = [];
$address = explode(',', $config['redis_hosts']);
foreach ($address as $node){
list($host, $port) = explode(':', $node);
$this->hosts[] = [$host, $port??FSOFConstants::FSOF_SERVICE_REDIS_PORT];
}
}
if($config['redis_connect_timeout'])
{
$this->timeout = $config['redis_connect_timeout'];
}
if($config['redis_connect_type'])
{
$this->connect_type = $config['redis_connect_type'];
}
if($config['redis_retry_count'])
{
$this->retry = min($config['redis_retry_count'], 1);
}

$this->get_redis();
}

public function get_redis()
{
if (!isset($this->m_redis))
{
try
{
$redis_cli = new \Redis();
$ret = $redis_cli->connect("127.0.0.1",FSOFConstants::FSOF_SERVICE_REDIS_PORT,self::REDIS_TIME_OUT);
if (!$ret)
{
$this->logger->warn("connect redis failed[127.0.0.1:6379]");
$ret = $redis_cli->connect("/var/fsof/redis.sock",-1,self::REDIS_TIME_OUT);
}
}
catch (\Exception $e)
{
$ret = false;
$this->logger->error('connect redis excepiton:'.$e->getMessage().', errno:' . $e->getCode());
throw new \Exception($e->getMessage());
}

$hosts_count = count($this->hosts);
$retry = $this->retry_count;
$rand_num = rand() % $hosts_count;
do{
try{
$redis_cli = new \Redis();
if($this->connect_type == FSOFConstants::FSOF_SERVICE_REDIS_CONNECT_TYPE_SOCK)
{
$node = $this->hosts[$rand_num];
$ret = $redis_cli->connect($node[0],$node[1],$this->timeout);
$rand_num = ($rand_num + 1)%$hosts_count;
if (!$ret)
{
$this->logger->warn("connect redis failed[{$node[0]}:{$node[1]}]");
}
}else{
$ret = $redis_cli->connect("/var/fsof/redis.sock",-1,FSOFConstants::FSOF_SERVICE_REDIS_PORT,$this->timeout);
}
if($ret)
{
$e = null;
break;
}
}catch (\Exception $e){
$this->logger->error('connect redis excepiton:'.$e->getMessage().', errno:' . $e->getCode());
}
}while($retry-- > 0);
if($ret)
{
$this->m_redis = $redis_cli;
}
else
{
$this->logger->error('connect redis failed:|errno:' . $redis_cli->getLastError());
throw new \Exception("连接本地redis异常");
throw new \Exception("连接redis异常");
}
}

@@ -19,16 +19,16 @@ zklog_level = 0
zklog_path = '/home/devops/workspace/dubbo/logs/zookeeper.log'

;zookeeper url list
zk_url_list = http://192.168.214.148:2181
zk_url_list = http://192.168.214.148:2181,http://192.168.214.148:2182,http://192.168.214.148:2183

;provider overload mode switch
overload_mode = true

;if request wait more than waiting_time before processed, we will lost this quest, unit is micro-second
;if request wait more than waiting_time before processed, we will lost this quest, unit is micro-second
waiting_time = 2000

;if overload_number requests trigger overload rule continuous, we will open loss request mode
overload_number = 5

;how many quest is lost before lost mode is close
;how many quest is lost before lost mode is close
loss_number = 20
@@ -61,6 +61,11 @@ final class ProxyFactory
*/
protected static $serviceConsumers = array();

/**
* @var *.consumer中配置的config信息
*/
protected static $configConsumer = Array();

private static $logger;

public static function setConsumerConfig($configData, $consumerConfigFile, $initSettings)
@@ -89,6 +94,11 @@ public static function setConsumerConfig($configData, $consumerConfigFile, $init
self::$appVersion = $configData['consumer_config']['version'];
}

if(isset($configData['consumer_config']))
{
self::$configConsumer = $configData['consumer_config'];
}

if(isset($configData['consumer_services']))
{
self::$serviceConsumers = $configData['consumer_services'];
@@ -99,7 +109,7 @@ public static function setConsumerConfig($configData, $consumerConfigFile, $init
private static function getInstancByRedis($service, $ioTimeOut, $version, $group)
{
$ret = NULL;
$providerInfo = ConsumerProxy::instance()->getProviders($service, $version, $group);
$providerInfo = ConsumerProxy::instance(self::$configConsumer)->getProviders($service, $version, $group);
if(!empty($providerInfo))
{
$cacheKey = $service.':'.$version.':'.$group;
@@ -1,5 +1,11 @@
[consumer_config]
p2p_mode = false
#tcp/sock
redis_connect_type = tcp
redis_hosts = 127.0.0.1:6379,127.0.0.1:6379
redis_connect_timeout = 1
redis_retry_count = 1


[consumer_services]

@@ -25,19 +25,21 @@ class ConsumerProxy
{
private static $_instance;
private $logger;
private $config = [];

public static function instance()
public static function instance($config)
{
if (!isset(ConsumerProxy::$_instance))
{
ConsumerProxy::$_instance = new ConsumerProxy();
ConsumerProxy::$_instance = new ConsumerProxy($config);
}
return ConsumerProxy::$_instance;
}

public function __construct()
public function __construct($config)
{
$this->logger = \Logger::getLogger(__CLASS__);
$this->config = $config;
}

/**
@@ -48,7 +50,7 @@ public function getProviders($service, $version=FSOFConstants::FSOF_SERVICE_VERS
try
{
//获取路由信息
$providerInfo = FSOFRedis::instance()->getProviderInfo($service);
$providerInfo = FSOFRedis::instance($this->config)->getProviderInfo($service);
return $this->filterProviderUrls($providerInfo, $version, $group, $service);
}
catch (\Exception $e)

0 comments on commit c726875

Please sign in to comment.