特此声明:文章参考自https://www.imooc.com/article/34866和https://blog.csdn.net/xlgen157387/article/details/49781487
实现高性能负载均衡的Tomcat集群:
1. 首先下载Nginx和Tomcat
Nginx下载地址:http://nginx.org/en/download.html
Tomcat下载地址:http://tomcat.apache.org/
2. 然后解压三个Tomcat,分别命名为apache-tomcat-8.5.33-server1、apache-tomcat-8.5.33-server2和apache-tomcat-8.5.33-server3,入下图所示:
3. 然后修改这三个Tomcat的端口,以其中一台为例,打开Tomcat的conf目录下的server.xml,共修改三 处,如下所示:
4. 然后启动这三个Tomcat,并访问,看是否正常
5.然后修改上面三个Tomcat的默认页面,为这三台Tomcat区分一下,如下图所示:
修改后,进行访问,如下如图所示:
6.以上完成后,就可以配置Nginx来实现负载均衡,只需要将Nginx的配置文件nginx.conf给配置好就可以,如下所示(只进行简单的配置,实际生产环境可以进行更为详细的完善配置):
# Nginx全局参数
#user nobody; 1. # 运行Nginx的用户的用户组
worker_processes 1; # Nginx启动你那个多个线程,最好跟计算机的CPU核数一致
# 全局的错误日志类型
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 进程号的保存文件
#pid logs/nginx.pid;
# Nginx等待事件参数
events { # 等待事件
worker_connections 1024; # 每个进程的最大连接数(总连接数 = worker_connections * worker_processes)
}
# Nginx的HTTP参数
http {
include mime.types; # 文件扩展名和文件类型映射表
default_type application/octet-stream; # 默认文件类型
# 日志的格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main; # 日志文件
sendfile on; # 高效文件传输模式,指定是否调用sendfile函数来传输文件,一般的应用都建议启用
#tcp_nopush on;
#keepalive_timeout 0; # 长连接超时时间,单位秒
keepalive_timeout 65;
#gzip on; # 是否启用压缩传输
# 服务器的集群
upstream netitcast.com {
server 127.0.0.1:18080 weight=1; #服务器配置,weight是权重的意思,权重越大,分配的概率越大
server 127.0.0.1:18081 weight=2;
server 127.0.0.1:18082 weight=3;
# 上面三行是集群的服务器列表,IIS,最终请求会被转发到这里执行
}
# 当前的Nginx的配置
server {
listen 80; # 端口
server_name localhost; # 主机名
# 如果请求为localhost:80,则交给名称为netitcast.com的Nginx集群来处理
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
#root html;
#index index.html index.htm;
#}
location / {
proxy_pass http://netitcast.com; # 要与上面的Nginx的名称保持一致
proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
到此配置完成,下面演示负载均衡。
7. 首先启动Nginx
8. 然后输入127.0.0.1或localhost