windows版:nginx+tomcat+redis负载均衡
详情
因为是几年前的笔记所以版本较低
下载地址
nginx下载地址:http://nginx.org/en/download.html
tomcat下载地址:http://tomcat.apache.org/download-70.cgi
步骤
Nginx+Tomcat
-
准备下载好的文件nginx-1.9.14、tomcat7文件
2. 首先安装nginx服务,并验证解压nginx-1.9.14.zip文件 进入nginx-1.9.14文件夹, 启动nginx的命令: start
nginx.exe 验证是否启动成功的方式:
方式一:在进程中查看配置,进程中包含两个nginx.exe
方式二:在浏览器中查看,默认端口号为80
快速停止或关闭nginx命令:nginx -s stop 正常停止或关闭nginx命令:nginx -s quit
配置文件修改重装载命令:nginx -s reload3.多个tomcat部署项目 复制两个tomcat,在tomcat中将webapps中的ROOT文件夹都删掉(注意:使用在server.xml文件中配置,启动tomcat时,将在webapps文件夹中生成ROOT文件夹,访问时不加项目名)
修改server.xml文件中的端口号,第一个tomcat中修改端口号为8081,第二个tomcat中修改端口
启动tomcat,访问登录页面http://localhost:8081/html/login.jsp
项目所在位置以及目录结构(注意:引入的文件夹是发布项目的文件夹,里面包括.class文件): 注意: 与 之间的差别解决方案:使用引入war包的时候,同时启动两台tomcat时,会将发布信息分布发布到两台tomcat中发布,当文件上传之后,当我不断
刷新页面展示图片时,会一会展示,一会找不到路径,所以最好建议使用引入文件夹的方式
4.nginx+tomcat结合发布项目,修改nginx-1.9.14文件夹下的conf文件夹下的nginx.conf文件
在http标签中添加一段代码:
#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 backend {
server localhost:8081; #连接本地端口号为8081的服务器
server localhost:8082; #连接本地端口号为8082的服务器
ip_hash;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#代表对所有请求进行拦截 location / { root html; index index.html index.htm; proxy_pass http://backend; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_connect_timeout 1; proxy_read_timeout 1; proxy_send_timeout 1;
}
#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;
# }
# }
}
将配置中
location / {
root html;
index index.html index.htm;
}
修改为
#代表对所有请求进行拦截
location / { root html;
index index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
}
nginx负载均衡常见策略
- 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
- weight(权重) 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。.
- ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
- fair(第三方)按后端服务器的响应时间来分配请求,响应时间短的优先分配。
- url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效
注意:ip_hash中黏贴的时候,容易导致连环坏服务器,所以一般选择策略为第三方策略运用(原因:主要是第三方分布服务器的时候有 一个算法,可以均匀的分布请求到服务器上)
6、测试nginx访问的到底是tomocat1 , 还是tomcat2的方式,使用火狐浏览器,查看控制台中网络下面的cookie
- tomcat1中的conf文件夹中的server.xml文件修改, 将105行添加
<Engine name="Catalina" defaultHost="localhost" jvmRoute="t1">
- tomcat2中的conf文件夹中的server.xml文件修改 将105行添加
<Engine name="Catalina" defaultHost="localhost" jvmRoute="t2" jvmRoute="t2">
基于redis共享session
-
安装redis服务器(windows版本),并启动redis服务器 redis-server.exe redis.conf
(redis默认的端口 号为6379) 启动成功,没有连接,所以连接clients为0个。 -
验证redis客户端是否可以连接到redis服务器 1) 通过redis客户端连接redis服务器:redis-cli.exe -h 127.0.0.1 -p 6379 2)
设置信息:set key value 获取信息: get key -
启动一个redis客户端,命令执行
查看redis服务器,看是否有一个连接
使用redis客户端,设置值,并获取值
redis服务器设置成功。
redis常见命令:
redis keys * :查看所有的key值
flush db: 清空所有的key值
下载redis和tomcat融合时所需要的jar包,将其放到各个tomcat的lib文件夹下
解压之后为:
将依赖包放到tomcat1中,即t1中,
将redis依赖包放到tomcat2中
配置各个tomcat中的context.xml文件和redis服务器进行通信
将tomcat1和tomcat2中conf文件夹下的context.xml文件中添加信息如下
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
</Context>
启动redis服务器
启动tomcat1和tomcat2,并启动nginx服务器
启动tomcat1:
启动tomcat2:
启动nginx服务器
查看redis服务器的连接(注意redis服务器的连接是根据访问的请求进行递增的)
访问请求之前redis服务器展示:
访问请求之后,连接递增:
项目中使用redis服务器,集成到tomcat中,以获得信息