研究了下nginx+tomcat+memcached 的配置,练习了下脚本的书写,参考了好几篇文章,在此记录一下,有不对的地方敬请指正。
系统环境:
RHEL6.5 x64
iptables -F & selinux is disabled
主机角色:
node1 :192.168.122.101 :nginx tomcat memcached
node2 :192.168.122.102 :tomcat memcached
https://code.google.com/memcached-session-manager
memcached的session管理
nginx做反向代理两台tomcat,用memcached同步session,防止数据丢失
Tomcat1将session存储在memcacted2上.tomcat和Memcached使用交叉存储,只有当M2不可用时T1才将数据存储在M1上(M1是T1的failoverNode),使用这种方式可以避免单点故障.从而实现应用的高可用性.
注意:两台node上的tomcat应完全一致
1.在两台node主机上配置tomcat环境
<code><span>#./jdk-6u32-linux-x64.bin</span><span># mv jdk1.6.0_32/ /usr/local/lnmp/jdk</span><span>#vim /etc/profile 编写环境变量</span>export JAVA_HOME=/usr/local/lnmp/jdk export CLASSPATH=:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$PATH:$JAVA_HOME/bin <span>#source /etc/profile</span>测试java能否正常工作 <span>#vim test.java</span><span>public</span><span><span>class</span><span>test</span>{</span><span>public</span><span>static</span><span>void</span> main(String[] args) {System.out.println(<span>"Hello!"</span>); } } <span>#javac test.java 编译</span><span>#java test 执行后出现Hello!说明java环境配置好</span></code>
安装tomcat服务器
<code><span>#tar zxf apache-tomcat-7.0.37.tar.gz 解压就能用,不需要编译</span><span>#mv apache-tomcat-7.0.37 tomcat</span><span>#/TOMCAT_ROOT_DIR/tomcat/webapps/ROOT tomcat的默认发布目录</span><span>#/TOMCAT_ROOT_DIR/tomcat/bin/startup.sh(shutdown.sh) tomcat默认的启动和关闭脚本</span></code>
tomcat默认开启8080端口,
测试http://192.168.122.101:8080 访问到tomcat默认的测试页
<code><span>#cd tomcat/webapps/ROOT</span><span>#cat test.jsp #测试页</span><span>this</span> time is: new java.util.Date()%></code>
测试访问;http://192.168.122.101:8080 显示当前时间
tomcat访问8080端口,使用nginx反向代理。
使用nginx发布jsp动态网页:
nginx配置文件:
cat /usr/local/nginx/conf/nginx.conf
<code><span>#user nginx nginx;</span> worker_processes <span>4</span>; <span>#error_log logs/error.log;</span><span>#error_log logs/error.log notice;</span><span>#error_log logs/error.log info;</span><span>#pid logs/nginx.pid;</span>events { use epoll; worker_connections <span>1024</span>; } http { upstream tomcat { <span>#两台tomcat负载均衡</span> sticky; <span>#session同步,为nginx的模块</span><span>server</span><span>192.168</span><span>.0</span><span>.1</span>:<span>8080</span>; <span>server</span><span>192.168</span><span>.0</span><span>.2</span>:<span>8080</span>; } include mime.types; default_type application/octet-stream; <span>#log_format main '$remote_addr - $remote_user [$time_local] "$request" '</span><span># '$status $body_bytes_sent "$http_referer" '</span><span>#access_log logs/access.log main;</span> sendfile on; <span>#tcp_nopush on;</span><span>#keepalive_timeout 0;</span> keepalive_timeout <span>65</span>; <span>#gzip on;</span><span>server</span> { listen <span>80</span>; server_name localhost; <span>#charset koi8-r;</span><span>#access_log logs/host.access.log main;</span> location / { root html; proxy_pass http:<span>//tomcat;</span><span>index</span><span>index</span>.html <span>index</span>.htm; } <span>#error_page 404 /404.html;</span><span># redirect server error pages to the static page /50x.html</span> error_page <span>500</span><span>502</span><span>503</span><span>504</span> /<span>50</span>x.html; location = /<span>50</span>x.html { root html; } location ~ \.jsp$ { proxy_pass http:<span>//tomcat;</span> } } nginx -t && nginx -s reload</code>
访问:http://192.168.122.101/test.jsp 测试
同步两台tomcat的内容,并且修改java环境变量/etc/profile
测试:http://192.168.122.102:8080/test.jsp
两台node(tomcat)准备好了
2、在node1上的nginx环境中发布网页
使用nginx的负载均衡功能,
体现在nginx.conf中:
<code><span>upstream</span> tomcat-lb { <span>server</span><span>192.168.122.101:8080</span>; <span>server</span><span>192.168.122.102:8080</span>; } <span>location</span><span>~ \.jsp$</span> { <span>proxy_pass</span><span>http://tomcat-lb</span>; } <span>nginx</span> -t && nginx -s reload</code>
测试:http://192.168.122.101/test.jsp
实现两台主机的负载均衡(访问nginx所在的node)
问题:在后台实现了nginx的负载均衡,但是当一个用户刷新数据时,数据总是变化的,试想,在动态页面,如果用户提交数据刷新后并没有提交到服务器上,并且必须要重新填写表格,这样会造成糟糕的用户体验。
解决:给nginx增加一个sticky模块.(需要重新编译nginx)
重新在nginx在进行模块化编译:
nginx-sticky-modules.tar.gz(解压就行)
<code><span>#tar zxf nginx-sticky-modules.tar.gz -C /root/nginx-1.4.2</span><span>#cd nginx-1.4.2/</span><span>#make clean</span><span>#./configure --prefix=/usr/local/nginx --add-module=$NGINX_PKG_DIR/$DIR/nginx-sticky-module-1.0 --with-http_ssl_module --with-http_stub_status_module</span><span>#make && make install</span><span>#vim nginx.conf</span>upstream linux { sticky; #装了nginx-sticky后的功能 server <span>192.168</span><span>.122</span><span>.101</span>:<span>8080</span> ; server <span>192.168</span><span>.122</span><span>.102</span>:<span>8080</span> ; }</code>
测试:http://192.168.122.101/test.jsp 后发现刷新不会来回负载(每个用户看到的其实不是一个tomcat上的数据,但是对用户是透明的)
3.nginx负载tomcat的jsp时,需要解决session共享:
使用memcache进行缓存(用户)后端数据,但是又要想到解决单点故障问题,因而可以采用两台memcache作为后端负载.
memcached默认端口11211,后端使用交叉存储(tomcat会将session同步,session自动寻找存储的memcached,但是默认是交叉存储,当一个memcached服务器坏掉,tomcat都会存到存活的memcached服务器上)
只要tomcat不宕掉,一切数据都还存在
但是当memcached宕掉,tomcat会向存活的memcached上存取
session 的序列化方案官方推荐的有 4 种:
java serialization
msm-kryo-serializer
msm-javolution-serializer
msm-xstream-serializer
其中性能最好的是Kryo,我们使用kryo来做
mecached服务器node1 and node2
<code><span>#yum install memcached -y</span><span>#/etc/init.d/memcached start</span></code>
memcached默认开启11211端口
测试访问:telnet localhost 11211
stats 查看状态
set user 0 0 3 存储新值(add replace)
get user 获取值
配置两台memcached服务器(tomcat),下载jar包(必须支持相关的java程序)
提供测试环境的包,解压即可用:
链接: http://pan.baidu.com/s/1mgIF9NU 密码: n5wu
asm-3.2.jar
couchbase-client-1.2.2.jar
kryo-1.03.jar
kryo-serializers-0.11.jar
memcached-session-manager-1.6.5.jar
memcached-session-manager-tc7-1.6.5.jar
minlog-1.2.jar
msm-kryo-serializer-1.6.5.jar
reflectasm-0.9.jar
spymemcached-2.10.3.jar
node1 and node2 同步内容
<code><span>#cd tomcat/lib/</span><span>#mget jar/* 下载tomcat的session共享管理包</span><span>#cd tomcat/conf/</span><span># vim context.xml</span><manager classname="<span">"de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes=<span>"n1:192.168.122.101:11211,n2:192.168.122.102:11211"</span> failoverNodes=<span>"n1"</span> #tomcat2需要写成n2 requestUriIgnorePattern=<span>".*\.(ico|png|gif|jpg|css|js)$"</span> transcoderFactoryClass=<span>"de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"</span> /> <span>#tomcat/bin/shutdown.sh #重启tomcat,以识别memcached-session-manager</span><span># tail -f logs/catalina.out #默认日志</span>INFO: MemcachedSessionService finished initialization, sticky <span>true</span>, operation timeout <span>1000</span>, with node ids [n2] and failover node ids [n1]</manager></code>
正常启动
编辑测试文件:(提交用户信息的jsp测试页)
<code># vim tomcat/webapps/ROOT/test.jsp <span>"text/html; charset=GBK"</span> %><span>"java.util.*"</span> %><span>html</span>><span>head</span>><span>title</span>>Cluster App Test<span><span>title</span>></span><span><span>head</span>></span><span>body</span>>Server Info: <span>request</span>.getLocalAddr() + <span>" : "</span> + <span>request</span>.getLocalPort()+<span>"<br>"</span>);%><span>"<br> ID "</span> + session.getId()+<span>"<br>"</span>); <span>String</span> dataName = <span>request</span>.getParameter(<span>"dataName"</span>); <span>if</span> (dataName != <span>null</span> && dataName.length() > <span>0</span>) { <span>String</span> dataValue = <span>request</span>.getParameter(<span>"dataValue"</span>); session.setAttribute(dataName, dataValue); } out.print(<span>"<b>Session list</b>"</span>); Enumeration e = session.getAttributeNames(); <span>while</span> (e.hasMoreElements()) { <span>String</span> name = (<span>String</span>)e.nextElement(); <span>String</span> value = session.getAttribute(name).toString(); out.println( name + <span>" = "</span> + value+<span>"<br>"</span>); System.out.println( name + <span>" = "</span> + value); } %><span>form</span><span>action</span>=<span>"test.jsp"</span><span>method</span>=<span>"POST"</span>>name:<span>input</span><span>type</span>=<span>text</span><span>size</span>=<span>20</span><span>name</span>=<span>"dataName"</span>><span>br</span>>key:<span>input</span><span>type</span>=<span>text</span><span>size</span>=<span>20</span><span>name</span>=<span>"dataValue"</span>><span>br</span>><span>input</span><span>type</span>=<span>submit</span>><span><span>form</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
session共享完成:两台tomcat和memcached做相同的配置(jdk,memcache,tomcat)
测试:
http://192.168.122.101/test.jsp
使用一台tomcat和另外一台的memcached进行session共享
任何一台tomcat或者memcached挂了都无所谓
session会记录并且保持用户的数据信息
我同时写了一键安装的脚本,还有一些问题,也分享出来,希望有大神能指点~~
第一个,在有nginx的机器上运行:
<code><span>#!/bin/bash </span> setenforce <span>0</span> > /dev/null iptables -F > /dev/null sed -i <span>'s/^SELINUX=enforcing/SELINUX=disabled/g'</span> /etc/selinux/config <span>############### nginx + tomcat + memcached</span>DIR_NOW=`<span>pwd</span>` IPADDR_NTM=<span>'192.168.122.101'</span> IPADDR_TM=<span>'192.168.122.102'</span>NGINX_PKG_DIR=<span>'/root/one_key_install'</span> NGINX_PKG_NAME=<span>'nginx-1.6.1.tar.gz'</span> NGINX_DIR=<span>'/usr/local/nginx'</span>TOMCAT_PKG_DIR=<span>'/root/one_key_install'</span> TOMCAT_PKG_NAME=<span>'apache-tomcat-7.0.37.tar.gz'</span> TOMCAT_DIR=<span>'/usr/local/tomcat'</span>STICKY_PKG_DIR=<span>'/root/one_key_install'</span> STICKY_PKG_NAME=<span>'nginx-sticky-module-1.0.tar.gz'</span>JDK_BIN_DIR=<span>'/root/one_key_install'</span> JDK_BIN_NAME=<span>'jdk-6u32-linux-x64.bin'</span><span>################### nginx + sticky install</span><span>cd</span><span>$NGINX_PKG_DIR</span> tar zxf <span>$NGINX_PKG_NAME</span> DIR=`ls -F | grep /$ | grep nginx | awk -F <span>'/'</span><span>'{print $1}'</span>` tar zxf <span>$STICKY_PKG_DIR</span>/<span>$STICKY_PKG_NAME</span> -C <span>$NGINX_PKG_DIR</span>/<span>$DIR</span>sed -i <span>'s/^CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g'</span> /<span>$NGINX_PKG_DIR</span>/<span>$DIR</span>/auto/cc/gcc sed -i <span>'s/^#define NGINX_VER \"nginx\/\" NGINX_VERSION/#define NGINX_VER \"nginx\/\"/g'</span> /<span>$NGINX_PKG_DIR</span>/<span>$DIR</span>/src/core/nginx.h yum install gcc pcre-devel openssl-devel -y <span>cd</span><span>$NGINX_PKG_DIR</span>/<span>$DIR</span> ./configure --prefix=/usr/local/nginx --add-module=<span>$NGINX_PKG_DIR</span>/<span>$DIR</span>/nginx-sticky-module-<span>1.0</span> --with-http_ssl_module --with-http_stub_status_module make && make install ln <span>-s</span> /usr/local/nginx/sbin/nginx /usr/local/sbin/ useradd -M <span>-d</span> /usr/local/nginx/ <span>-s</span> /sbin/nologin nginx <span>cd</span><span>$DIR_NOW</span> rm <span>-f</span> /usr/local/nginx/conf/nginx.conf cp nginx.conf.exp /usr/local/nginx/conf/nginx.conf sed -i <span>"s/server 192.168.0.1:8080;/server <span>${IPADDR_NTM}</span>:8080;/g"</span> /usr/local/nginx/conf/nginx.conf sed -i <span>"s/server 192.168.0.2:8080;/server <span>${IPADDR_TM}</span>:8080;/g"</span> /usr/local/nginx/conf/nginx.conf <span>##################### tomcat install</span><span>cd</span><span>$JDK_BIN_DIR</span> sh <span>$JDK_BIN_NAME</span> DIR=`ls -F | grep /$ | grep jdk | awk -F <span>'/'</span><span>'{print $1}'</span>` mv <span>$DIR</span> /usr/local/ mv /usr/local/<span>$DIR</span> /usr/local/jdk <span>echo</span><span>"export JAVA_HOME=/usr/local/jdk export CLASSPATH=:\$JAVA_HOME/lib export PATH=\$PATH:\$JAVA_HOME/bin "</span>>>/etc/profile <span>cd</span><span>$TOMCAT_PKG_DIR</span> tar zxf <span>$TOMCAT_PKG_NAME</span> -C /usr/local <span>cd</span> /usr/local mv /usr/local/`ls | grep tomcat` /usr/local/tomcat ln <span>-s</span> /usr/local/tomcat/bin/startup.sh /usr/local/sbin/tomcat-start ln <span>-s</span> /usr/local/tomcat/bin/shutdown.sh /usr/local/sbin/tomcat-stop <span>cd</span><span>$DIR_NOW</span> tar zxf kryo_pkgs.tar.gz <span>cd</span> kryo_pkgs cp * /usr/local/tomcat/lib <span>cd</span><span>$DIR_NOW</span> rm <span>-f</span> /usr/local/tomcat/conf/context.xml cp context.xml.exp /usr/local/tomcat/conf/context.xml <span>cd</span><span>$DIR_NOW</span> rm <span>-f</span> /usr/local/tomcat/conf/context.xml cp context.xml.exp /usr/local/tomcat/conf/context.xml sed -i <span>"s#memcachedNodes=\"n1:192.168.0.1:11211,n2:192.168.0.2:11211\"#memcachedNodes=\"n1:<span>${IPADDR_NTM}</span>:11211,n2:<span>${IPADDR_TM}</span>:11211\"#"</span> /usr/local/tomcat/conf/context.xml <span>#################### memcached install</span>yum install memcached -y <span>#################### start services</span> /etc/init.d/memcached start <span>source</span> /etc/profile tomcat-start nginx</code>
第二个,在只有tomcat和memcached的机器上运行
<code><span>#!/bin/bash </span> setenforce <span>0</span> > /dev/null iptables -F > /dev/null sed -i <span>'s/^SELINUX=enforcing/SELINUX=disabled/g'</span> /etc/selinux/config DIR_NOW=`<span>pwd</span>` IPADDR_NTM=<span>'192.168.122.101'</span> IPADDR_TM=<span>'192.168.122.102'</span>TOMCAT_PKG_DIR=<span>'/root/no_ngx'</span> TOMCAT_PKG_NAME=<span>'apache-tomcat-7.0.37.tar.gz'</span> TOMCAT_DIR=<span>'/usr/local/tomcat'</span>STICKY_PKG_DIR=<span>'/root/no_ngx'</span> STICKY_PKG_NAME=<span>'nginx-sticky-module-1.0.tar.gz'</span>JDK_BIN_DIR=<span>'/root/no_ngx'</span> JDK_BIN_NAME=<span>'jdk-6u32-linux-x64.bin'</span><span>cd</span><span>$JDK_BIN_DIR</span> sh <span>$JDK_BIN_NAME</span>DIR=`ls -F | grep /$ | grep jdk | awk -F <span>'/'</span><span>'{print $1}'</span>` mv <span>$DIR</span> /usr/local/ mv /usr/local/<span>$DIR</span> /usr/local/jdk <span>echo</span><span>"export JAVA_HOME=/usr/local/jdk export CLASSPATH=:\$JAVA_HOME/lib export PATH=\$PATH:\$JAVA_HOME/bin "</span>>>/etc/profile <span>cd</span><span>$TOMCAT_PKG_DIR</span> tar zxf <span>$TOMCAT_PKG_NAME</span> -C /usr/local/ <span>cd</span> /usr/local mv /usr/local/`ls | grep tomcat` /usr/local/tomcat <span>cd</span><span>$DIR_NOW</span> tar zxf kryo_pkgs.tar.gz <span>cd</span> kryo_pkgs cp * /usr/local/tomcat/lib <span>cd</span><span>$DIR_NOW</span> rm <span>-f</span> /usr/local/tomcat/conf/context.xml cp context.xml.exp /usr/local/tomcat/conf/ mv /usr/local/tomcat/conf/context.xml.exp /usr/local/tomcat/conf/context.xml sed -i <span>"s#memcachedNodes=\"n1:192.168.0.1:11211,n2:192.168.0.2:11211\"#memcachedNodes=\"n1:<span>${IPADDR_NTM}</span>:11211,n2:<span>${IPADDR_TM}</span>:11211\"#"</span> /usr/local/tomcat/conf/context.xml sed -i <span>'s/failoverNodes="n1"/failoverNodes="n2"/'</span> /usr/local/tomcat/conf/context.xml yum install memcached -y /etc/init.d/memcached start <span>source</span> /etc/profile ln <span>-s</span> /usr/local/tomcat/bin/startup.sh /usr/local/sbin/tomcat-start ln <span>-s</span> /usr/local/tomcat/bin/shutdown.sh /usr/local/sbin/tomcat-stop tomcat-start</code>
这两个脚本存在同样的问题,执行完毕之后发现
<code><span>source</span> /etc/profile</code>
这一句没有执行,必须手动执行,一直百思不得其解,希望有谁能解答,多谢啦~~
The above introduces the nginx+tomcat+memcached configuration + script, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Springboot内置tomcat禁止不安全HTTP方法1、在tomcat的web.xml中可以配置如下内容让tomcat禁止不安全的HTTP方法/*PUTDELETEHEADOPTIONSTRACEBASIC2、Springboot使用内置tomcat没有web.xml配置文件,可以通过以下配置进行,简单来说就是要注入到Spring容器中@ConfigurationpublicclassTomcatConfig{@BeanpublicEmbeddedServletContainerFacto

springboot内置的tomcat支持最大的并发量SpringBoot内置Tomcat,在默认设置中,Tomcat的最大线程数是200,最大连接数是10000。支持的并发量是指连接数,也就是说支持最大并发量是一万(默认)Tomcat有两种处理连接的模式1,是BIO,一个线程只处理一个Socket连接,2,是NIO,一个线程处理多个Socket连接。由于HTTP请求不会太耗时,而且多个连接一般不会同时来消息,所以一个线程处理多个连接没有太大问题。一个线程处理多个连接数会很慢有可能会超时默认内嵌

nginx是一个高性能的http服务器/反向代理服务器及电子邮件(imap/pop3)代理服务器。其占有内存少,并发能力强,在同类型的网页服务器中表现较好。nginx可以在大多数unixlinuxos上编译运行,并有windows移植版。一般情况下,对于新建站点,建议使用最新稳定版作为生产版本。单个tomcat最大支持在线访问是500左右,要通知支持更多的访问量一个tomcat就没法做到了。在这里我们采用集群部署方式,使用多个tomcat,反向代理使用nginx。架构如下:准备工作apache-

1.Nginx和tomcat的区别nginx常用做静态内容服务和代理服务器,直接外来请求转发给后面的应用服务器(tomcat,Django等),tomcat更多用来做一个应用容器,让javawebapp泡在里面的东西。严格意义上来讲,Apache和nginx应该叫做HTTPServer,而tomcat是一个ApplicationServer是一个Servlet/JSO应用的容器。客户端通过HTTPServer访问服务器上存储的资源(HTML文件,图片文件等),HTTPServer是中只是把服务器

负载均衡的基本概念负载平衡(loadbalancing)是一种计算机网络技术,用来在多个计算机(计算机集群)、网络连接、cpu、磁盘驱动器或其他资源中分配负载,以达到最佳化资源使用、最大化吞吐率、最小化响应时间、同时避免过载的目的。使用带有负载平衡的多个服务器组件,取代单一的组件,可以通过冗余提高可靠性。负载平衡服务通常是由专用软体和硬件来完成。负载平衡最重要的一个应用是利用多台服务器提供单一服务,这种方案有时也称之为服务器农场。通常,负载平衡主要应用于web网站,大型的internetrela

首先,安装两个tomcat,可以是同一个复制成两个,也可以下载两个不同版本的tomcat,我就是下载了两个不同版本的。(这是8.0版本的,随便找两个不是特别老的版本的就行)。然后启动两个tomcat,在启动前,先更改其中一个的端口号,使得两个tomcat启动时不会端口冲突,一个是本身的8080端口,一个是改成了9080端口。配好以后,打开cmd命令窗口,我的tomcat一个放在d:\software\apache-tomcat-8.5.24目录下,按照如下命令即可启动,启动成功会弹出另一个窗口,

第一步:配置与系统一致的jdk环境变量(32位/64位)(1)官网下载jdk安装包:官网下载地址:根据自己的操作系统选择对应的安装包(一定要选择接受许可协议)(2)安装jdk:安装jdk选择安装目录安装过程中会出现两次安装提示。第一次是安装jdk,第二次是安装jre。建议两个都安装在同一个java文件夹中的不同文件夹中。(不能都安装在java文件夹的根目录下,jdk和jre安装在同一文件夹会出错)安装完成如下图所示(3)配置环境变量安装完jdk后配置环境变量计算机→属性→高级系统设置→高级→环境

发现并发容器问题对单接口进行6000线程压测,每个线程请求5次,线程在5秒内创建完毕,当进行一半的时候,已经出现了请求响应时间过大及其错误率达到了43%。这个并发容量对于配置比较好点的服务器相对来说有点弱。深入SpringBoot底层了解原因在SpringBoot官方文档中提到了关于元数据的配置可以看到,关于我们最常用的设置项目的端口的默认配置就在其中。默认内嵌Tomcat配置1、server.tomcat.accept-count:等待队列长度,当可分配的线程数全部用完之后,后续的请求将进入等


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
