コード例 #1
0
ファイル: MqClient.class.php プロジェクト: WickyLeo/bobcat
 public function publish_message($topic, $message, $partition = 0)
 {
     $stime = microtime(true);
     $rtn = false;
     $offset = false;
     if (!is_string($message)) {
         $message = json_encode($message);
     }
     $p = $this->getProducer();
     $p->setRequireAck($this->producerRequireAck);
     $p->setMessages($topic, $partition, $message);
     try {
         $result = $p->send();
     } catch (\Exception $e) {
         $catched_exception = $e;
         \Libs\Log\LevelLogWriter::warning("send to kafka failed,msg:" . $e->getMessage(), $e->getCode());
         $rtn = false;
     }
     if ($result && isset($result[$topic][$partition]['errCode']) && $result[$topic][$partition]['errCode'] == 0) {
         $rtn = $offset = $result[$topic][$partition]['offset'];
     } else {
         $rtn = false;
     }
     $logData = array();
     $logData['zk'] = $this->zkHosts;
     $logData['topic'] = $topic;
     $logData['message'] = $message;
     $logData['partition'] = $partition;
     $logData['offset'] = $offset;
     $logData['exception'] = isset($catched_exception) ? json_encode($catched_exception) : '';
     $logData['timecost'] = number_format((microtime(true) - $stime) * 1000, 2);
     \Libs\Log\LevelLogWriter::selfLog("mqclient", "publish_message", $logData);
     return $rtn;
 }
コード例 #2
0
ファイル: Redis.class.php プロジェクト: WickyLeo/bobcat
 /**
  * Get an initialized Redis connection according to the key.
  */
 protected static function getRedis($key = NULL, $reconnect = FALSE)
 {
     $class = get_called_class();
     $config = $class::getConfig();
     $count = count($config->servers);
     $server_id = is_null($key) ? 0 : hexdec(substr(md5($key), 0, 2)) % $count;
     $host = $config->servers[$server_id]['host'];
     $port = $config->servers[$server_id]['port'];
     $connect_index = $host . ":" . $port;
     $isConnect = TRUE;
     if (!isset(self::$connections[$connect_index])) {
         self::$connections[$connect_index] = self::connect($host, $port, $class::$timeout);
         $isConnect = self::$connections[$connect_index]->isConnected();
         if (!$isConnect && !$reconnect) {
             unset(self::$connections[$connect_index]);
             //reconnect
             return self::getRedis($key, TRUE);
         }
     }
     if (!$isConnect) {
         \Libs\Log\LevelLogWriter::warning("redis server connect error:{$host}:{$port}:{$key}");
     }
     return self::$connections[$connect_index];
 }