1、环境

两台(centos)机器,每台机器跑2个web应用

ip web
192.168.0.109 http://192.168.0.109:8081
192.168.0.109 http://192.168.0.109:8082
192.168.0.111 http://192.168.0.111:8083
192.168.0.111 http://192.168.0.111:8084

 

 

 

 

 

 

 

 

 

注:由于我本机是ubuntu系统,我是在ubuntu系统上面安装的虚拟机,跑了2个centos7,如果发现截图怎么那么像ubuntu的色调,别感到意外,以下的截图均为centos7,具体型号为centos7-1810 ,web应用的准备工具为idea

 

2、web应用准备

为了便于验证,配置了一个干净的springboot应用

代码请参考spring官网:https://spring.io/guides/gs/rest-service/

在此基础上进行修改,只是修改了返回内容,返回当前应用的端口(4个应用的端口分别为8081,8082,8083,8084)

nginx tomcat、springboot 配置

3、两台机器均安装nginx服务

可以在官网查看安装步骤,比如我是2台centos机器

http://nginx.org/en/linux_packages.html#RHEL-CentOS

安装步骤参考官网进行安装

4、配置nginx

安装完成后,配置文件位置为:/etc/nginx

nginx tomcat、springboot 配置

修改nginx.conf

vim nginx.conf

在http的{}中添加一下内容:

server{
    #nginx端口
    listen 8080;
   #服务名称
    server_name 192.168.0.109;
    location / {
       #proxy_pass 后边的地址需要与下方的upstream后边的名称一样
       #此处为tomcats
       proxy_pass http://tomcats;
       root html;
        index index.html index.htm;
    }
}
#app的配置
upstream tomcats{
    #weight为权重,值越大,分配的就越多
    server 192.168.0.109:8081 weight=10;
    server 192.168.0.109:8082 weight=10;
    server 192.168.0.111:8083 weight=10;
    server 192.168.0.111:8084 weight=10;
}     

两台机器均要配置

5、启动nginx

我是在centos中根据官方文档在线安装,所以,命令可以直接调用

nginx

#停止命令  nginx -s stop

如下图:

nginx tomcat、springboot 配置

证明nginx已经启动起来了,访问nginx,不断的刷新页面,会发现四个web应用均会被分配到:

nginx tomcat、springboot 配置nginx tomcat、springboot 配置nginx tomcat、springboot 配置nginx tomcat、springboot 配置nginx tomcat、springboot 配置

 

分类:

技术点:

相关文章: