Ngnix tomcat 服务器集群配置 负载均衡

能够使用Nginx搭建Tomcat集群,并完成负载均衡

本文是基于mac系统设置;其他系统的基本类似

完成Nginx负载均衡,那么需要先来介绍Tomcat的安装和配置,我们首先要来配置Tomcat完成集群的配置.因为我们没有多台服务器运行Tomcat.那么我们可以模拟在一台服务器上运行多个Tomcat程序.

下载地址http://tomcat.apache.org/download-70.cgi

mac 环境下直接brew  install  Nginx  安装即可

下载tomcat 拷贝两份;tomcat-8081   和 tomcat-8088 两个;分别修改conf/server.xml文件;修改内容是


<Server port="8025" shutdown="SHUTDOWN">//tomcat服务的地址的port

<Connector port="8088" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />//http 协议访问的地址的port

    <Connector port="8029" protocol="AJP/1.3" redirectPort="8443" />//修改AJP访问地址的port


    <Engine name="Catalina" defaultHost="localhost" jvmRoute=“tomcat-8088”>//引擎修改jvmRoute

</Server>

tomcat  修改完事


下面修改nginx  

//usr/local/etc/nginx

修改nginx.conf 文件

 



#user  nobody;
worker_processes  1;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;




events {
    worker_connections  1024;
}




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;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;
    upstream server_lb{
    server  127.0.0.1:8088 weight=1;
    server  127.0.0.1:8081 weight=5;
    }


//weight  是服务器的使用权重
    server {
        listen       8080;//nginx 默认的监听端口
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            proxy_pass  http://server_lb;//反向代理服务器别名;与上述的一致
            index  index.html index.htm;
        }


        #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;
    #    }
    #}
    include servers/*;
}

好了先启动两个tomcat  然后

sudo nginx  启动nginx
sudo php-fpm

修改 nginx.conf 后,重载配置文件

sudo nginx -s reload

停止 nginx 服务器

sudo nginx -s stop





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现nginx和tomcat一起部署springboot项目并实现tomcat集群,需要进行以下步骤: 1. 部署springboot项目到tomcat上,并启动多个tomcat实例,这些实例需要在不同的端口上运行。 2. 配置nginx作为反向代理服务器,将所有请求转发到tomcat集群中的一个实例上。可以使用upstream模块来配置tomcat集群,如下所示: ```nginx http { upstream tomcat_cluster { server tomcat1_ip:tomcat1_port; server tomcat2_ip:tomcat2_port; server tomcat3_ip:tomcat3_port; } server { listen 80; server_name example.com; location / { proxy_pass http://tomcat_cluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } } ``` 在上面的配置中,tomcat_cluster是一个upstream实例,包含了所有tomcat实例的IP地址和端口号。nginx会将请求转发到tomcat_cluster中的一个实例上。 3. 配置session共享,以便在tomcat集群中的不同实例之间共享用户会话信息。可以使用memcached或redis等分布式缓存来实现session共享。 4. 配置负载均衡器,以便nginx可以根据不同的负载均衡算法来分配请求到不同的tomcat实例上。可以使用nginx自带的负载均衡模块或第三方模块,如nginx-upsync-module等。 5. 测试集群的可伸缩性和容错性,以确保tomcat集群可以在高负载和节点故障的情况下正常运行。 总结来说,要实现nginx和tomcat一起部署springboot项目并实现tomcat集群,需要进行反向代理、session共享、负载均衡器等多方面的配置和测试。这样可以保证项目的高可用性和可伸缩性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值