在centos7.3虚拟机环境下
nginx服务器
www 172.18.70.60
tomcat服务器
node1 172.18.70.40
node2 172.18.70.50
同步时间
dns解析
yum 源安装: 端口号(http协议端口:8080、管理接口:8005、AJP协议端口:8009)
[root@node1 ~]#yum -y install java-1.8.0-openjdk-devel tomcat tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp
手动提供一测试类应用,并冷部署:
[root@node1 ~]#mkdir -pv /usr/share/tomcat/webapps/myapp/{lib,classes,WEB-INF}
[root@node1 ~]#vim /usr/share/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
TomcatA
TomcatA.magedu.com
首部是红色的
Session ID
<% session.setAttribute("magedu.com","magedu.com"); %>
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
[root@node2 ~]#mkdir -pv /usr/share/tomcat/webapps/myapp/{lib,classes,WEB-INF}
[root@node2 ~]#vim /usr/share/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
TomcatA
TomcatB.magedu.com
首部是蓝色的
Session ID
<% session.setAttribute("magedu.com","magedu.com"); %>
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
配置调度器nginx
配置nginx
[root@www /etc/nginx]#vim nginx.conf
default_type application/octet-stream;
upstream tcsrvs { 定义服务器组
server 172.18.70.40:8080;
server 172.18.70.50:8080;
}
location / {
proxy_pass http://tcsrvs; 传递后端服务器地址
}
[root@www /etc/nginx]#nginx -t
[root@www /etc/nginx]#systemctl start nginx
由于网页有缓存,有时刷不出来最好使用curl 命令看到更清楚
[root@www /etc/nginx]#for i in {1..10}; do curl -s http://172.18.70.60/myapp/index.jsp |grep -i tomcat ;done
负载均衡效果没有问题了
nginx转换成http测试下效果
httpd + tomcat集群基于balancer实现
[root@www /etc/nginx]#yum install httpd
[root@www /etc/httpd/conf.d]#vim http-tomcat.conf 基于http模块反向代理的负载均衡配置
BalancerMember http://172.18.70.40:8080 # loadfactor=1 添加权重
BalancerMember http://172.18.70.50:8080 # loadfactor=2
ProxySet lbmethod=byrequests
ServerName www.centos.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
Require all granted
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
Require all granted
[root@www /etc/httpd/conf.d]#httpd -t
[root@www /etc/httpd/conf.d]#systemctl start httpd
[root@www /etc/httpd/conf.d]#ss -ntl
跟之前测试一样用curl测试相对准确点,页面访问有缓存不容易出效果
[root@www /etc/httpd/conf.d]#for i in {1..10}; do curl -s http://172.18.70.60/myapp/index.jsp |grep -i tomcat ;done
基于ajp模块实现会话粘性:
[root@www /etc/httpd/conf.d]#killall httpd
[root@www /etc/httpd/conf.d]#cp http-tomcat.conf ajp-tomcat.conf
[root@www /etc/httpd/conf.d]#mv http-tomcat.conf{,.bak}
[root@www /etc/httpd/conf.d]#vim ajp-tomcat.conf
BalancerMember ajp://172.18.70.40:8009 # status=D 标记某个服务不可用 在A主机出现问题的时候,访问信息会调度到B主机
BalancerMember ajp://172.18.70.50:8009
ProxySet lbmethod=byrequests
ServerName www.centos.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
Require all granted
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
Require all granted
[root@www /etc/httpd/conf.d]#systemctl restart httpd
[root@www /etc/httpd/conf.d]#for i in {1..10}; do curl -s http://172.18.70.60/myapp/index.jsp |grep -i tomcat ;done
启用管理接口:內建管理接口,专用于bakancer ,不要开放此端口给别人,http也可以使用,效果一样
[root@www /etc/httpd/conf.d]#vim ajp-tomcat.conf
BalancerMember ajp://172.18.70.40:8009
BalancerMember ajp://172.18.70.50:8009
ProxySet lbmethod=byrequests
ServerName www.centos.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
Require all granted
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
Require all granted
#管理接口设置
SetHandler balancer-manager
ProxyPass !
Require all granted
[root@www /etc/httpd/conf.d]#httpd -t
[root@www /etc/httpd/conf.d]#systemctl reload httpd
可以看到有两个后端主机状态
LoadBalancer Status for
被访问次数
修改调度算法
LoadBalancer Status for [balancer://tcsrvs] [p9f66a393_tcsrvs]
[root@node1 /etc/tomcat]#vim server.xml
#jvmRoute="ctA"
[root@node1 ~]#systemctl restart tomcat
[root@node2 ~]#vim /etc/tomcat/server.xml
#jvmRoute="ctB"
[root@node2 ~]#systemctl restart tomcat
基于Cookie会话源地址绑定
修改虚拟主机配置
[root@www /etc/httpd/conf.d]#vim ajp-tomcat.conf
添加响应报文首部,额外附加键值ROUTEID替换挑选出来的服务器 ,path=/是Cookie适用范围 什么时候修改对应值生效
env=如果调度的后端服务器改变了,routeid就会发生改变,否则就不变就不做重新调度,后端服务器宕机了需要重新调度,
后端服务器没有宕机ROUTEID值就不会变
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
BalancerMember ajp://172.18.70.40:8009 route=tcA #route=tcA映射标识符
BalancerMember ajp://172.18.70.50:8009 route=tcB
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID 定义基于什么绑定会话
ServerName www.centos.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
Require all granted
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
Require all granted
SetHandler balancer-manager
ProxyPass !
Require all granted
[root@www /etc/httpd/conf.d]#httpd -t
[root@www /etc/httpd/conf.d]#systemctl reload httpd
网页测试基于Cookie绑定成功 F12
delta-manager实现会话复制集群实现
配置启用集群,将下列配置放置于或中;
[root@node1 ~]#vim /etc/tomcat/server.xml
"
官网文档上面缺少" / "
官网文档上面缺少" / "
[root@node1 ~]#scp /etc/tomcat/server.xml node2:/etc/tomcat/
[root@node2 ~]#vim /etc/tomcat/server.xml
官网文档上面缺少" / "
官网文档上面缺少" / "
官方配置示例有语法问题,倒数2,3行行尾>前没有“/”符号,添加即可
http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
配置webapps:将/etc/tomcat/WEB-INF/web.xml(tomcat yum安装路径)
拷贝到当前使用的根目录下的WEB-INF下,
编辑在 元素;
以上内容两台RS-tomcat配置相同,区别在与Receiver className下的address的ip需写RS本机地址。
[root@node1 ~]#cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/myapp/WEB-INF/
[root@node1 ~]#vim /usr/share/tomcat/webapps/myapp/WEB-INF/web.xml
[root@node1 ~]#systemctl restart tomcat
[root@node2 ~]#cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/myapp/WEB-INF/
[root@node2~]#vim /usr/share/tomcat/webapps/myapp/WEB-INF/web.xml
[root@node2 ~]#systemctl restart tomcat、
重启tomcat服务
测试发现尽管负载均衡切换了主机,但是session不会改变
使用msm实现session服务器实现:
http+tomcat7+memcached集群
memcached
安装memcached
[root@node1 ~]#yum install memcached -y
[root@node1 ~]#systemctl start memcached.service
[root@node1 ~]#yum -y install libmemcached 专用克隆工具
[root@node1 ~]#lftp 172.18.0.1/pub 需要安装的jar
cd ok, cwd=/pub
lftp 172.18.0.1:/pub> cd Sources/7.x86_64/msm/
lftp 172.18.0.1:/pub/Sources/7.x86_64/msm> ls
-rw-r--r-- 1 0 0 452748 Aug 21 09:14 javolution-5.4.3.1.jar
-rw-r--r-- 1 0 0 147025 Aug 21 09:14 memcached-session-manager-1.8.3.jar
-rw-r--r-- 1 0 0 11284 Aug 21 09:14 memcached-session-manager-tc7-1.8.3.jar
-rw-r--r-- 1 0 0 10407 Aug 21 09:14 memcached-session-manager-tc8-1.8.3.jar
-rw-r--r-- 1 0 0 71051 Aug 21 09:14 msm-javolution-serializer-1.8.3.jar
-rw-r--r-- 1 0 0 459447 Aug 21 09:14 spymemcached-2.11.1.jar
lftp 172.18.0.1:/pub/Sources/7.x86_64/msm> mget *
[root@node1 ~]#cp *.jar /usr/share/java/tomcat/ 添加缓存会话管理器到Tomcat
[root@node1 ~]#scp *.jar node2:/usr/share/java/tomcat/
[root@node1 ~]#systemctl stop tomcat
[root@node1 ~]#vim /etc/tomcat/server.xml 删除原有的Cluster
编辑server.xml文件Host上下文中定义context
搭建Java 开源博客 Solo
下载安装包
将war包发布到tomcat服务器的webapps目录下 solo-2.4.0.war
[root@node1 ~]#cp solo-2.4.0.war /usr/share/tomcat/webapps/ 自动解压
[root@node1 ~]#scp /etc/tomcat/solo-2.4.0.war node2:/usr/share/tomcat/webapps/ 复制到node2 tomcat机器上面
[root@node1 ~]#vim /etc/tomcat/server.xml
修改路径文件名字 node2也做修改
[root@node1 ~]#vim /usr/share/tomcat/webapps/solo-2.4.0/WEB-INF/classes/latke.properties
#### Server ####
# Browser visit protocol
serverScheme=http
# Browser visit domain name
serverHost=172.18.70.40 修改成本主机ip node2也进行修改
# Browser visit port, 80 as usual, THIS IS NOT SERVER LISTEN PORT!
serverPort=8080
#### Cookie ####
cookieName=solo
cookieSecret=Beyond
#### Runtime Mode ####
#runtimeMode=DEVELOPMENT
runtimeMode=PRODUCTION
[root@node1 ~]#systemctl restart tomcat
测试输入 tomcat 主机ip,文件存放路径
测试www代理服务器
测试成功
OK