无论大家做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
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; }
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(); } }
下面是监听端,这里做了一个简单的实现,我们需要在监听端始终记录一个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(); } }
原文地址:http://blog.csdn.net/shunlongjin/article/details/42458645