Tomcat+Nginx-集群实战

Tomcat-学习目录

1、Tomcat+Nginx集群架构概述

1.1、Tomcat集群能带来什么

1、提高服务的性能、并发能力、以及高可用性
2、提高项目架构的扩展能力

1.2、Tomcat集群实现原理

通过Nginx负载均衡进行请求转发

1.3、Tomcat集群架构图

2、tomcat+nginx部署

2.1、主机准备

截止目前,我们已经部署两台tomcat+zrlog服务分别是tomcat1、tomcat2
tomcat1 192.168.10.5
tomcat2 192.168.10.7

2.2、配置tomcat服务

2.2.1、安装tomcat+zrlog

2.2.2、tomcat1-配置

]# vi /usr/local/tomcat/conf/server.xml
...
    <Engine>
      <Host name="zrlog.cyc.com"  appBase="/opt/zrlog" unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="zrlog_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
...

2.2.3、tomcat2-配置

]# vi /usr/local/tomcat/conf/server.xml
...
    <Engine>
      <Host name="zrlog.cyc.com"  appBase="/opt/zrlog" unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="zrlog_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
...

2.3、配置Nginx服务

2.3.1、安装nginx

2.3.2、配置nginx

# proxy_params
cat >/etc/nginx/proxy_params <<'EOF'
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffering on;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
EOF

# zrlog.cyc.com.conf
cat >/etc/nginx/conf.d/zrlog.cyc.com.conf <<'EOF'
upstream zrlog {
  server 192.168.10.5:8080;
  server 192.168.10.7:8080;
}
server{
  listen 80;
  server_name zrlog.cyc.com;
  location / {
    proxy_pass http://zrlog;
    include proxy_params;
  }
}
EOF

2.3.3、重新加载nginx

systemctl restart nginx

2.3.4、配置hosts

192.168.10.4 zrlog.cyc.com

2.3.5、测试访问

2.4、zrlog负载均衡配置成功

刷新两次,tomcat1 \ tomcat2 访问日志分别打印各一条,说明已经配置成功

 

 

 

posted @ 2023-05-03 23:12  小粉优化大师  阅读(85)  评论(0编辑  收藏  举报