Nginx + SpringBoot set up load balancing

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: http: //nginx.org/en/download.html

  

  2, then open two servers I use IDEA tool, or use even a tomcat configuration but very troublesome, open two different port number, write a page in a simple

 

  

  3, and then modify these two Tomcat startup port, respectively, 8011 and 8022, the following first to modify the Tomcat, for example, under the open server.xml Tomcat conf directory:

  

 

  4, and then start two IDEA, and access to, to see if normal :( It is recommended to use postmain development tools, use the browser because of the cache, the effect may not be evident, I climb out of the pit)

  

  

 

  5, and then modify the above two different ports of the default page (in order to distinguish the following table that is accessible in the end, as can easily change it):

  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):

复制代码

html] view plaincopy

 worker_processes  1;#工作进程的个数,一般与计算机的cpu核数一致  
  events {  
    worker_connections  1024;#单个进程最大连接数(最大连接数=连接数*进程数) 
 }  
 http {  

    include       mime.types; #文件扩展名与文件类型映射表  
     default_type  application/octet-stream;#默认文件类型  
   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查看运行状况了

  第一次访问,发现访问的是8011上的程序:

  然后刷新,访问的还是8011上的程序:

  再刷新,发现变为了8022上的程序:

  再刷新,发现又变为了8011上的程序:

  

  到此,我们利用Nginx已经实现了负载均衡的Tomcat集群。我们不断的刷新,发现访问Tomcat2的概率大概是Tomcat1的2倍,这是因为我们在Nginx中配置的两台Tomcat的权重起的作用,如下图:

  

 

 

     9 .下面说一下如何启动nginx

      一、首先window+R 进入cmd。之后cd 进入到nginx所在目录发现界面一闪即逝证明已经启动了    

       

   二、浏览器访问localhost:nginx 端口号出现欢迎界面证明已经成功了

 

        下面是一些常用语句

查看Nginx的版本号:nginx -V

启动Nginx:start nginx

快速停止或关闭Nginx:nginx -s stop

正常停止或关闭Nginx:nginx -s quit

配置文件修改重装载命令:nginx -s reload

Guess you like

Origin blog.csdn.net/caidingnu/article/details/90274607