nginx+tomcat+memcached

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat+memcached相关的知识,希望对你有一定的参考价值。

[[email protected] ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# ln -s jdk1.7.0_79/ java
[[email protected] local]# ls
[[email protected] local]# vim /etc/profile
技术分享图片
[[email protected] local]# source /etc/profile
[[email protected] ~]# vim test.java
技术分享图片
[[email protected] ~]# javac test.java
[[email protected] ~]# java test
Hello World
server2和server1同样
部署tomcat
[[email protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# ls
apache-tomcat-7.0.37 etc include jdk1.7.0_79 lib64 sbin src
bin games java lib libexec share
[[email protected] local]# ln -s apache-tomcat-7.0.37/ tomcat
[[email protected] local]# ls
apache-tomcat-7.0.37 etc include jdk1.7.0_79 lib64 sbin src
bin games java lib libexec share tomc
[[email protected] local]# cd tomcat/
[[email protected] tomcat]# bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[[email protected] tomcat]# netstat -antlp
技术分享图片
技术分享图片
[[email protected] tomcat]# cd webapps/ROOT/
[[email protected] ROOT]# vim test.jsp
The time is: <%=new java.util.Date() %>
server2同上
server3安装nginx
[[email protected] ~]# tar zxf nginx-1.10.1.tar.gz
[[email protected] ~]# tar zxf nginx-sticky-module-ng.tar.gz
[[email protected] ~]# cd nginx-1.10.1
[[email protected] nginx-1.10.1]# vim auto/cc/gcc #注释掉
技术分享图片
[[email protected] nginx-1.10.1]# yum install gcc pcre-devel openssl-devel -y 安装依赖

[[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-threads --user=nginx --group=nginx --add-module=/root/nginx-sticky-module-ng
[[email protected] nginx-1.10.1]# make && make install 编译安装
[[email protected] nginx-1.10.1]# cd /usr/sbin/
[[email protected] sbin]# nginx
[[email protected] sbin]# cd /usr/local/lnmp/nginx/conf/
[[email protected] conf]# vim nginx.conf
[[email protected] conf]# pwd
/usr/local/lnmp/nginx/conf

[[email protected] conf]# vim nginx.conf

17 http {
18 upstream tomcat { #负载均衡模块
19 sticky;
20 server 192.168.122.12:8080; #后端tomcat
21 server 192.168.122.13:8080;
22 }
23
24 include mime.types;
25 default_type application/octet-stream;

68 location ~ .jsp$ { #所有jsp页面交给tomcat处理,动静分离
69 proxy_pass http://tomcat;
70 }

[[email protected] logs]# nginx -t
测试:
[[email protected] conf]# curl 172.25.135.1:8080/test.jsp
[[email protected] conf]# curl 172.25.135.2:8080/test.jsp

配置session
server1,2上
把如下软件包放置到/usr/local/tomcat/lib 目录中
kryo-1.03.jar
kryo-serializers-0.8.jar
memcached-2.5.jar
memcached-session-manager-1.5.1.jarmemcached-session-manager-tc7-1.5.1.jar
minlog-1.2.jar
msm-kryo-serializer-1.5.1.jar
reflectasm-0.9.jar
[[email protected] ROOT]# cd /usr/local/tomcat/lib/
[[email protected] lib]# ls
annotations-api.jar jsp-api.jar spymemcached-2.7.3.jar
asm-3.2.jar kryo-1.04.jar tomcat-api.jar
catalina-ant.jar kryo-serializers-0.10.jar tomcat-coyote.jar
catalina-ha.jar memcached-session-manager-1.6.3.jar tomcat-dbcp.jar
catalina.jar memcached-session-manager-tc6-1.6.3.jar tomcat-i18n-es.jar
catalina-tribes.jar memcached-session-manager-tc7-1.6.3.jar tomcat-i18n-fr.jar
ecj-4.2.1.jar minlog-1.2.jar tomcat-i18n-ja.jar
el-api.jar msm-kryo-serializer-1.6.3.jar tomcat-jdbc.jar
jasper-el.jar reflectasm-1.01.jar tomcat-util.jar
jasper.jar servlet-api.jar
[[email protected] lib]# cd ..
[[email protected] tomcat]# cd conf/
[[email protected] conf]# pwd
/usr/local/tomcat/conf

[[email protected] conf]# vim context.xml

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<Context>

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.135.1:11211,n2:172.25.135.2:11211"

failoverNodes="n1" 在 node2(server3) 上此项设置为“n2 (坏了找自己)

requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>

[[email protected] conf]# yum install -y memcached 安装
[[email protected] conf]# /etc/init.d/memcached start
[[email protected] conf]# vim /usr/local/tomcat/webapps/ROOT/test.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>

server2相同配置

以上是关于nginx+tomcat+memcached的主要内容,如果未能解决你的问题,请参考以下文章

整合Nginx和Tomcat

Nginx + Tomcat

nginx + tomcat配置负载均衡

搭建Nginx+Tomcat 负载均衡

nginx怎么知道哪台tomcat挂了?

Nginx+Tomcat简单集群