Nginx+tomcat+redis集群共享session实现负载均衡

1.nginx是一款轻量级兼备高性能的Http和反向代理服务器。所谓反向代理就是指用户发起访问请求,由代理服务器接受,然后将请求转发给正式服务器,并且将正式服务器处理完的数据返回给客户单,此时代理服务器就表现为一个服务器。

进入nginx的安装目录后,输入相关命令,一个窗口一闪而过,在任务管理器中可以找到它的进程。

浏览器输入localhost,可以看到nginx的欢迎页面!

修改nginx的配置文件,

       worker_processes:工作进程个数,

       worker_connections:单个进程最大连接数,

       server:每一个server相当于一个代理服务器,

       listern:监听端口,默认80,

       server_name:当前服务的域名,可以有多个,用空格分割,

       location:表示匹配的路径,配置/ 表示所有请求都被匹配到这里,

       index:当没有指定主页时,默认会选择这个指定的文件,可多个,空格分割 ,

        proxy_pass:请求转向自定义的服务器列表,

       upstream name{} :服务器集群名称    

  1. 然后再cmd中输入命令nginx -s reload重启nginx;
  2. 部署三个Tomcat,分别修改server.xml 和context.xml

server.xml依次修改 Server的port为8005,8006,8007 ,Connector的port为8081 8082 8083,8009 8010 8011

context.xml增加

 

 并在webappsROOT下增加session.jsp ,加上标记tomcat 1 tomcat 2 tomcat 3

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
    <br>session id=<%=session.getId()%>
    <br>tomcat 1
</body>
</html>

将如下几个jar包复制到${TOMCAT_HOME}/lib下

tomcat-redis-session-manager-VERSION.jar 
jedis-2.5.2.jar 
commons-pool2-2.2.jar

3.修改Ngnix的配置文件增加负载均衡配置
#user  nobody;
#nginx进程数,建议设置为等于CPU总核心数
worker_processes  4;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程pid文件
pid        logs/nginx.pid;


events {
    worker_connections  65535;
}


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;
	
	upstream mt.com {
		#upstream的负载均衡,weight是权重,可根据机器配置定义权重.weight参数表示权值,权值越高被分配的几率越大.
		server 127.0.0.1:8081 weight=1;
		server 127.0.0.1:8082 weight=2;
		server 127.0.0.1:8083 weight=3;
	}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            index  index.html index.htm;
			proxy_pass http://mt.com;
			proxy_set_header X-Real-IP $remote_addr;
			#允许客户端请求的最大单文件字节数
			client_max_body_size 100m;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 4.启动redis,tomcat1,tomcat2,tomcat3 访问localhost/session.jsp且不断刷新

可以看到每次刷新都可能访问不同的tomcat,实现了负载均衡,而且session都是一致的,保证了session的一致性

原文参照: https://www.cnblogs.com/zhrxidian/p/5432886.html

      https://www.cnblogs.com/linjiqin/p/5761281.html

      http://blog.csdn.net/lipc_/article/details/52766884

      https://www.cnblogs.com/machanghai/p/5956195.html

原文地址:https://www.cnblogs.com/Wanted-Tao/p/8183024.html