1.nginx + tomcat集羣部署
1、下載Tomcat
1)兩個Tomcat修改不同的端口號
(1)端口號
tomcat1 :端口號8086
tomcat2:端口號8089
2、下載nginx並安裝
1)下載:https://nginx.org/en/download.html
2)安裝包直接解壓
3)啓動等相關命令
nginx端口默認80。啓動後查看是否存在nginx.exe的進程。若啓動失敗,可能是端口衝突,可再nginx.conf中修改端口號
cmd命令進入安裝文件;
1、啓動:
C:\server\nginx-1.0.2>start nginx
或
C:\server\nginx-1.0.2>nginx.exe
注:建議使用第一種,第二種會使你的cmd窗口一直處於執行中,不能進行其他命令操作。
2、停止:
C:\server\nginx-1.0.2>nginx.exe -s stop
或
C:\server\nginx-1.0.2>nginx.exe -s quit
注:stop是快速停止nginx,可能並不保存相關信息;quit是完整有序的停止nginx,並保存相關信息。
3、重新載入Nginx:
C:\server\nginx-1.0.2>nginx.exe -s reload
當配置信息修改,需要重新載入這些配置時使用此命令。
4、重新打開日誌文件:
C:\server\nginx-1.0.2>nginx.exe -s reopen
5、查看Nginx版本:
C:\server\nginx-1.0.2>nginx -v
3、配置nginx
在conf文件夾中找到nginx.conf配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 server_test{
server 127.0.0.1:8086;
server 127.0.0.1:8089;
ip_hash;
}
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://server_test;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_Forwarded_for;
proxy_redirect off;
proxy_connect_timeout 1;
proxy_read_timeout 30;
proxy_send_timeout 30;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
//proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
#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、tomcat下放測試項目
Tomcat(Tomcat1跟Tomcat2都加)下添加測試項目
tomcat/webapps/路徑下新建test文件夾,文件夾下添加index.jsp,代碼如下
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster Test</title></head>
<body>
SessionID:<%=session.getId()%><BR>SessionIP:<%=request.getServerName()%>
<BR>SessionPort:<%=request.getServerPort()%>
<BR>這裏是服務器1
<BR>tomcat端口是8086
</body>
</html>
5、啓動Tomcat1.Tomcat2.nginx
訪問nginx http://127.0.0.1:8088/test/index.jsp即可。
刷新頁面可以看到,輪流訪問該Tomcat。
2.session共享
方式1:IP綁定 ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決集羣部署環境下session共享的問題。
但是如果訪問的Tomcat掛掉,訪問該Tomcat的用戶就會無法使用系統
nginx.conf文件下添加 ip_hash
upstream server_test{
server 127.0.0.1:8086;
server 127.0.0.1:8089;
ip_hash;
}
方式2:Tomcat自帶的cluster(併發量大不適用)
server.xml文件中<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 節點下添加以下內容。
Tomcat5的話直接將註釋掉的節點放開就行,不要用下面的代碼。
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 這是原來的,現註釋掉-->
<!--開始-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4008"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
</Cluster>
需要在項目中建一個WEB-IN文件夾,裏面新建一個web.xml文件,打開web.xml,並在web-app節點下添加<distributable/>
節點。
<web-app>
<distributable/>
</web-app>
方式3:memcache
1)下載memcache(以及相關jar包下載)
memcache以及jar包以及配置:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
memcache下載:https://www.runoob.com/memcached/window-install-memcached.html
jar包下載:https://mvnrepository.com/
2)解壓安裝
3)啓動
1. 下載memcache的windows穩定版,解壓放某個盤下面,比如在c:\memcached
2. 在終端(也即cmd命令界面)下輸入‘c:\memcached\memcached.exe -d install’安裝
3. 再輸入:‘c:\memcached\memcached.exe -d start’啓動。NOTE: 以後memcached將作爲windows的一個服務每次開機時自動啓動。這樣服務器端已經安裝完畢了。
4)配置
Tomcat conf/context.xml文件內添加<Context>標籤下添加一下內容
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:127.0.0.1:11211"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
sessionBackupAsync="false"
sessionBackupTimeout="100"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
copyCollectionsForSerialization="false"
sticky="false"/>
3.redis(這個方式我測試出現了問題,沒有深入研究)
參考文檔https://www.cnblogs.com/notDog/p/5318686.html
1)下載
2)安裝
3)jar包
下載地址:https://github.com/jcoleman/tomcat-redis-session-manager/downloads
下載地址:https://mvnrepository.com/
4)配置
打開Context.xml,添加
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60" />
Tomcat集羣Cluster配置
nginx+tomcat集羣搭建
nginx參考鏈接: