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

来源:互联网 发布:上海网络教育学校排名 编辑:程序博客网 时间:2024/04/28 13:23

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以服务的形式启动可以不用,软件说明看官方文档如下

  1. 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的安装,下载最新版本,放到指定目录(免安装),执行如下命令,就是解压与启动

  1. cd c:\
  2. unzip nginx-1.11.2.zip
  3. cd nginx-1.11.2
  4. start nginx

使用如下命令来调节Nginx

  1. nginx -s stop fast shutdown
  2. nginx -s quit graceful shutdown
  3. nginx -s reload changing configuration, starting new worker processes with a new configuration, graceful shutdown of old worker processes
  4. nginx -s reopen re-opening log files

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

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

2.2.将Nginx安装成系统服务

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

  1. <service>
  2. <id>nginx</id>
  3. <name>nginx</name>
  4. <description>nginx</description>
  5. <executable>D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0\nginx.exe</executable>
  6. <logpath>D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0\logs\</logpath>
  7. <logmode>roll</logmode>
  8. <depend></depend>
  9. <startargument>-p D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0</startargument>
  10. <stopargument>-p D:\BALANCE_TOMCAT__NGINX\nginx-1.6.0 -s stop</stopargument>
  11. </service>

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

  1. To install a service, run myapp.exe install
  2. To start a service, run myapp.exe start
  3. To stop a service, run myapp.exe stop
  4. To restart a service, run myapp.exe restart
  5. To uninstall a service, run myapp.exe uninstall

2.3.安装Tomcat7与redis

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

3.各种配置

3.1.配置Nginx

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

  1. nginx.conf
  2. #user nobody;
  3. worker_processes 2;
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. #pid logs/nginx.pid;
  8. events {
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. # '$status $body_bytes_sent "$http_referer" '
  16. # '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. #gzip on;
  23. upstream localhost { # 发到localhost上的请求,通过Nginx转发到实际处理请求的服务器
  24. server 172.20.41.201:8980 weight=1;
  25. server 172.20.41.201:8780 weight=1;
  26. }
  27. server {
  28. listen 8999;
  29. server_name localhost;
  30. #charset koi8-r;
  31. #access_log logs/host.access.log main;
  32. location / {
  33. root html; # 请求资源的路径(代理:/home/shirdrn/servers/nginx/tml/solr/,该目录下没有任何数据)
  34. index index.html index.htm;
  35. proxy_pass http://localhost; # 代理:对发送到localhost上请求进行代理
  36. include proxy.conf; # 引入proxy.conf配置
  37. }
  38. #error_page 404 /404.html;
  39. # redirect server error pages to the static page /50x.html
  40. #
  41. error_page 500 502 503 504 /50x.html;
  42. location = /50x.html {
  43. root html;
  44. }
  45. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  46. #
  47. #location ~ \.php$ {
  48. # proxy_pass http://127.0.0.1;
  49. #}
  50. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  51. #
  52. #location ~ \.php$ {
  53. # root html;
  54. # fastcgi_pass 127.0.0.1:9000;
  55. # fastcgi_index index.php;
  56. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  57. # include fastcgi_params;
  58. #}
  59. # deny access to .htaccess files, if Apache's document root
  60. # concurs with nginx's one
  61. #
  62. #location ~ /\.ht {
  63. # deny all;
  64. #}
  65. }
  66. # another virtual host using mix of IP-, name-, and port-based configuration
  67. #
  68. #server {
  69. # listen 8000;
  70. # listen somename:8080;
  71. # server_name somename alias another.alias;
  72. # location / {
  73. # root html;
  74. # index index.html index.htm;
  75. # }
  76. #}
  77. # HTTPS server
  78. #
  79. #server {
  80. # listen 443 ssl;
  81. # server_name localhost;
  82. # ssl_certificate cert.pem;
  83. # ssl_certificate_key cert.key;
  84. # ssl_session_cache shared:SSL:1m;
  85. # ssl_session_timeout 5m;
  86. # ssl_ciphers HIGH:!aNULL:!MD5;
  87. # ssl_prefer_server_ciphers on;
  88. # location / {
  89. # root html;
  90. # index index.html index.htm;
  91. # }
  92. #}
  93. }

需要用到proxy.conf如下

  1. proxy_redirect off;
  2. proxy_set_header Host $host;
  3. proxy_set_header X-Real-IP $remote_addr;
  4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  5. client_max_body_size 10m;
  6. client_body_buffer_size 128k;
  7. proxy_connect_timeout 300;
  8. proxy_send_timeout 300;
  9. proxy_read_timeout 300;
  10. proxy_buffer_size 4k;
  11. proxy_buffers 4 32k;
  12. proxy_busy_buffers_size 64k;
  13. proxy_temp_file_write_size 64k;

3.2配置Tomcat7

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

  1. tomcat-redis-session-manager-tomcat-7-1.1.jar
  2. jedis-2.0.0.jar
  3. commons-pool-1.6.jar

修改Tomcat 7进行Session共享,打开context.xml添加下面代码,注意顺序

  1. <!-- Uncomment this to disable session persistence across Tomcat restarts -->
  2. <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
  3. <!--<Manager pathname="" />-->
  4. <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
  5. host="localhost"
  6. port="6379"
  7. database="0"
  8. maxInactiveInterval="60" />

配置完的context.xml如下

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <!-- The contents of this file will be loaded for each web application -->
  17. <Context>
  18. <!-- Default set of monitored resources -->
  19. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  20. <!-- Uncomment this to disable session persistence across Tomcat restarts -->
  21. <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
  22. <!--<Manager pathname="" />-->
  23. <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
  24. host="localhost"
  25. port="6379"
  26. database="0"
  27. maxInactiveInterval="60" />
  28. <!-- Uncomment this to enable Comet connection tacking (provides events
  29. on session expiration as well as webapp lifecycle) -->
  30. <!--
  31. <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
  32. -->
  33. </Context>

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

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



0 0
原创粉丝点击