A tool
nginx-1.8.0
apache-tomcat-6.0.33
Second, the goal
High-performance load balancing Tomcat cluster:
Third, step
1, first download Nginx, to download the stable version:
2, then extract two Tomcat, named apache-tomcat-6.0.33-1 and apache-tomcat-6.0.33-2:
3, and then modify these two ports Tomcat startup, 18080 and 28080, respectively, following the first Tomcat to modify, for example, under the open server.xml Tomcat conf directory:
Modify totaling at three ports:
Of course, the second station Tomcat same, as shown below:
4, and then start two Tomcat, and access to, to see if normal:
5, and then modify the default Tomcat page two of the above (in the end in order to distinguish the following table that is accessible Tomcat, you can easily change it):
After completion of change, access, as shown below:
6, OK, now we can begin to configure Nginx to achieve load balancing, in fact, very simple, only needs to configure Nginx configuration file to:
Configuration is as follows (here only a simple configuration, the actual production environment can be more detailed sound configuration):
- worker_processes 1; # number of work processes, generally consistent with the number of cpu core computers
- events {
- worker_connections 1024; # maximum number of connections a single process (the maximum number of connections connecting * = number of processes)
- }
- http {
- include mime.types; # file extension and file type map
- default_type application / octet-stream; # default file type
- sendfile on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
- keepalive_timeout 65; #长连接超时时间,单位是秒
- gzip on;#启用Gizp压缩
- #服务器的集群
- upstream netitcast.com { #服务器集群名字
- server 127.0.0.1:18080 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
- server 127.0.0.1:28080 weight=2;
- }
- #当前的Nginx的配置
- server {
- listen 80;#监听80端口,可以改成其他端口
- server_name localhost;############## 当前服务的域名
- location / {
- proxy_pass http://netitcast.com;
- proxy_redirect default;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- }
核心配置如下:
到此配置完成,下面开始演示负载均衡。
7、首先,我们启动Nginx:
8、然后我们即可输入:localhost/index.jsp查看运行状况了
第一次访问,发现访问的是Tomcat2上的程序:
然后刷新,访问的还是Tomcat2上的程序:
再刷新,发现变为了Tomcat1上的程序:
再刷新,发现又变为了Tomcat2上的程序:
到此,我们利用Nginx已经实现了负载均衡的Tomcat集群。我们不断的刷新,发现访问Tomcat2的概率大概是Tomcat1的2倍,这是因为我们在Nginx中配置的两台Tomcat的权重起的作用,如下图:
四、 总结
谁能想到实现一个高性能的负载均衡集群会如此简单。Nginx的功能如此强大,配置却如此简单,我们还有什么理由拒绝它呢?这比我们动不动就十多万至几十万人民币的F5 BIG-IP、NetScaler等硬件负载均衡交换机廉价了不知多少。此外,大家别忘了Nginx不仅仅是一个反向代理服务器,它本身也可以托管网站,作为Web服务器,进行Http服务处理。