centos下的activemq的配置及PHP的使用

來源:互聯網
上載者:User

標籤:llb   network   art   service   cape   tools.jar   2.4   file   取資料   

由於公司打算將php-resque的訊息佇列切換到activemq來,瞭解一下centos+php+activemq+stomp搭建訊息佇列的。

 

一、安裝JDK

    下載JDK(官網:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)

    1.1 選擇:jdk-8u131-linux-x64.tar.gz 下載到 /data/service

    1.2 解壓:tar -zxvf jdk-8u131-linux-x64.tar.gz

    1.3 移到:mkdir -p /usr/local/java     cp -R jdk1.8.0_131/ /usr/local/java/ 

      1.4 添加JAVA環境變數  vi /etc/profile

JAVA_HOME=/usr/local/java/jdk1.8.0_131JRE_HOME=/usr/local/java/jdk1.8.0_131/jrePATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATHCLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/libexport JAVA_HOME JRE_HOME PATH CLASSPATH

    1.5 重新載入 profile

      source /etc/profile

 

二、安裝activemq

   2.1:下載activemq  

  cd /data/service       wget http://mirrors.hust.edu.cn/apache//activemq/5.14.5/apache-activemq-5.14.5-bin.tar.gz

   2.2:解壓 

  tar -zxvf apache-activemq-5.14.5-bin.tar.gz

  2.3 配置stomp

  vim /data/software/apache-activemq-5.14.5/conf/activemq.xml   在<transportConnectors>中添加如下:  <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>

  2.4 啟動 activemq

  /data/software/apache-activemq-5.14.5/bin/activemq start

  2.5:監控activemq

http://activemqserverip:8161/admin/   (預設的帳號:admin 預設密碼:admin)

  2.6:修改監控帳號

vi /data/software/apache-activemq-5.14.5/conf/jetty-realm.properties

注意的是 使用者名稱和密碼的格式是

使用者名稱 : 密碼 ,角色名稱

 

三、PHP使用activemq測試訊息佇列(發送資料:sender,擷取資料:receiver)

  3.1 sender(生產端)

class MqController extends Controller{    public static $activemqConf = array(        ‘url‘ => ‘tcp://activemqServerIp:61613‘,        ‘id‘ => ‘admin‘,        ‘pswd‘ => ‘admin‘,        ‘queue‘ => ‘sms‘,        ‘enable‘ => TRUE    );    public function connectActiveMq()    {        $link = stomp_connect(self::$activemqConf[‘url‘], self::$activemqConf[‘id‘], self::$activemqConf[‘pswd‘]);        if (!$link) {            die("Can‘t connect MQ !!");        } else {            return $link;        }    }    public function actionSender()    {        $link = $this->connectActiveMq();        $item = ‘{ "mobile":"13900000000", "content":"您好!您的驗證碼為112233", "callback":"demo.xiaohuideng.com/sms/callback" }‘;        //使用 persistent message        $result = stomp_send($link, self::$activemqConf[‘queue‘], $item, array("persistent" => "true"));        if (FALSE === $result) {            echo ‘推入隊列失敗‘;        } else {            echo ‘job_id:‘ . $result;        }    }}

  

  3.2 receiver(接收端)

class MqController extends Controller{    public static $activemqConf = array(        ‘url‘ => ‘tcp://activemqServerIp:61613‘,        ‘id‘ => ‘admin‘,        ‘pswd‘ => ‘admin‘,        ‘queue‘ => ‘sms‘,        ‘enable‘ => TRUE    );    public function connectActiveMq()    {        $link = stomp_connect(self::$activemqConf[‘url‘], self::$activemqConf[‘id‘], self::$activemqConf[‘pswd‘]);        if (!$link) {            die("Can‘t connect MQ !!");        } else {            return $link;        }    }    public function actionReceiver()    {        $this->connectActiveMq();        $stomp = new Stomp(self::$activemqConf[‘url‘], self::$activemqConf[‘id‘], self::$activemqConf[‘pswd‘]);        $stomp->subscribe(‘*‘);        while ($stomp->hasFrame()) {            $frame = $stomp->readFrame();            if ($frame != NULL) {                // 收到的資料為 $frame->body                $res = json_decode($frame->body, true);                $res = json_encode($res, JSON_UNESCAPED_UNICODE);                echo ‘<pre>‘;                print_r($res);                echo ‘</pre>‘;                $stomp->ack($frame);            }        }    }}

 

centos下的activemq的配置及PHP的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.