一、tomcat結合nginx作負載均衡,session綁定html
nginx:192.168.223.136 tomcat:192.168.223.146:8081,192.168.223.146:8082這裏使用tomcat的多實例作示例node
upstream backserver {
server 192.168.223.146:8081 weight=1;
server 192.168.223.146:8082 weight=1;
}nginx
location / {
root html;
index index.html index.htm;
proxy_pass http://backserver/;
}apache
負載均衡已實現,如今進行session會話綁定:後端
upstream backserver {
ip_hash;
server 192.168.223.146:8081 weight=1;
server 192.168.223.146:8082 weight=1;
}centos
而後無論怎麼訪問,源ip相同的都會被派日後端的同一臺tomcat實例tomcat
二、tomcat結合apache作負載均衡,session綁定(apache與tomcat基於http協議)session
httpd2.4以上版本,編譯安裝192.168.223.136,tomcat仍是多實例192.168.223.146:8081,192.168.223.146:8082負載均衡
httpd配置:vhosts.confjvm
<proxy balancer://lbcluster>
BalancerMember http://192.168.223.146:8081 loadfactor=1 route=tomcat1 這裏route對應後端tomcat的配置文件的engine設置:<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
BalancerMember http://192.168.223.146:8082 loadfactor=1 route=tomcat2
</proxy>
<VirtualHost *:80>
ServerName 192.168.223.136
proxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://lbcluster/
ProxyPassReverse / balancer://lbcluster/
<Location />
Require all granted
</Location>
</VirtualHost>
配置以後一直不見出現80端口,查看error日誌:
[root@node1 ~]# tail -f /usr/local/apache2.4/logs/error_log
[Wed Aug 09 13:41:53.523457 2017] [mpm_prefork:notice] [pid 85758] AH00169: caught SIGTERM, shutting down
[Thu Aug 10 09:54:14.263767 2017] [proxy_balancer:emerg] [pid 88118] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Thu Aug 10 09:54:14.263926 2017] [:emerg] [pid 88118] AH00020: Configuration Failed, exiting
[Thu Aug 10 09:54:26.284340 2017] [proxy_balancer:emerg] [pid 88123] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Thu Aug 10 09:54:26.284427 2017] [:emerg] [pid 88123] AH00020: Configuration Failed, exiting
[Thu Aug 10 09:55:07.894942 2017] [proxy_balancer:emerg] [pid 88135] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Thu Aug 10 09:55:07.895056 2017] [:emerg] [pid 88135] AH00020: Configuration Failed, exiting
[Thu Aug 10 09:57:00.483255 2017] [proxy:crit] [pid 88148] AH02432: Cannot find LB Method: byrequests
[Thu Aug 10 09:57:00.483388 2017] [proxy_balancer:emerg] [pid 88148] (22)Invalid argument: AH01183: Cannot share balancer
[Thu Aug 10 09:57:00.483420 2017] [:emerg] [pid 88148] AH00020: Configuration Failed, exiting
根據錯誤提示,在配置文件中開啓相應的模塊:
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so
解決完後,開啓服務後訪問網站:
負載均衡已成功實現,如今將session會話進行綁定:
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster>
BalancerMember http://192.168.223.146:8081 loadfactor=1 route=tomcat1
BalancerMember http://192.168.223.146:8082 loadfactor=1 route=tomcat2
ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>
ServerName 192.168.223.136
proxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://lbcluster/
ProxyPassReverse / balancer://lbcluster/
<Location />
Require all granted
</Location>
</VirtualHost>
proxyVia On:
三、tomcat結合apache作負載均衡,session綁定(apache與tomcat基於ajp協議)
只須要將上面的配置文件修改成ajp協議就行,這裏省略
四、tomcat結合apache作負載均衡,session綁定(apache與tomcat基於mod_jk模塊)
一、編譯安裝mod_jk模塊
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
cd tomcat-connectors-1.2.41-src/native
./configure --with-apxs=/usr/local/apache2.4/bin/apxs
make && make install
二、配置相應的文件
cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/extra/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* tomcat1
JkMount /status/ stat1
三、workers.properties
cat workers.properties
worker.list=tomcat1,stat1
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.1.155
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.stat1.type = status
可是檢查語法時一直報錯:
[root@wadeson conf]# /usr/local/apache2.4/bin/httpd -t
AH00526: Syntax error on line 1 of /usr/local/apache2.4/conf/extra/workers.properties:
Invalid command 'worker.list=tomcat1,stat1', perhaps misspelled or defined by a module not included in the server configuration
httpd2.4編譯加載mod_jk模塊設置配置文件一直報錯,沒有找到相應的解決辦法
而後換成了centos7的yum安裝的httpd2.4以上版本,192.168.223.147
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
cd tomcat-connectors-1.2.41-src/native
./configure --with-apxs=/usr/bin/apxs
make && make install
[root@wadeson conf.d]# cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* tomcat1 這裏的tomcat1要和後端tomcat的配置jvmRoute值相同<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
JkMount /status/ stat1
[root@wadeson conf.d]# cat workers.properties
worker.list=tomcat1,stat1
worker.tomcat1.port=8010
worker.tomcat1.host=192.168.223.146
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.stat1.type = status
配置都是如出一轍,centos7自帶的httpd就沒有報錯,編譯安裝就一直報錯,因而採用centos7進行操做
基於模塊mod_jk的反向代理已經成功,因而進行負載均衡:
[root@wadeson conf.d]# cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkMount /* lbcluster1
JkMount /status/ stat1
[root@wadeson conf.d]# cat workers.properties
worker.list=lbcluster1,stat1
worker.tomcat1.port=8010
worker.tomcat1.host=192.168.223.146
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8011
worker.tomcat2.host=192.168.223.146
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.lbcluster1.type=lb
worker.lbcluster1.sticky_session=0
worker.lbcluster1.balance_workers = tomcat1,tomcat2
worker.stat1.type = status
訪問網站效果:
如今將負載均衡效果進行session會話綁定:
[root@wadeson conf.d]# cat workers.properties
worker.list=lbcluster1,stat1
worker.tomcat1.port=8010
worker.tomcat1.host=192.168.223.146
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8011
worker.tomcat2.host=192.168.223.146
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.lbcluster1.type=lb
worker.lbcluster1.sticky_session=1 只需將這裏的值將0變爲1便可綁定session會話
worker.lbcluster1.balance_workers = tomcat1,tomcat2
worker.stat1.type = status
綜上,httpd在編譯安裝2.4結合mod_jk模塊遇到不可知的錯誤,更換centos7自帶的2.4以上版本的yum包成功解決該錯誤