Nginx實現長串連應用

來源:互聯網
上載者:User
無論大家做web後端還是app後端,還是SOA服務化,長串連都是一個不錯的選擇,一方面節省了每次都建立串連的資源消耗,另一方面,可以讓訊息及時的響應,提升了體驗。

這裡介紹一種通過Nginx module實現長串連的辦法,這種方式是http協議上的長串連,嚴格上講http協議本身就是請求應答式的,並沒有嚴格意義的長串連,所謂的長串連是指當沒有相應的時候,可以一直hold,一直到有相應為止,然後立刻再重建立立一次串連。

下面來講一下如何來實現的。

1、首先下載NGiNX_HTTP_Push_Module和Nginx,就這兩個tar檔案;


2、將這兩個tar檔案拷貝到linux系統上,在nginx目錄下執行:

  ./configure --add-module=path/to/nginx_http_push_module ... makemake install

3、中間有可能會出現找不到pcre模組等,如果你是Centos系統,使用yum -y install pcre-devel openssl openssl-devel來安裝
安裝後,繼續執行nginx的安裝

4、等nginx都安裝完畢後,配置長串連:

在/use/local/nginx/conf的nginx.conf檔案
添加:

  location /publish {     set $push_channel_id $arg_id;     push_publisher;     push_store_messages on;     push_message_timeout 2h;     push_max_message_buffer_length 10; } location /activity {     push_subscriber;     set $push_channel_id $arg_id;     push_subscriber_concurrency broadcast;     default_type text/plain; }

5、重啟ngnix,訪問http://你的IP:連接埠/activity?id=你的Channel ,如果瀏覽器一直等待,
然後,你寫一段代碼去發布一條訊息,如果瀏覽器能接受到,說明安裝成功!

    public void testNginx(){        String http = "http://172.16.4.108/publish?id=my";        PostMethod postMethod = new PostMethod(http);        RequestEntity requestEntity = new StringRequestEntity("444");        postMethod.setRequestEntity(requestEntity);        try{            int status =this.client.executeMethod(postMethod);            if (status == HttpStatus.SC_OK) {                String text = postMethod.getResponseBodyAsString();                System.out.println(text);            }        }catch (Exception e){            e.printStackTrace();        }    }

6、以上都安裝完畢後,就開始我們自己的邏輯了

下面是監聽端,這裡做了一個簡單的實現,我們需要在監聽端始終記錄一個lastModified,這個時間代表了他最後接受到的新訊息的時間

private static String etag = "";    private static String lastModified = "";    public static void main(String[] args){        while (true) {            HttpClient httpClient = new HttpClient();            String http = "http://172.16.4.108/activity?id=my";            GetMethod getMethod = new GetMethod(http);            getMethod.setRequestHeader("Connection","keep-alive");            getMethod.setRequestHeader("If-None-Match", etag);            getMethod.setRequestHeader("If-Modified-Since", lastModified);            try {                int status = httpClient.executeMethod(getMethod);                if(getMethod.getResponseHeader("Etag") != null) {                    etag = getMethod.getResponseHeader("Etag").getValue();                }                if(getMethod.getResponseHeader("Last-Modified") != null) {                    lastModified = getMethod.getResponseHeader("Last-Modified").getValue();                }                System.out.println("etag=" + etag + ";lastmodif=" + lastModified + ";status=" + status);                if (status == HttpStatus.SC_OK) {                    String text = getMethod.getResponseBodyAsString();                    System.out.println(text);                }            } catch (Exception e) {                e.printStackTrace();            }        }    }

下面就是發送訊息端:和我們測試時候使用的代碼一樣

        public void testNginx(){        String http = "http://172.16.4.108/publish?id=my";        PostMethod postMethod = new PostMethod(http);        RequestEntity requestEntity = new StringRequestEntity("444");        postMethod.setRequestEntity(requestEntity);        try{            int status =this.client.executeMethod(postMethod);            if (status == HttpStatus.SC_OK) {                String text = postMethod.getResponseBodyAsString();                System.out.println(text);            }        }catch (Exception e){            e.printStackTrace();        }    }

到這裡,我們的方案就完成了。

以上就介紹了Nginx實現長串連應用,包括了方面的內容,希望對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.