七层负载

Nginx+Tomcat

Tomcat配置

1.复制程序文件

wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.54/bin/apache-tomcat-8.5.54.tar.gz
yum -y install lszrz
rz jdk jdk-8u211-liunx-x64.tar.gz
tar xf apache-tomcat-8.5.54.tar.gz  -C /usr/local/tomcat
tar xf jdk-8u211-liunx-x64.tar.gz   -C /usr/local/java
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile.d/java.sh
cd /usr/local
cp -a tomcat/ ./tomcat8_1
cp -a tomcat/ ./tomcat8_2

2.修改端口

sed -i 's#8005#8011#;s#8080#8081#' tomcat8_1/conf/server.xml
sed -i 's#8005#8012#;s#8080#8082#' tomcat8_2/conf/server.xml

3.启动

/usr/local/tomcat8_1/bin/startup.sh
/usr/local/tomcat8_2/bin/startup.sh
ss -tnlp

4.区分

echo 8081 >>/usr/local/tomcat8_1/webapps/ROOT/index.jsp 
echo 8082 >>/usr/local/tomcat8_1/webapps/ROOT/index.jsp 

Nginx配置

1.加载原配置

egrep -v '#|^$' /usr/local/nginx/sbin/nginx/conf/nginx.conf.default  > /usr/local/nginx/sbin/nginx/conf/nginx.conf

2.配置

http{
  upstream web{
    server 10.0.0.17:8081;
    server 10.0.0.17:8082;
  server {
      location / {
            root   html;
            index  index.jsp index.htm;
            proxy_pass http://web;
                  }
        }
}

3.重载测试

/usr/local/nginx/sbin/nginx -s  reload
curl -s 10.0.0.5|tail -1

Haproxy

1.安装准备

yum -y install haproxy
cp -rf /etc/haproxy/haproxy.cfg{,.bak}
sed -i -r '/^[]*#/d;/^$/d' /etc/haproxy/haproxy.cfg

2.配置

vim /etc/haproxy/haproxy.cfg
global
...
defaults
...

----------------------------配置监控[可选]------------------------------
listen stats
    bind                    	*:1314
    stats                   	enable
    stats refresh 		30s
    stats                   	hide-version
    stats uri              	/haproxystats
    stats realm         	Haproxy\ stats
    stats auth           	hapa:123
    stats admin         if TRUE
----------------------------------------------------------------------

frontend web
    mode                   	http
    bind                    	    *:80
    default_backend    httpservers

backend httpservers
    balance roundrobin
    server http1 192.168.188.9:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
    server http2 192.168.188.10:80 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
 
/*实现四层负载
listen mysql
    bind *:3306
    mode tcp
    balance roundrobin
    server mysql1 192.168.122.40:3306 weight 1  check inter 1s rise 2 fall 2
    server mysql2 192.168.122.50:3306 weight 1  check inter 1s rise 2 fall 2    */

systemctl restart haproxy

发表评论