單臺主機nginx+tomcat+mencached部署測試


  單臺主機部署 亦可分佈式部署 改動配置ip即可

多個tomcat要一起協同工作有幾種辦法,可以考慮的方案有以下幾個:
1. 使用tomcat自帶的cluster方式,多個tomcat間自動實時複製session信息,配置起來很簡單。但這個方案的效率比較低,在大併發下表現並不好。
2. 利用nginx的基於訪問ip的hash路由策略,保證訪問的ip始終被路由到同一個tomcat上,這個配置更簡單。但如果應用是某一個局域網大量用戶同時登錄,這樣負載均衡就沒什麼作用了。
3. 利用memcached把多個tomcat的session集中管理,前端在利用nginx負載均衡和動靜態資源分離,在兼顧系統水平擴展的同時又能保證較高的性能。

nginx反向代理HTTP Server,用於實現資源緩存、web server負載均衡等功能
memcached服務進行 session 統一存儲管理的共享方案。
系統環境
Centons6.5 Tomcat 7.0.69 jdk1.8.0_91Nginx 1.8.1  pcre-8.38  Memcached 1.4.25                   Libevent 2.0.22-stable  
Centons使用最小安裝版,安裝完更新系統  
yum –y update
給普通用戶添加sudo命令
如下給用戶dnhc添加sudo命令權限
vi  /etc/sudoers
root    ALL=(ALL)     ALL
dnhc    ALL=(ALL)       ALL
安裝nginx
添加運行nginx用戶以及用戶組
sudo groupadd -r nginx
sudo useradd -s /sbin/nologin -g nginx -r nginx
查詢是否創建了用戶
id nginx
安裝nginx所需的依賴包 安裝路徑一個人習慣 我統一安裝在opt文件下 所有的安裝文件放在opt文件下
yum install -y gcc gcc-c++ libgcc
yum -y install openssl openssl-devel
tar -zvxf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/opt/prce
make && make install
cd ..
tar -zvxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/opt/nginx --user=nginx --group=nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-pcre=/opt/pcre-8.38
make && make install
防火牆添加80端口
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
保存重啓防火牆
service iptables restart
啓動 停止 重啓nginx
/opt/nginx/sbin/nginx
/opt/nginx/sbin/nginx -s stop
/opt/nginx/sbin/nginx -s reload
對nginx反向代理 配置
vi /etc/nginx/nginx.conf
#user  nobody;
user  nginx nginx;
worker_processes  6;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections 65533 ;
}
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_min_length 1024k;
      gzip_buffers  4 16k;
      gzip_comp_level  2;
      gzip_types text/plain text/JavaScript text/css application/xml application/x-javascript application/json;
      gzip_vary    on;
        upstream opc {
        #ip_hash;
                 server 192.168.0.151:8080 max_fails=3  fail_timeout=30s;
                 server 192.168.0.151:8081 max_fails=3  fail_timeout=30s;
                      }
                                    
    server {
        listen       80;
        server_name  192.168.0.151;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass  http://opc;
            proxy_redirect default;
            proxy_connect_timeout 10;
        }

        #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;
    #    }
    #}

}

安裝memcached
 安裝依賴包
tar -zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/opt/libevent
make && make install
cd ..
tar -zxvf memcached-1.4.25.tar.gz
cd memcached-1.4.25
./configure --prefix=/opt/memcached --with-libevent=/opt/libevent
make && make install
啓動memcache
 useradd   memcache   #添加memcache用戶 或者直接用root鏈接
 memcached -m 64 -d -u memcache -p 11211 -l 192.168.0.233 -c 5000 -P /var/run/memcached.pid
 /opt/memcached/bin/memcached -d -m 128 -u root -p 11211 -c 256 -P /var/run/memcached.pid
 安裝tomcat和jdk
 tar -zxvf apache-tomcat-7.0.69.tar.gz
 tar -zxvf jdk-8u91-linux-x64.tar.gz
 cp -ri /opt/apache-tomcat-7.0.69 /opt/tomcat/tomcat3
 cp -ri /opt/apache-tomcat-7.0.69 /opt/tomcat/tomcat4
 編輯全局變量文件
 vi /etc/profile
 在最後面添加
export JAVA_HOME=/opt/jdk1.8.0_91
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar  
保存後執行 java -version 可顯示jdk 的版本爲配置成功
因爲是同一臺主機多實例tomcat 所有兩個tomcat的端口不能衝突否則會啓動不來
編輯
vim /opt/tomcat/tomcat3/conf/server.xml  

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">

vim /opt/tomcat/tomcat4/conf/server.xml
 <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat4">

配置session共享文件
vim /opt/tomcat/tomcat3/conf/context.xml
vim /opt/tomcat/tomcat4/conf/context.xml
在context下直接添加
  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:192.168.0.151:11211"
        sticky="false"
        lockingMode="auto"
        sessionBackupAsync="false"   
        sessionBackupTimeout="1000"  
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
        />
把所需的jar文件放到tomcat的lib文件夾下 所有文件都已打包測試
啓動tomcat
cd /opt/tomcat/tomcat3/bin/   ./startup.sh
cd /opt/tomcat/tomcat4/bin/   ./startup.sh

測試頁面
vi /opt/tomcat/tomcat3/webapps/ROOT/test.jsp
vi /opt/tomcat/tomcat4/webapps/ROOT/test.jsp
分別加入下面內容保存
Server Info:   
SessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>  
<br>
SessionPort:<%=request.getServerPort()%>
<br>
<%
  out.println("111");
%>
訪問兩個tomcat
192.168.0.151:8080/test.jsp
192.168.0.151:8081/test.jsp

 
 測試成功
 

感謝各位大大的相關文章

個人收集相關下載包 鏈接: http://pan.baidu.com/s/1bp5HrKb 密碼: yfmn

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章