1、tomcat结合nginx做负载均衡,session绑定
nginx:192.168.223.136 tomcat:192.168.223.146:8081,192.168.223.146:8082这里使用tomcat的多实例做示例
upstream backserver {
server 192.168.223.146:8081 weight=1;
server 192.168.223.146:8082 weight=1;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://backserver/;
}
负载均衡已实现,现在进行session会话绑定:
upstream backserver {
ip_hash;
server 192.168.223.146:8081 weight=1;
server 192.168.223.146:8082 weight=1;
}
然后不管怎么访问,源ip相同的都会被派往后端的同一台tomcat实例
2、tomcat结合apache做负载均衡,session绑定(apache与tomcat基于http协议)
httpd2.4以上版本,编译安装192.168.223.136,tomcat还是多实例192.168.223.146:8081,192.168.223.146:8082
httpd配置:vhosts.conf
<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:
3、tomcat结合apache做负载均衡,session绑定(apache与tomcat基于ajp协议)
只需要将上面的配置文件修改为ajp协议就行,这里省略
4、tomcat结合apache做负载均衡,session绑定(apache与tomcat基于mod_jk模块)
1、编译安装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
2、配置相应的文件
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
3、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包成功解决该错误