// 发布/订阅参数 $pipe_name = 'pipe1'; $pipelet_id = 2; // 加1在哪里做好呢? $start_point = -1; // 取得可发布broker $broker = $adapter->get_pub_broker($pipe_name, $pipelet_id); if (false === $broker) { echo "[Failure][get pub info]\n"; } else { echo "[Success][get pub info]\n"; // 向broker发起连接请求 $pub_dest = array("socket_address" => $broker['ip'], "socket_port" => $broker['port'], "socket_timeout" => 300); $stomp->set_destination($pub_dest); $stomp->role = BStompRoleType::PUBLISHER; $stomp->topic_name = $broker['stripe']; $stomp->session_id = BigpipeUtilities::get_pipelet_name($pipe_name, $pipelet_id) . '_' . BigpipeUtilities::get_uid(); if ($stomp->connect()) { echo '[Success][connected on broker][ip:' . $broker['ip'] . '][port:' . $broker['port'] . ']\\n'; echo '[session message id][' . $stomp->session_message_id . ']\\n'; } echo "\n"; $ofs = fopen('pub.json', 'w+'); $oval = json_encode($broker); fwrite($ofs, $oval); fclose($ofs); } // 取得可订阅broker_group // todo 用户操作 $stomp->close(); $adapter->uninit();
/** * 更新meta信息, 记录重试次数 * @return boolean */ private function _failover() { // failover时, 订阅、发布状态无效,重置状态 if (true == $this->_is_subscribed) { $this->_unsubscribe(); // 先尝试取消订阅, 但是不用考虑错误 (因为failover中有错误是常态) $this->_is_subscribed = false; } if ($this->_fo_count > $this->_max_fo_cnt) { // 重置failover BigpipeLog::fatal("[%s:%u][%s][can not do more]", __FILE__, __LINE__, __FUNCTION__); $this->_fo_sleep_time = 0; $this->_fo_count = 0; return false; } if (0 == $this->_fo_sleep_time) { // 第一次flush subscribe时,我们往往不希望等待, // 因此这时跳过sleep过程 // php中只有微秒级的usleep和秒级的sleep $this->_fo_sleep_time = BigpipeCommonDefine::INIT_FO_SLEEP_TIME * 1000; } else { usleep($this->_fo_sleep_time); } $this->_fo_count++; $this->_fo_sleep_time *= 2; // increase failover sleep time if ($this->_fo_sleep_time > BigpipeCommonDefine::MAX_FO_SLEEP_TIME) { // failover sleep time不能无限制增长 $this->_fo_sleep_time = BigpipeCommonDefine::MAX_FO_SLEEP_TIME; } // 通过meta跟新stripe if (false === $this->_update_meta()) { BigpipeLog::fatal("[%s:%u][%s][can not update meta from meta agent]", __FILE__, __LINE__, __FUNCTION__); return false; } // 随机选择并连接一个broker $is_ok = false; do { $broker = $this->_random_select_broker(); if (false === $broker) { // 无新broker可选, failover失败 BigpipeLog::fatal("[%s:%u][%s][no broker to subcribe]", __FILE__, __LINE__, __FUNCTION__); break; } // try to connect to broker $sub_dest = array("socket_address" => $broker->ip, "socket_port" => $broker->port, "socket_timeout" => $this->_conn_timeo); $this->_stomp_adapter->set_destination($sub_dest); $this->_stomp_adapter->role = BStompRoleType::SUBSCRIBER; $this->_stomp_adapter->topic_name = $this->_stripe['stripe_name']; $this->_stomp_adapter->session_id = BigpipeUtilities::get_pipelet_name($this->_pipe_name, $this->_pipelet_id) . '_' . BigpipeUtilities::get_uid(); if ($this->_stomp_adapter->connect()) { BigpipeLog::debug("[%s:%u][%s][Success][connected on broker][ip:%s][port:%u]", __FILE__, __LINE__, __FUNCTION__, $broker->ip, $broker->port); BigpipeLog::debug('[%s:%u][%s][session message id][smid:%s]', __FILE__, __LINE__, __FUNCTION__, $this->_stomp_adapter->session_message_id); $is_ok = true; break; // 跳出连接 } } while (true); return $is_ok; }