阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Tomcat+Redis+Nginx配置详解

136次阅读
没有评论

共计 3694 个字符,预计需要花费 10 分钟才能阅读完成。

为客户开发的一个绩效系统,采用了 Java Web 的开发方式,使用了一些 Spring MVC, Mybatis 之类的框架。相比于 Oracle EBS 的二次开发,这种开发更加灵活,虽然和 ebs 集成的时候遇到一些问题,但是最后也都解决了。

在部署的时候,客户要求要能同事承受一两千人在线,相对于客户公司的总人数(七八万人),应该足够了。ebs 的二次都是直接部署在 oracle ebs 的 application server 上面,之前也没怎么关注过程序的部署。这次采用 tomcat 部署,考虑到单个 tomcat 的最大也就能承受 500 左右的在线人数,这次采用了一个小的集群部署,使用了 5 个 tomcat,反向代理使用的 nginx。

现在程序基本稳定,压力测试也都能没什么大的问题,趁着有时间,把部署和配置都整理一下。

准备

  • apache tomcat 7.0.55
  • nginx 1.7.2
  • redis 2.8.9

配置环境使用三个 tomcat, 三台 tomcat、redis 和 nginx 都在一台机器上,为了方便测试和部署。

大致的整个配置的架构:

Tomcat+Redis+Nginx 配置详解

在这个图中,nginx 做为反向代理,将客户请求根据权重随机分配给三台 tomcat 服务器,redis 做为三台 tomcat 的共享 session 数据服务器。
 
规划

redis
localhost:6379

nginx
localhost:80

tomcat
localhost:8081
localhost:8082
localhost:8083
 
配置

tomcat

修改 tomcat 文件夹中 conf/context.xml 文件,在 context 节点下添加如下配置:
<Valve  className=”com.radiadesign.catalina.session.RedisSessionHandlerValve” />
<Manager className=”com.radiadesign.catalina.session.RedisSessionManager”
    host=”localhost”
    port=”6379″
    database=”0″
    maxInactiveInterval=”60″ />

conf/server.xml 文件中的端口根据规划依次修改。

另外要在 tomcat 的 lib 文件夹下分别添加三个 jar 文件,这个地方 jar 文件的版本有可能会有冲突,配置的时候需要多尝试。我这里的版本如下,是验证过可以使用的,通过 maven 的库都可以下载到。

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

jedis-2.2.0.jar

commons-pool-1.6.jar

nginx

修改 nginx 文件目中的 conf/nginx.conf 文件为:
#user  nobody;
worker_processes  1;

error_log  logs/error.log;

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  {
          server  localhost:8081 weight=1; 
          server  localhost:8082 weight=2; 
    server  localhost:8083 weight=3;
 } 

 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://localhost; 
         proxy_set_header  X-Real-IP  $remote_addr; 
         client_max_body_size  100m; 
     }

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

 }
}

redis 的配置就直接使用默认配置,因为只是测试用,和 tomcat 一样没有做参数优化配置。
 
运行

分别启动 redis、nginx 和三台 tomcat。

Tomcat+Redis+Nginx 配置详解

Tomcat+Redis+Nginx 配置详解

Tomcat+Redis+Nginx 配置详解
 
测试

在三个 tomcat 的 webapps/ROOT 目录下,分别添加 session.jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>shared session</title>
</head>
<body>
 <br>session id=<%=session.getId()%>
 <br>tomcat 3
</body>
</html>

注: 每个 tomcat 下的标示不同

Tomcat+Redis+Nginx 配置详解

Tomcat+Redis+Nginx 配置详解

Tomcat+Redis+Nginx 配置详解 

从截图中,可以看出,分别访问了不同的 tomcat,但是得到的 session 却是相同的,说明达到了集群的目的。

在这个架构中,有个明显的瓶颈,就是数据库。因为使用了企业级的 Oracle 数据库,所以在压力测试种也没有出现大的问题。但是作为后续的可以优化的地方,数据库是一定要做读写分离的。

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

Nginx 403 forbidden 的解决办法  http://www.linuxidc.com/Linux/2017-08/146084.htm

CentOS 7 下 Nginx 服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx 服务的 SSL 认证和 htpasswd 认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Ubuntu 16.04 上启用加密安全的 Nginx Web 服务器  http://www.linuxidc.com/Linux/2017-07/145522.htm

Linux 中安装配置 Nginx 及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9 编译安装 Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-11/148250.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3694字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中