基于cookie的session sticky lnmt实现

 


 

 

架构图

wKioL1ZipZjxfgppAABGRleQs2g582.png

 

环境说明

  • 所有主机基于centos 6.5

  • 后端主机:ha111  IP: 192.168.61.139 安装tomcat

  • 后端主机:ha222  IP: 192.168.61.140 安装tomcat

  • 负载均衡调度主机:rs1  IP: 192.168.61.131 安装nginx

 

ha111主机配置

 1. jdk安装,可以用sunopenjdk,这里用sun

 

  • 下载地址,下载linuxx64 rpm安装包即可
    http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

 

  • 安装
    rpm -ivh jdk-8u65-linux-x64.rpm

 

  • 环境变量设置
    vim /etc/profile.d/java.sh
    export JAVA_HOME=/usr/java/latest
    export PATH=$JAVA_HOME/bin:$PATH

 

  • 生效
    source /etc/profile.d/java.sh

 

 2.tomcat 安装

 

  • 下载地址,这里用8.0的,apache-tomcat-8.0.28.tar.gz
    http://tomcat.apache.org/download-80.cgi

 

  • 安装
    tar xvzf apache-tomcat-8.0.28.tar.gz -C /usr/local/
    ln -sv /usr/local/apache-tomcat-8.0.28/ /usr/local/tomcat

 

  • 环境变量设置
    vim /etc/profile.d/tomcat.sh
    export CATALINA_HOME=/usr/local/tomcat
    export PATH=$CATALINA_HOME/bin:$PATH

 

  • 立即生效
    source /etc/profile.d/tomcat.sh

 

  • 添加系统服务
    vim /etc/init.d/tomcat
    #!/bin/sh
    # Tomcat init scriptfor Linux.
    #
    # chkconfig: 2345 96 14
    # description: TheApache Tomcat servlet/JSP container.
    # JAVA_OPTS='-Xms64m-Xmx128m'
    JAVA_HOME=/usr/java/latest  #注意这些路径是否正确
    CATALINA_HOME=/usr/local/tomcat  #注意这些路径是否正确
    exportJAVA_HOME CATALINA_HOME  #这句不加也可以,前面环境都设置好了就行

    case $1 in
    start)
      exec $CATALINA_HOME/bin/catalina.sh start ;;
    stop)
     exec $CATALINA_HOME/bin/catalina.sh stop;;
    restart)
      $CATALINA_HOME/bin/catalina.sh stop
      sleep 2
      exec $CATALINA_HOME/bin/catalina.sh start ;;
      *)
      echo "Usage: `basename $0`{start|stop|restart}"
      exit 1
      ;;
     esac

 

  • 修改默认监听的8080端口为80
    vim /usr/local/tomcat/conf/server.xml
    <Connector port="8080" protocol="HTTP/1.1" 改为
    <Connector port="80" protocol="HTTP/1.1"

    chmod +x /etc/init.d/tomcat
    chkconfig --add tomcat
    service tomcat start 启动服务

  • 建立测试站点
    cd /usr/local/tomcat/webapp
    mkdir -pv testapp/{WEB-INF,classes,META-INF,lib}

 

  • 建立首页
    vim testapp/index.jsp
    <%@ page language="java" %>
    <html>
     <head><title>TomcatA</title></head>
      <body>
        <h1><fontcolor="red">Tomcat.ha111</font></h1>
        <tableborder="1">
          <tr>
            <td>Session ID</td>
        <%session.setAttribute("dx.com","dx.com"); %>
            <td><%= session.getId()%></td>
          </tr>
          <tr>
            <td>Created on</td>
            <td><%=session.getCreationTime() %></td>
         </tr>
        </table>
      </body>
    </html>

  • 测试curl http://localhost:8080/testapp/index.jsp

 

 3. ha222主机配置和ha111一样

 

 

 4 .nginx负载均衡调度器rs1配置

 

  • 安装包
    下载最新稳定版nginx-1.8.0
    http://nginx.org/en/download.html

 

  • 安装依赖
    yum -y install pcre-devel openssl-devel
    tar xvzf nginx-1.8.0.tar -C /root/

 

  • sticky模块
    wget
    https://nginx-sticky-module.googlecode.com/files/nginx-sticky-module-1.1.tar.gz
    tar xvzf nginx-sticky-module-1.1.tar.gz -C /root/

 

  • 解压后编译安装

  • ./configure --prefix=/usr/local/nginx--conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock --with-http_ssl_module--with-http_stub_status_module --with-http_gzip_static_module--with-http_flv_module --with-http_mp4_module--http-client-body-temp-path=/var/tmp/nginx/client--http-proxy-temp-path=/var/tmp/nginx/proxy--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --add-module=../nginx-sticky-module-1.1

    make && make install
    注意:如果需要开启sticky session功能,需要加上上面红色这句,nginx-sticky-module-1.1为模块文件所在位置,这里nginx的源文件与sticky模块源文件在同一目录

    创建上面指定的目录,不然启动时会报错
    mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi}

 

  • 修改配置文件
    vim /etc/nginx/nginx.conf
    添加下面内容:
    upstream testapp {
            sticky;  #开启 session sticky
            server 192.168.61.139:80;
            server 192.168.61.140:80;
            }

    server {
            listen       80;
            server_name  192.168.61.131;
            location / {
                   proxy_pass http://testapp;
                index  index.jsp index.html index.htm;
            }

            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    }

 

至此全部配置完成,只要访问http://192.168.61.131/testapp/index.jsp即可实现基于cookiesession sticky lnmt

 

 

 

 


你可能感兴趣的:(tomcat,nginx,linux,LNMT)