1、Tomcat的配置
1、系统环境变量配置:
首先要实现Tomcat的集群就得拥有多个tomcat,所以我在本地电脑下载了两个Tomcat,我这里使用的是Tomcat7,当然,配置与Tomcat的版本没多大关系~

下载之后我们先来配置好环境变量:


在我们的系统变量中增加上面六个对应变量值,对应变量的对应值,是根据自己Tomocat所在的目录决定的,配置的时候自己注意下~
2、更改Tomcat的配置文件
1、tomcat1的修改
首先,我们弄了两个Tomcat,一个tomcat1
、一个tomcat2
,我们以tomcat1
为默认tomcat,然后在tomcat2
的基础上更改。
tomcat1
上我们只增加一个UTF-8
的配置,我们打开tomcat1
conf
目录下面的server.xml
文件,添加如图所示的:URIEncoding="UTF-8"
配置,防止乱码

1、tomcat2的修改
首先,
1、tomcat2
要修改的第一点是和上面 tomcat1
一样,增加URIEncoding="UTF-8"
配置,防止乱码。
2、更改tomcat2
的server.xml
配置文件里面的相关端口,如下图所示操作即可。



3、修改
tomcat2
/bin
目录下面的相关文件:
修改上图所示的
catalina.bat
和startup.bat
这两个配置文件使用文本编辑器:
Sublime Text
或者Notepad++
软件打开上面两个.bat
文件:需要更改两个地方:
①:我们需要将这两个
.bat
文件里面的 CATALINA_HOME
全部替换成:CATALINA_2_HOME
②:
CATALINA_BASE
全部替换成:CATALINA_2_BASE
至此,我们的Tomcat相关的配置就差不多修改完了,为了增加两个Tomcat的区别。
我们把
tomcat2
/webapps
/ROOT
目录下面的tomcat.png
换个图标。
好,接下来我们就来运行下试试吧~
我们进对应tomcat下面的
bin
目录,使用CMD
运行startup.bat
,如图所示:我们看到下面红色框框中的对比,这就是我们为什么更改上面相关配置文件的原因了,因为不改的话两个同时启动就会有冲突
既然tomcat都跑起来了我们就在浏览器里面访问看看吧~

可以看到,我们的两个Tomcat都正常跑起来啦~
2、Nginx实现负载均衡
1、修改本地host
:
在host
文件最下面添加127.0.0.1 www.test123.com
,然后保存(注意www前面还有空格的)

2、修改Nginx相关配置:
1、打开Nginx conf
/nginx.conf
文件:下对应行添加入下图所指代码
include vhost/*.conf;

2、在Nginx根目录新建vhost
文件夹:

3、在
vhost
问价夹下新建:www.imooc.com.conf
文件,给这个配置文件添加下面的代码:
upstream www.test123.com{
server 127.0.0.1:8080;
server 127.0.0.1:9080;
}
server {
listen 80;
autoindex on;
server_name www.test123.com;
access_log c:/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
location / {
proxy_pass http://www.test123.com;
add_header Access-Control-Allow-Origin *;
}
}
好,至此我们的相关配置就差不多配置完啦~
我们下面启动Nginx看看:
在Nginx目录打开cmd

然后执行:start nginx
接下来我们访问在浏览器里面访问试试:

我们可以看到,访问同一个网址进入到不同的tomcat里面啦~
当然我们还可以设置访问不同tomcat的权重:
在上面的配置文件中配置即可,当然,我们配置完之后记得在Nginx里面执行Nginx -s reload
哦~ 以此来刷新我们刚添加的配置。

权重越大,访问对应的几率也就越大~
网友评论