单台主机nginx+tomcat+mencached部署测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单台主机nginx+tomcat+mencached部署测试相关的知识,希望对你有一定的参考价值。
单台主机部署 亦可分布式部署 改动配置ip即可
多个tomcat要一起协同工作有几种办法,可以考虑的方案有以下几个:
1. 使用tomcat自带的cluster方式,多个tomcat间自动实时复制session信息,配置起来很简单。但这个方案的效率比较低,在大并发下表现并不好。
2. 利用nginx的基于访问ip的hash路由策略,保证访问的ip始终被路由到同一个tomcat上,这个配置更简单。但如果应用是某一个局域网大量用户同时登录,这样负载均衡就没什么作用了。
3. 利用memcached把多个tomcat的session集中管理,前端在利用nginx负载均衡和动静态资源分离,在兼顾系统水平扩展的同时又能保证较高的性能。
nginx反向代理HTTP Server,用于实现资源缓存、web server负载均衡等功能
memcached服务进行 session 统一存储管理的共享方案。
系统环境
Centons6.5 Tomcat 7.0.69 jdk1.8.0_91Nginx 1.8.1 pcre-8.38 Memcached 1.4.25 Libevent 2.0.22-stable
Centons使用最小安装版,安装完更新系统
yum –y update
给普通用户添加sudo命令
如下给用户dnhc添加sudo命令权限
vi /etc/sudoers
root ALL=(ALL) ALL
dnhc ALL=(ALL) ALL
安装nginx
添加运行nginx用户以及用户组
sudo groupadd -r nginx
sudo useradd -s /sbin/nologin -g nginx -r nginx
查询是否创建了用户
id nginx
安装nginx所需的依赖包 安装路径一个人习惯 我统一安装在opt文件下 所有的安装文件放在opt文件下
yum install -y gcc gcc-c++ libgcc
yum -y install openssl openssl-devel
tar -zvxf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/opt/prce
make && make install
cd ..
tar -zvxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/opt/nginx --user=nginx --group=nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-pcre=/opt/pcre-8.38
make && make install
防火墙添加80端口
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
保存重启防火墙
service iptables restart
启动 停止 重启nginx
/opt/nginx/sbin/nginx
/opt/nginx/sbin/nginx -s stop
/opt/nginx/sbin/nginx -s reload
对nginx反向代理 配置
vi /etc/nginx/nginx.conf
#user nobody;
user nginx nginx;
worker_processes 6;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65533 ;
}
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;
gzip_min_length 1024k;
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain text/javascript text/css application/xml application/x-javascript application/json;
gzip_vary on;
upstream opc {
#ip_hash;
server 192.168.0.151:8080 max_fails=3 fail_timeout=30s;
server 192.168.0.151:8081 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name 192.168.0.151;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://opc;
proxy_redirect default;
proxy_connect_timeout 10;
}
#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;
# }
#}
}
安装memcached
安装依赖包
tar -zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/opt/libevent
make && make install
cd ..
tar -zxvf memcached-1.4.25.tar.gz
cd memcached-1.4.25
./configure --prefix=/opt/memcached --with-libevent=/opt/libevent
make && make install
启动memcache
useradd memcache #添加memcache用户 或者直接用root链接
memcached -m 64 -d -u memcache -p 11211 -l 192.168.0.233 -c 5000 -P /var/run/memcached.pid
/opt/memcached/bin/memcached -d -m 128 -u root -p 11211 -c 256 -P /var/run/memcached.pid
安装tomcat和jdk
tar -zxvf apache-tomcat-7.0.69.tar.gz
tar -zxvf jdk-8u91-linux-x64.tar.gz
cp -ri /opt/apache-tomcat-7.0.69 /opt/tomcat/tomcat3
cp -ri /opt/apache-tomcat-7.0.69 /opt/tomcat/tomcat4
编辑全局变量文件
vi /etc/profile
在最后面添加
export JAVA_HOME=/opt/jdk1.8.0_91
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
保存后执行 java -version 可显示jdk 的版本为配置成功
因为是同一台主机多实例tomcat 所有两个tomcat的端口不能冲突否则会启动不来
编辑
vim /opt/tomcat/tomcat3/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
vim /opt/tomcat/tomcat4/conf/server.xml
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8444" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat4">
配置session共享文件
vim /opt/tomcat/tomcat3/conf/context.xml
vim /opt/tomcat/tomcat4/conf/context.xml
在context下直接添加
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.0.151:11211"
sticky="false"
lockingMode="auto"
sessionBackupAsync="false"
sessionBackupTimeout="1000"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
把所需的jar文件放到tomcat的lib文件夹下 所有文件都已打包测试
启动tomcat
cd /opt/tomcat/tomcat3/bin/ ./startup.sh
cd /opt/tomcat/tomcat4/bin/ ./startup.sh
测试页面
vi /opt/tomcat/tomcat3/webapps/ROOT/test.jsp
vi /opt/tomcat/tomcat4/webapps/ROOT/test.jsp
分别加入下面内容保存
Server Info:
SessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<br>
<%
out.println("111");
%>
访问两个tomcat
192.168.0.151:8080/test.jsp
192.168.0.151:8081/test.jsp
测试成功
感谢各位大大的相关文章
以上是关于单台主机nginx+tomcat+mencached部署测试的主要内容,如果未能解决你的问题,请参考以下文章
|NO.Z.00004|——————————|^^ 构建 ^^|——|Nginx&Nginx.V1.16&部署&降级&升级.V3|