Mac下 tomcat+nginx试验负载均衡 简单模拟

来源:互联网 发布:js对cfg文件增删查改 编辑:程序博客网 时间:2024/06/06 00:21

1.下载两个不同版本的tomcat 

打开各自server.conf修改tomcat端口号,主要为了一台电脑下运行两个tomcat防止端口冲突

第一个tomcat:修改port由8080改为18080,第三个端口号不必与前两个一致

 <Connector port="18080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="18080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->

 <Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />

第二个tomcat: 修改port由8080改为28080,第三个防止与前一个tomcat端口冲突

<Connector port="28080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="28080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->


<Server port="28005" shutdown="SHUTDOWN">


2.各自启动tomcat,进入index.jsp页面

进入tomcat bin目录,命令  sh catalina.sh run 启动tomcat 

输入地址:http://localhost:18080/index.jsp

http://localhost:28080/index.jsp

3.在终端上通过homebrew安装nginx

brew 查看是否安装brew

brew search nginx  查询是否有nginx

brew install nginx  安装nginx 

此处会弹出 Updating Homebrew...耐心等待几分钟

安装完毕 输入nginx -v 查询版本号安装是否成功

地址栏输入 http://localhost:8080 安装成功会提示

Welcome to nginx!

4.找到nginx配置目录

/usr/local/etc/nginx

修改nginx.conf



#gzip  on;

    upstream zmy.com{
    server 127.0.0.1:18080 weight=1;
    server 127.0.0.1:28080 weight=2;
    }


  server{


listen       8080;
        server_name  localhost;


location / {
        proxy_pass http://zmy.com;
        proxy_redirect default;
        }

  }


保存修改,重启nginx,在 nginx根目录下输入命令:

sudo nginx -s reload

重启完毕



5.地址栏输入 

http://localhost:8080/index.jsp

刷新看看nginx访问到哪个版本的tomcat上


注:



启动nginx
sudo nginx


修改 nginx.conf 后,重载配置文件
sudo nginx -s reload


停止 nginx 服务器


sudo nginx -s stop

  
nginx.conf配置说明,以下转自:http://blog.csdn.net/wang379275614/article/details/47778201


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;        }    }}


原创粉丝点击