rabbitMQ 推送隊列

首先,代碼中, 需要推送消息 到隊列中, 因爲我也不知道啥原因 ,反正我們 推送消息是單獨一個項目  ,使用 CURL請求發送隊列的接口  例如 

我的數據需要發送到隊列
public function syncRefundStatus($childOrderNum)
{
$res = array();
$q_type = "cloud_refund_status";
$_data = "";
RedisLog::info('cloud_refund_status參數:'.$childOrderNum);
$list = Db::table('order_goods')->field('child_order_num')->where("child_order_num='$childOrderNum' and is_delete='2' ")->select();
foreach ($list as $k=>$v) {
    $_data = array("child_order_num"=>$v['child_order_num']);
    $_data = json_encode(array($_data));
    $res[$k][] = $this->sendQueue($q_type, $q_type, $_data);   // 發動到隊列
}

RedisLog::info('cloud_refund_status:'.json_encode($res));
return $res;
/**
 * 發送到隊列
 */
public function sendQueue($type, $qtype, $content)
{
    $url = Config('app.api_order_queue');

    $_data['type'] = $type;
    $_data['qtype'] = $qtype;
    $_data['data'] = $content;

    RedisLog::info('order_sync:'.$url."_".json_encode($_data));

    $res = $this->_sendPostRequest($url, $_data);
    $res = json_decode($res, true);
    return $res;
}
api_order_queue = http://rabbitmq.d.gbicom.com/send

他會根據不同的type 調用不同的發送隊列

public function send()
{

    $type = input('post.type');     // 隊列名稱
    $qtype = input('post.qtype');   // q_type
    $data = input('post.data');


    RedisLog::info('rabbitqueueLog:'.$type."_".$data);

    if (empty($type) || empty($data)) {
        return json(['code' => 302, 'msg' => '數據爲空']);
    }

    $data = json_decode($data, true);
    $enqueue = new enqueue();

    if ($type == 'biz') {   // 提交批量產品編碼到隊列查詢服務進度

        return $enqueue->write_biz($type, $data);

    }elseif ($type == 'submit_deliver_Cloud'){  // 訂單提交到交付

        return $enqueue->write_biz($type, $data);

    }elseif ($type == 'cloud_refund_status'){  // 訂單提交到交付  確認退款問題

        return $enqueue->write_biz($type, $data);

    }elseif ($type == 'order_biz') {    // 訂單提交到交付的確認函和委託書

        return $enqueue->write_biz_at($qtype, $data);

    }elseif ($type == 'DistributionLead' || $type == 'ReleaseLead' || $type == 'CreateLeadCommunication') {

        return $enqueue->write_ai($type, $data);

    } elseif ($type == 'CreateAICommunication') {

        return $enqueue->crm_ai($type, $data);

    } elseif ($type == 'coupon_crm') {

        return $enqueue->write_campaign($type, $data);   // 5.18 優惠券同步到crm   -- by zuiw 2019.04.10

    } elseif($type == 'order' ||$type == 'ProcessContractInvoice' || $type =='ProcessContractRefund') {

        return $enqueue->write_crm($type, $data);
    }
}

 

以type= 'submit_deliver_Cloud' 爲例 

public function write_biz($type, $data = array())
{
    $config = array_merge($this->base_config(), $this->write_biz_config());

    return $this->send_queue($config, $type, $data);
}
/**
 * 服務器配置.
 *
 * @return array
 */
private function base_config()
{
    return [
        'host' => config('rabbitmq.server.host'),
        'port' => config('rabbitmq.server.port'),
        'user' => config('rabbitmq.server.user'),
        'password' => config('rabbitmq.server.password'),
        'vhost' => config('rabbitmq.server.vhost'),
    ];
}
/**
 * 交付隊列配置.
 *
 * @return array
 */
private function write_biz_config()
{
    return [
        'exchange_name' => config('rabbitmq.write_biz.exchange_name'),    //上一篇文章有介紹字段
        'queue_name' => config('rabbitmq.write_biz.queue_name'),
        'route_key' => config('rabbitmq.write_biz.route_key'),
        'consumer_tag' => config('rabbitmq.write_biz.consumer_tag'),
    ];
}

 

/**
 * 消息寫入mq.
 *
 * @param $config
 * @param $type
 * @param array $data
 *
 * @return \think\response\Json
 *
 * @throws \Exception
 */
private function send_queue($config, $type, $data = array())
{
    $q_id = session_create_id();
    $msg = [
        'q_id' => $q_id,    //session_create_id(),
        'q_time' => date('Y-m-d H:i:s'),
        'q_data' => $data,
        'q_type' => $type,
    ];
    $jsonMsg = json_encode($msg);
    RedisLog::info('rabbitmq入隊:'.$jsonMsg);
    try {
        $this->handle = new rabbitmq();
        $this->handle->connect($config);
        $this->handle->publish($jsonMsg);
    } catch (\Exception $e) {
        throw new \Exception($e->getMessage(), 300);
    }

    return json(['code' => 200, 'msg' => 'success', 'q_id' => $q_id]);
}

 

發佈了32 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章