当前位置:Gxlcms > PHP教程 > Nginx实现长连接应用

Nginx实现长连接应用

时间:2021-07-01 10:21:17 帮助过:4人阅读

无论大家做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 ... 
	make
	make 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教程有兴趣的朋友有所帮助。

人气教程排行