4321pp 发表于 2017-8-29 09:36:16

Linux 下 tomcat基于nginx做负载均衡

测试目的:在一台装有nginx服务器上访问nginx这台的ip地址,刷新一次就会显示后端三台不同的tomcat服务器的测试页。
测试环境:三台centos 6.8
               一台 centos 7.3

软件版本: nginx 1.12.1
                  tomcat 8

软件部署的话 就不操作了 之前已经部署好了的,不会的话 看我之前的博客里都有。
nginx 安装
http://dklwj.blog.51cto.com/9199080/1949570

tomcat 安装:
http://dklwj.blog.51cto.com/9199080/1955403

在三台tomcat上做些修改
1、把/usr/local/tomcat/webapps下原有的东西都删了除ROOT目录留着,不过ROOT目录下的东西可以清空然后把咱自己用到的测试页及所需用到的文件佳放进去。
# ls
classesindex.jsplibMETA-INFWEB-INF--这几个文件的话是java必须有的文件
然后编辑index.jsp文件,后面两个的话 从第一个配置好的服务器拷贝过去 修改下里头的标题这里的话 我就用A、B、C 来代替三台服务器页面的测试标题。然后保存退出即可。

# vim index.jsp
<%@ page language="java" %>

<html>
      <head>
                <title>tomcatB</title>
      </head>
      <body>
                <h1><font color="blue"> tomcatB.dklwj.com</font></h1>
                <table align="centre" border="1">
                <tr>
                  <td>Session ID</td>
          <% session.setAttribute("dklwj.com","dklwj.com"); %>
                <td> <%= session.getId() %></td>
                </tr>
                <tr>
                   <td>Created on </td>
                   <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
      </body>
</html>
启动tomcat

$ /usr/local/tomcat/bin/startup.shUsing CATALINA_BASE:   /usr/local/tomcatUsing CATALINA_HOME:   /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME:      /usr/local/jdkUsing CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarTomcat started.用ss -tnl看下端口是否被监听

# ss -tnl
State      Recv-Q Send-Q                              Local Address:Port                              Peer Address:Port
LISTEN   0      1                              ::ffff:127.0.0.1:8005                                          :::*   
LISTEN   0      100                                          :::8009                                          :::*   
LISTEN   0      100                                          :::8080                                          :::*   
LISTEN   0      128                                          :::22                                          :::*   
LISTEN   0      128                                             *:22                                             *:*   
其它两台tomcat配置一样这里就忽略了。
配置nginx服务器
# cd /etc/nginx/
# vim nginx.conf
在http配置栏下 增加以下一段

    upstream tcsrvs {
      server 192.168.2.32:8080;
      server 192.168.2.38:8080;
      server 192.168.2.39:8080;

    }
在server配置里头加上,然后保持退出;
server {
      listen       80;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

      location / {
            root   html;
            indexindex.html index.htm;
         proxy_pass http://tcsrvs;
      }
用nginx -t 检查配置文件是否报错。
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#
然后浏览器验证:一刷新就跳到其它两台上去,说明测试成功了。

页: [1]
查看完整版本: Linux 下 tomcat基于nginx做负载均衡