zoukankan      html  css  js  c++  java
  • Nginx+Redis 实现 jsp页面缓存,提升系统吞吐率

    最近在开发的时候,发现之前APP客户端的一部分页面用的是webview交互,这些页面请求很多,打开一套试卷,将会产生100+的请求量,导致系统性能下降。于是考虑在最靠近客户端的Nginx服务器上做Redis缓存。综合了下网上对于php缓存的资料,经过一番改动,终于搭建成功。由于网上的是针对php的,而且没有说明,对于我这种完全不动运维的人来说,研究下来还是挺痛苦的。所以整理一份比较完整的,供大家参考。

    以下的配置中,可能有不适合或者写的有问题的。请留言指出,谢谢!

    最终缓存以后,整个项目结构图如下(图片复制的,请自动脑补充memcache为redis,php为tomcat):

    参考文章地址:

    1.srcache_nginx+redis构建缓存系统 http://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

    2.httpsrcachemodule wiki  http://wiki.nginx.org/HttpSRCacheModule#srcache_response_cache_control

    1. 安装Nginx

    首先下载Nginx安装包,tar zvxf解压到/usr/local/src目录;

    下载模块ngx_devel_kit,  set-misk-nginx-module,  srcache-nginx-module,  echo-nginx-module,  ngx-http-redis,  redis2-nginx-module;

    将这些模块解压到/usr/local/src/modules/下面;

    进入/usr/local/src/nginx-1.8.0/目录,执行如下命令:

    ./configure --add-module=../modules/echo-nginx-module-0.57 --add-module=../modules/ngx_http_redis-0.3.7  --add-module=../modules/ngx_devel_kit-0.2.19 --add-module=../modules/set-misc-nginx-module-0.29  --add-module=../modules/srcache-nginx-module-master --add-module=../modules/redis2-nginx-module-master

    然后执行make;make install;

    默认安装到/usr/local/nginx/目录中,至此安装成功;

    2. 配置Nginx

    首先在Http体中声明upstream(这个命令没有研究,只能自己猜测了下),代码如下:

    upstream redis{
        server 127.0.0.1:6379;
        keepalive 512;
    }

     server 是Redis服务器的IP+PORT,keepalive是保持的连接数,这个连接数是网上的,对于我的项目来说应该是太大了。大家自行修改。

    配置Server中的location监听

    location /test/ {
        #这三个命令参考srcache 文档,http://wiki.nginx.org/HttpSRCacheModule
        srcache_store_private on;
        srcache_methods GET;
        srcache_response_cache_control off;
    
        #匹配自己的路径,由于Nginx不支持嵌套if,所以这么写
        if ($uri ~ /test/index.jsp$){
            set $flag "${flag}1";
        }
        if ($arg_id ~ [0-9]+$){
            set $flag "${flag}1";
        }
        if ($flag = "011"){
            #这里我用普通的请求参数来作为缓存的键值,网上的是用MD5,但是对于更新缓存又多了操作。大家根据业务自行调整。
            set $key $arg_id;
            set_escape_uri $escaped_key $key;
            #请求过来会先查询这个
            srcache_fetch GET /redis $key;
            #过期时间
            srcache_default_expire 60;
            srcache_store PUT /redis2 key=$escaped_key&exptime=$srcache_expire;
            #添加头信息
            add_header X-Cached-From $srcache_fetch_status;
            add_header X-Cached-Store $srcache_store_status;
            add_header X-Key $key;
            set_md5 $md5key $key;
            add_header X-md5-key $md5key;
            add_header X-Query_String $query_string;
            add_header X-expire $srcache_expire;
        }
        #网上都是用fast_cgi来代理,没弄会,就用最初的了,貌似fast_cgi是apache php下用的
        proxy_pass http://192.168.1.102:8080;
    }
    #redis模块
    location = /redis {
        internal;
        set $redis_key $args;
        redis_pass redis;
    }
    #redis2模块
    location = /redis2 {
        internal;
     
        set_unescape_uri $exptime $arg_exptime;
        set_unescape_uri $key $arg_key;
     
        redis2_query set $key $echo_request_body;
        redis2_query expire $key $exptime;
        redis2_pass redis;
    }

     到这里后,配置就完成了。

    3. 运行效果

    没有做缓存状态:

    有做缓存状态:

     

    响应头信息

    第一次请求:

    再次请求:

     

    运行效果明显的是X-cached-from这个头信息的变化。

    4. 未解决的问题

    不明白用了httpredis2为什么还要引入httpredis,查询文档后的结果是说redis2是httpredis升级版,但是wiki上说:

    Also, you need both HttpRedisModule and HttpRedis2Module. The former is used in the srcache_fetch subrequest and the latter is used in the srcache_store subrequest.

    也就是说两个都要。

    另外就是对于/redis这个location里,没有redis get这样的代码,是怎么获取到返回信息的。最初自己只安装httpredsi2,用redis2_pass redis不好使。然后加入redis2_query get $redis_key,依然不好使。无奈只好按照文档上的来了。

  • 相关阅读:
    27. Remove Element
    列表变成字典
    1. Two Sum
    CVPR2019:What and How Well You Performed? A Multitask Learning Approach to Action Quality Assessment
    959. Regions Cut By Slashes
    118. Pascal's Triangle
    loj3117 IOI2017 接线 wiring 题解
    题解 NOI2019 序列
    题解 省选联考2020 组合数问题
    题解 Educational Codeforces Round 90 (Rated for Div. 2) (CF1373)
  • 原文地址:https://www.cnblogs.com/barrywxx/p/8525716.html
Copyright © 2011-2022 走看看