nginx+jdk+tomcat+memcache配置

一、jdk的源码安装

1、解压源码,设定连接

[root@server6 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server6 ~]# cd /usr/local/
[root@server6 local]# ln -s jdk1.7.0_79/ java
[root@server6 local]# ll
total 48
drwxr-xr-x. 2 root root 4096 Jun 28  2011 bin
drwxr-xr-x. 2 root root 4096 Jun 28  2011 etc
drwxr-xr-x. 2 root root 4096 Jun 28  2011 games
drwxr-xr-x. 2 root root 4096 Jun 28  2011 include
lrwxrwxrwx  1 root root   12 Jul  5 15:33 java -> jdk1.7.0_79/
drwxr-xr-x  8 uucp  143 4096 Apr 11  2015 jdk1.7.0_79
drwxr-xr-x. 2 root root 4096 Jun 28  2011 lib
drwxr-xr-x. 2 root root 4096 Jun 28  2011 lib64
drwxr-xr-x. 2 root root 4096 Jun 28  2011 libexec
drwxr-xr-x  6 root root 4096 Jul  5 13:41 lnmp
drwxr-xr-x. 2 root root 4096 Jun 28  2011 sbin
drwxr-xr-x. 5 root root 4096 Jun 19 11:56 share
drwxr-xr-x. 2 root root 4096 Jun 28  2011 src

2、添加环境变量

[root@server6 local]# vim /etc/profile
 80 export  JAVA_HOME=/usr/local/java
 81 export  CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
 82 export  PATH=$PATH:$JAVA_HOME/bin
 [root@server6 local]# source /etc/profile   ##在当前bash更新环境变量
[root@server6 local]# echo $JAVA_HOME
/usr/local/java
[root@server6 local]# echo $CLASSPATH
.:/usr/local/java/lib:/usr/local/java/jre/lib
[root@server6 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin:/usr/local/java/bin

3、java测试

[

root@server6 ~]# vim test.java
[root@server6 ~]# cat test.java 
public class test {

        public  static  void main(String[] args){
                System.out.println("Hello World!");
        }

}

[root@server6 ~]# javac test.java    ##编译
[root@server6 ~]# java test
Hello World!

二、tomcat安装

1、解压源码,设定软链接

[root@server6 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server6 ~]# cd /usr/local/
[root@server6 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server6 local]# ll
total 52
drwxr-xr-x  9 root root 4096 Jul  5 15:41 apache-tomcat-7.0.37
drwxr-xr-x. 2 root root 4096 Jun 28  2011 bin
drwxr-xr-x. 2 root root 4096 Jun 28  2011 etc
drwxr-xr-x. 2 root root 4096 Jun 28  2011 games
drwxr-xr-x. 2 root root 4096 Jun 28  2011 include
lrwxrwxrwx  1 root root   12 Jul  5 15:33 java -> jdk1.7.0_79/
drwxr-xr-x  8 uucp  143 4096 Apr 11  2015 jdk1.7.0_79
drwxr-xr-x. 2 root root 4096 Jun 28  2011 lib
drwxr-xr-x. 2 root root 4096 Jun 28  2011 lib64
drwxr-xr-x. 2 root root 4096 Jun 28  2011 libexec
drwxr-xr-x  6 root root 4096 Jul  5 13:41 lnmp
drwxr-xr-x. 2 root root 4096 Jun 28  2011 sbin
drwxr-xr-x. 5 root root 4096 Jun 19 11:56 share
drwxr-xr-x. 2 root root 4096 Jun 28  2011 src
lrwxrwxrwx  1 root root   21 Jul  5 15:41 tomcat -> apache-tomcat-7.0.37/

2、启动tomcat,tomcat默认端口为8080

[root@server6 local]# cd tomcat/bin/
[root@server6 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
[root@server6 bin]# pwd
/usr/local/tomcat/bin

1、浏览器测试:
这里写图片描述
2、生成jsp文件测试

[root@server6 bin]# cd /usr/local/tomcat/webapps/ROOT/
[root@server6 ROOT]# vim test.jsp
[root@server6 ROOT]# cat test.jsp 
server6 time is: <%=new java.util.Date() %>

这里写图片描述

3、修改nginx配置文件,当访问jsp文件时,自动调转,

[root@server6 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf
 42         location / {
 43             #root   html;
 44             root    /usr/local/tomcat/webapps/ROOT;
 45             index  index.php index.html index.htm;
 46         }
 47 
 48         #error_page  404              /404.html;
 49 
 50         # redirect server error pages to the static page /50x.html
 51         #
 52         error_page   500 502 503 504  /50x.html;
 53         location = /50x.html {
 54             root   html;
 55         }
 56         
 57         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 58         #
 59         location ~ \.jsp$ {
 60             proxy_pass   http://172.25.9.6:8080;
 61         }

此时重启nginx浏览器访问
这里写图片描述
这里写图片描述

三、tomcat实现负载均衡

1、环境配置

server6:172.25.9.6
server7:172.25.9.7
server7安装jdk和tomcat,添加环境变量,编写一个显示时间的jsp文件
这里写图片描述

2、nginx配置

 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19         upstream tomcat {
 20             server 172.25.9.6:8080;
 21             server 172.25.9.7:8080;
 22         }

 65         location ~ \.jsp$ {
 66             proxy_pass   http://tomcat;
[root@server6 ~]# vim /usr/local/lnmp/nginx/conf/nginx.conf
[root@server6 ~]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server6 ~]# nginx -s reload

物理机测试:

[root@foundation9 images]# for i in {1..10}; do curl 172.25.9.6/test.jsp;done;
server7 time is: Thu Jul 05 16:32:06 CST 2018
server6 time is: Thu Jul 05 16:32:06 CST 2018
server7 time is: Thu Jul 05 16:32:06 CST 2018
server6 time is: Thu Jul 05 16:32:06 CST 2018
server7 time is: Thu Jul 05 16:32:06 CST 2018
server6 time is: Thu Jul 05 16:32:06 CST 2018
server7 time is: Thu Jul 05 16:32:06 CST 2018
server6 time is: Thu Jul 05 16:32:06 CST 2018
server7 time is: Thu Jul 05 16:32:06 CST 2018
server6 time is: Thu Jul 05 16:32:06 CST 2018

四、seccsion不同步的解决方式:

##修改 test.jsp (/usr/local/tomcat/webapps/ROOT)
##server1 和 server2 主机保持一致

[root@server2 ROOT]# cat 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);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>

##访问 http://172.25.12.1/test.jsp 时,每次提交
  session 都会刷新,由于不同步,资料无法存储

Server Info: 172.25.12.1 : 8080
Server Info: 172.25.12.2 : 8080

ID 383B38491A1706021CDB48FB682F6877
Session listuser2 = 456
name:
key: 

1、nginx轮询方式选择ip_hash

这里写图片描述

[root@server6 bin]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server6 bin]# nginx -s reload

访问 172.25.12.1/test.jsp,session可以保存
弊端:如果server1主机 tomcat shutdown,数据丢失
ip_hash 过不了 cdn 服务器;但是 cookie 可以

2、采用cookie

这里写图片描述

3、memcache缓存机制

[root@server6 lib]# pwd  ##下载模块
/usr/local/tomcat/lib

[root@server6 lib]# ls
annotations-api.jar asm-3.2.jar catalina-ant.jar
catalina-ha.jar     catalina.jar    catalina-tribes.jar
ecj-4.2.1.jar       el-api.jar  jasper-el.jar
jasper.jar      jsp-api.jar kryo-1.04.jar
kryo-serializers-0.10.jar   memcached-session-manager-1.6.3.jar
memcached-session-manager-tc6-1.6.3.jar memcached-session-manager-tc7-1.6.3.jar
minlog-1.2.jar      msm-kryo-serializer-1.6.3.jar   reflectasm-1.01.jar
servlet-api.jar     spymemcached-2.7.3.jar      tomcat-api.jar
tomcat-coyote.jar   tomcat-dbcp.jar         tomcat-i18n-es.jar
tomcat-i18n-fr.jar  tomcat-i18n-ja.jar  tomcat-jdbc.jar tomcat-util.jar

[root@server6 lib]# rm -fr memcached-session-manager-tc6-1.6.3.jar
[root@server6 lib]# yum install -y memcached
[root@server6 lib]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]

[root@server6 conf]# pwd
/usr/local/tomcat/conf
[root@server6 conf]# vim context.xml
 36 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
 37 memcachedNodes="n1:172.25.9.6:11211,n2:172.25.9.7:11211"
 38 failoverNodes="n2"    ##在server7中指向n1
 39 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
 40 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory    "
 41 />
 42 </Context>

[root@server6 tomcat]# cat logs/catalina.out 查看日志,如果出现此提示,表明配置成功
这里写图片描述
测试:

、访问 172.25.9.6/test.jsp 测试:

 Server Info: 172.25.9.6 : 8080

ID 51EAC4845CCD9734F0D21988CC4A1938-n2
Session listuser2 = 222
user1 = 111
user4 = 444
user3 = 333
user6 = 666
name:
key

##server6 主机 tomcat shutdown 之后,数据依然正常
##server6 主机 (安装 telnet )
[root@server6 tomcat]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get 51EAC4845CCD9734F0D21988CC4A1938-n2
VALUE 51EAC4845CCD9734F0D21988CC4A1938-n2 2048 149
Wdcj�dcj�01dcj�+dcj�=#51EAC4845CCD9734F0D21988CC4A1938-n2user6666user2222user1111user4444user3333
END

猜你喜欢

转载自blog.csdn.net/weixin_41789003/article/details/80930188
今日推荐