예제 #1
0
파일: Kfk.php 프로젝트: kerisy/framework
 /**
  * 消费初始化
  * @param $groupName
  * @param string $configKey
  * @throws Exception
  */
 function getCustomerInstance($groupName, $configKey = "default")
 {
     $configObj = new Config("kfk");
     $config = $configObj->get($configKey);
     try {
         $conf = new \RdKafka\Conf();
         $conf->setRebalanceCb(function (\RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) {
             switch ($err) {
                 case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
                     //                        echo "Assign: ";
                     //                        var_dump($partitions);
                     $kafka->assign($partitions);
                     break;
                 case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
                     //                        echo "Revoke: ";
                     //                        var_dump($partitions);
                     $kafka->assign(NULL);
                     break;
                 default:
                     throw new \Exception($err);
             }
         });
         $conf->set('group.id', $groupName);
         $conf->set('metadata.broker.list', $config);
         $topicConf = new \RdKafka\TopicConf();
         $topicConf->set('auto.offset.reset', 'smallest');
         $conf->setDefaultTopicConf($topicConf);
         $rk = new \RdKafka\KafkaConsumer($conf);
         $this->type = 1;
         $this->rk = $rk;
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage());
     }
 }
예제 #2
0
 function __construct($configKey = "default")
 {
     $configObj = new Config("mq");
     $config = $configObj->get($configKey);
     try {
         $this->connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['password'], $config['vhost'], false, 'AMQPLAIN', null, 'en_US', self::CONNECT_TIMEOUT, self::READ_TIMEOUT);
     } catch (Exception $e) {
         throw new Exception($e->getMessage());
     }
     $this->channel = $this->connection->channel();
     //脚本退出前,关闭连接
     register_shutdown_function([$this, 'close']);
 }
예제 #3
0
파일: Handle.php 프로젝트: kerisy/framework
 /**
  * 发送监控数据
  *
  * @param $tag
  * @param $group
  * @param string $msg
  * @return bool
  */
 public function send($tag, $group, $msg = '', $diffTime = '')
 {
     $configObj = new Config("config");
     $config = $configObj->get("monitor");
     if (!$config) {
         return false;
     }
     $obj = new Client($config['server']['host'], $config['server']['port']);
     if (!$diffTime) {
         $diffTime = RunTime::runtime();
     }
     $str = [];
     $str['name'] = $config['name'];
     $str['group'] = $group;
     $str['tag'] = $tag;
     $str['diff_time'] = $diffTime;
     $str['time'] = time();
     $str['msg'] = $msg;
     $obj->send(json_encode($str));
 }
예제 #4
0
 public function view($template, $data = [])
 {
     $this->initView();
     if (!is_null($this->attach) && is_array($this->attach)) {
         $data += $this->attach;
     }
     if ($this->headers->get("Location")) {
         $this->headers->set('Content-Type', 'text/html');
         return $this;
     }
     $configObj = new Config("config");
     $templateConfig = $configObj->get("template_engine");
     $templateConfig = $templateConfig ? $templateConfig : "kerisy";
     $tplClassName = "\\Kerisy\\Template\\" . ucfirst($templateConfig);
     $obj = new $tplClassName();
     $obj->path($template, $this->view);
     $html = $obj->render($data);
     $this->data = $html;
     $this->headers->set('Content-Type', 'text/html');
     return $this;
 }