tomcat結合nginx或apache做負載均衡及session繫結

wadeson發表於2017-08-10

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包成功解決該錯誤

相關文章