Jslfl【软件开发技术笔记】

nginx+tomcat+redis实现tomcat集群实践

参考引用:

http://www.jianshu.com/p/47a94a3bff34
http://www.cnblogs.com/zhrxidian/p/5491285.html
http://blog.csdn.net/grhlove123/article/details/48047735

环境:
win10,jdk7,tomcat7,nginx-1.11,redis-64b.3.0
所有服务都是本地单机安装。

1.nginx+tomcat集群

本机安装tomcat-A,tomcat-B
由于是单机多tomcat,所以注意修改其中一个tomcat的server.xml中三处端口号(8015,9091,8019),多个tomcat不能重复,如果不是单机则忽略此端口修改。如:


两个tomcat 的webapps 目录下都建目录cluster,里面放一个index.jsp,输出sessionid。
注意tomcat-A,tomcat-B中h1内分别是Tomcat Server A和Tomcat Server B以区分请求的服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
    <h1>Tomcat Server A</h1>
    <h2>Session ID : <%=session.getId() %></h2>
    <h2>Session Created : <%=session.getCreationTime() %></h2>
    ServerName : <%=request.getServerName()%>  
    <br>  
    ServerPort : <%=request.getServerPort()%>  
    </body>
</html>

启动两个tomcat服务器,分别访问测试服务是否正常:
http://localhost:9090/cluster/index.jsp
http://localhost:9091/cluster/index.jsp
1
2

nginx下载安装
http://nginx.org/en/download.html 我这下载windos版,解压到安装目录,修改conf\nginx.conf中集群及负载配置。参考如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
events {
   #use   epoll;             #epoll是多路复用IO(I/OMultiplexing)中的一种方式,仅用于linux2.6以上内核,可大大提高nginx的性能
   worker_connections  1024;  #单个后台work processes 进程最大的并发连接数
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip on;
    gzip_disable "msie6";
   
    #fail_timeout:当该时间内服务器没响应,则认为服务器失效,默认10s;max_fails:允许连接失败次数,默认为1
    #所需等待时间 = proxy_connect_timeout + fail_timeout * max_fails,等待时间后nginx便会将请求转给另外server
    upstream tomcatCluster{
        server 192.168.7.235:9090 max_fails=1 fail_timeout=2s;
        server 192.168.7.235:9091 max_fails=1 fail_timeout=2s;
    }
   
    server {
        listen       8080;
        server_name  localhost 127.0.0.1;
    location / {
        root   html;
        index  index.html;
        proxy_pass http://tomcatCluster;
   
            proxy_set_header        Host $host:$server_port;
            # 真实IP
            proxy_set_header        X-Real-IP $remote_addr;
            # 代理者的真实ip
            proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for;
            # 解决getScheme,isSecure,sendRedirect
            proxy_set_header X-Forwarded-Scheme  $scheme;
            client_max_body_size    10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout   2s;  #与服务器连接的超时时间,默认60s
            #proxy_send_timeout      90;
            #proxy_read_timeout      90;
            proxy_buffer_size       4k;
            proxy_buffers           4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
        }
       
    }
}

启动nginx,cmd进入安装目录启动等操作
start nginx 启动
nginx -s stop 停止
nginx -s reload 重启,配置更新后

测试效果http://localhost:8080/cluster/index.jsp,多次刷新会看到分别请求到Tomcat Server A和B,且每次sessionid不一样。
3
4
目前即实现负载均衡,下面来做session共享。

2.session共享
下载redis https://github.com/ServiceStack/redis-windows
可下载redis管理客户端(Redis Desktop Manager):http://redisdesktop.com/download

redis.windows.conf中可以设置密码(requirepass),我这为了方便测试暂没有设置。

cmd进入redis目录下,启动服务redis-server.exe redis.windows.conf,也可以注册为服务。

去 https://github.com/izerui/tomcat-redis-session-manager/tree/master/jar 下载三个jar放入tomcat-A和B的lib下。
commons-pool2-2.2.jar
jedis-2.5.2.jar init
tomcat-redis-session-manager-2.0.0.jar

tomcat-A和B中conf\context.xml中加入配置

1
2
3
4
5
6
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
        host="localhost"
        port="6379"
        database="0"
        maxInactiveInterval="60" />

重启tomcat后测试效果:http://localhost:8080/cluster/index.jsp,多次刷新会看到分别请求到Tomcat Server A和B,且每次sessionid相同,至此完全nginx+tomcat+redis实现tomcat集群.
5
6

, ,

Comments are currently closed.