您的位置:首页 > 数据库 > Redis

Windows下Nginx+tomcat7+redis负载均衡与session共享

2016-07-25 14:47 477 查看
1.需要的软件列表.
2.软件安装
2.1.Nginx的安装,下载最新版本,放到指定目录(免安装),执行如下命令,就是解压与启动
2.2.将Nginx安装成系统服务
2.3.安装Tomcat7与redis
3.各种配置
3.1.配置Nginx
3.2配置Tomcat7


1.需要的软件列表.

Windows下利用Nginx+Tomcat7+redis负载均衡实现起来要比Linux下稍微麻烦很多,有些软件本身对Windows支持不好,再一个无法通过解压安装的形式自动配置软件自启动,所以要依赖的软件相对比较多.

1.官网下载nginx最新稳定版本,文章中使用nginx1.11.2. 

2.Tomcat 7,小版本随意,理论上越大越好. 

3.官网下载redis最新稳定版本,最好有redis单独的Linux服务器,如果没有就去社区下载windows版本. 

4.Windows Service Wrapper,一个可以将可执行程序安装成服务的软件,如果不需要nginx以服务的形式启动可以不用,软件说明看官方文档如下
https://kenai.com/projects/winsw/pages/Home


5.利用redis实现session共享还要使用GitHub上的一个软件,它利用gradle打包,如果你要自行打包那你必须安装gradle,可以下载我的附件,以打包完毕,当然你也可以使用其他方式实现session共享,例如memcached或Mongo. 

6.其它依赖,jedis-2.0.0.jar;commons-pool-1.6.jar


2.软件安装


2.1.Nginx的安装,下载最新版本,放到指定目录(免安装),执行如下命令,就是解压与启动

cd c:\

unzip nginx-1.11.2.zip

cd nginx-1.11.2

start nginx


使用如下命令来调节Nginx
nginx -s stop   fast shutdown

nginx -s quit   graceful shutdown

nginx -s reload changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes

nginx -s reopen re-opening log files


具体可以看官网上面nginx for Windows这一块

http://nginx.org/en/docs/windows.html



2.2.将Nginx安装成系统服务

使用Windows Service Wrapper来将Nginx安装成服务,将软件拷贝到Nginx根目录下,然后重命名一个你喜欢的名字如:myapp.exe,然后在同级目录下建立一个同名的配置文件myapp.xml,里面内容按如下格式
<service>

<id>nginx</id>

<name>nginx</name>

<description>nginx</description>

<executable>D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0\nginx.exe</executable>

<logpath>D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0\logs\</logpath>

<logmode>roll</logmode>

<depend></depend>

<startargument>-p D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0</startargument>

<stopargument>-p D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0 -s stop</stopargument>

</service>


然后myapp.exe install来安装成服务,ok打完收工,下面一些命令可以调节,可以看官网文档
To install a service, run myapp.exe install

To start a service, run myapp.exe start

To stop a service, run myapp.exe stop

To restart a service, run myapp.exe restart

To uninstall a service, run myapp.exe uninstall



2.3.安装Tomcat7与redis

windows下面安装Tomcat7只需要解压即可,很方便; 

redis如果是windows版本那很简单下载安装即可,Linux下面可能还挺麻烦需要各种配置参数,具体自查,不过最新版本软件包中自带安装脚本,也很方便.


3.各种配置


3.1.配置Nginx

Nginx作为反向代理还是很出名的,如果需要深入了解Nginx可以去了解相关方面的内容,如果只是想用起来那么只需要简单的了解即可,在这里不做具体的讲解,只列出我用到的配置文件,里面参数根据现场环境微调

nginx.conf


#user  nobody;

worker_processes  2;


#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 localhost { # 发到localhost上的请求,通过Nginx转发到实际处理请求的服务器

server 172.20.41.201:8980 weight=1;

server 172.20.41.201:8780 weight=1;

}


server {

listen       8999;

server_name  localhost;


#charset koi8-r;


#access_log  logs/host.access.log  main;


location / {

root   html; # 请求资源的路径(代理:/home/shirdrn/servers/nginx/tml/solr/,该目录下没有任何数据)

index  index.html index.htm;

proxy_pass   http://localhost; # 代理:对发送到localhost上请求进行代理

include proxy.conf; # 引入proxy.conf配置

} 


#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;[/code] 
#}


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

#}

#}


}


需要用到proxy.conf如下
proxy_redirect          off;

proxy_set_header        Host $host;

proxy_set_header        X-Real-IP $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size    10m;

client_body_buffer_size 128k;

proxy_connect_timeout   300;

proxy_send_timeout      300;

proxy_read_timeout      300;

proxy_buffer_size       4k;

proxy_buffers           4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;



3.2配置Tomcat7

下载第一步中的相关jar包,编译或下载Tomcat7下session共享jar 包,将这几个包放入到tomcat的bin目录下面

tomcat-redis-session-manager-tomcat-7-1.1.jar

jedis-2.0.0.jar

commons-pool-1.6.jar


修改Tomcat 7进行Session共享,打开context.xml添加下面代码,注意顺序
<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />

<!--<Manager pathname="" />-->

<Manager className="com.radiadesign.catalina.session.RedisSessionManager"

host="localhost"

port="6379"

database="0"

maxInactiveInterval="60" />


配置完的context.xml如下
<?xml version='1.0' encoding='utf-8'?>

<!--

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements.  See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the "License"); you may not use this file except in compliance with

the License.  You may obtain a copy of the License at


http://www.apache.org/licenses/LICENSE-2.0[/code] 

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

-->

<!-- The contents of this file will be loaded for each web application -->

<Context>


<!-- Default set of monitored resources -->

<WatchedResource>WEB-INF/web.xml</WatchedResource>


   <!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />

<!--<Manager pathname="" />-->

<Manager className="com.radiadesign.catalina.session.RedisSessionManager"

host="localhost"

port="6379"

database="0"

maxInactiveInterval="60" />



<!-- Uncomment this to enable Comet connection tacking (provides events

on session expiration as well as webapp lifecycle) -->

<!--

<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />

-->


</Context>


好了现在将Tomcat拷贝多份,修改server.xml中的端口信息,然后在nginx.conf下添加多个tomcat进行负载均衡即可.

相关软件:因为没找到在哪里上传我本地的jar包,需要的自己去下载或编译吧,或者私信我,我发你好了,尽量自己下载.


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息