Nginx+tomcat+memcache

Source: Internet
Author: User

Nginx+tomcat+memcache

Nginx support Static page Tomcat support Dynamic page if the Nginx server you want to make a dynamic page, you need to combine with Tomcat at this time Nginx is just a forwarding role of the JSP processing is handled by Tomcat.

Build a JDK

Build the Java Environment first

1 tar zxf jdk-7u79-linux-x64.tar.gz-c/usr/local/# # #解压到/usr/local down # # #

2 ln-s Jdk1.7.0_79/java # # #软链接方便版本升级后的使用 # # #

3 Vim/etc/profile ###/etc/profile is a global definition all users can use/.bash_profile is locally defined only when a user starts to load # # #

Content

Export Java_home=/usr/local/java # # # #使用变量 $JAVA _home If the path changes the changes will be convenient # # #

Export classpath=.: $JAVA _home/lib: $JAVA _home/jre/lib # # # #该环境变量是当我们再写java程序时要应用别人写好的类时要让java解释器知道到哪里去找这个类 # #

Export path= $PATH: $JAVA _home/bin # # #系统路径一定要先写不然系统的一些命令可能会找不到不过可以用绝对路径去执行系统和命令, set the PATH to JAVA application e run will be more convenient, such as Javacja Vajavah et cetera # #

4 Source/etc/profile # # #使文件生效 # # #




Test whether to build well

[[Email protected] ~] #vim Test.java

Content

public class Test {
public static void Main (string[] arge) {
System.out.println ("Hello world!");


}
}

[Email protected] ~]# Javac Test.java
[[Email protected] ~]# java test
Hello world!

The specific process is as follows
[Email protected] ~]# tar zxf jdk-7u79-linux-x64.tar.gz-c/usr/local/
[Email protected] ~]# cd/usr/local/
[[email protected] local]# ls
Bin etc games include jdk1.7.0_79 Lib lib64 libexec sbin share src
[Email protected] local]# ln-s Jdk1.7.0_79/java
[email protected] local]# LL
Total 44
Drwxr-xr-x. 2 root root 4096 June
Drwxr-xr-x. 2 root root 4096 June (etc)
Drwxr-xr-x. 2 root root 4096 June
Drwxr-xr-x. 2 root root 4096 June
lrwxrwxrwx 1 root root from 10:00am-jdk1.7.0_79/
Drwxr-xr-x 8 UUCP 143 4096 APR jdk1.7.0_79
Drwxr-xr-x. 2 root root 4096 June
Drwxr-xr-x. 2 root root 4096 June lib64
Drwxr-xr-x. 2 root root 4096 June Libexec
Drwxr-xr-x. 2 root root 4096 June Sbin
Drwxr-xr-x. 5 root root 4096 Jul 11:24 share
Drwxr-xr-x. 2 root root 4096 June (SRC)
[Email protected] local]# Vim/etc/profile
[Email protected] local]# Source/etc/profile
[Email protected] local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin
[[Email protected] local]# CD
[Email protected] ~]# vim Test.java
[Email protected] ~]# Javac Test.java
[[Email protected] ~]# java test
Hello world!




Two build Tomcat

Install package download can go to http//tomcat.apache.org/download
The main is the processing of dynamic pages jsp
Default Publishing Directory/usr/local/tomcat/webapps/root
Default Publishing Page index.jsp

1 tar zxf apache-tomcat-7.0.37.tar.gz-c/usr/local/# # #解压到/usr/local/###

2 ln-s Apache-tomcat-7.0.37/tomcat # # #软链接方便版本升级 # # #

3/usr/local/tomcat/bin/startup.sh/# # #开启服务 # # #

4 NETSTAT-ANTLP # # #查看端口为8080


Test

[Email protected] root]# pwd
/usr/local/tomcat/webapps/root
[Email protected] root]# vim test.jsp

Content

The time Is:<%=new java.util.Date ()%>

172.25.78.1:8080/test.jsp





Three to integrate Nginx and Tomcat together


1 vim/usr/local/lnmp/nginx/conf/nginx.conf

Content

Location ~ \.jsp$ {
Proxy_pass http://172.25.38.2:8080;
}


2 Nginx-s Reload

Test
Visit 172.25.38.1/test.jsp



# # # #作负载均衡 # #

JSP pages consume resources to do a load balancer

1 vim/usr/local/lnmp/nginx/conf/nginx.conf

Content
Upstream westos{
Server 172.25.38.2:8080;
Server 172.25.38.3:8080;
Server 127.0.0.1:8000 backup;
}

Location ~ \.jsp$ {
Proxy_pass Http://westos;
}

2 Nginx-s Reload


# # # #ip_hash # #

When you do not specify an algorithm for load balancing, the default is that polling will cause the user to submit the request to another tomcat and then submit the request again. If it's a shopping cart, then the user adds a thing and it will disappear. So it's unreasonable to use IP_. Hash will not jump to other tomcat as long as the request from the same IP is made

1 vim/usr/local/lnmp/nginx/conf/nginx.conf

Content:
Upstream westos{
Ip_hash;
Server 172.25.38.2:8080;
Server 172.25.38.3:8080;
}

Location ~ \.jsp$ {
Proxy_pass Http://westos;
}


2 Nginx-s Reload

Tomcat's Publishing page

vim/usr/local/tomcat/webapps/root/test.jsp

Content
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.util.*"%>
<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>







# # # #tomcat +memcache###

Use the Session object to store the information that is required for a specific user session so that the variables stored in the session object will not be lost when the user jumps between the application's Web pages, but persist throughout the user session
With IP_ Although the hash resolves the user request can be saved on another tomcat but if the Tomcat fails, it will cause the session to be lost, so add an additional backup storage memcache so that Tomcat fails and can go to memcache to get the information.

Tomcat-1 (T1) stores the session on Memcached-2 (T2). T1 stores the session on Memcached-1 only if M2 is not available (M1 is T1 failovernode). The advantage of using this configuration is that when both T1 and M1 crash, the session sessions are not lost and the single point of failure is avoided.
Server2

1 Yum install-y memcached

2 Put Memcached-session-manager-tc7-1.6.3.jar and so on/usr/local/tomcat/lib

3 Vim/usr/local/tomcat/conf/context.xml

<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:172.25.78.2:11211,n2:172.25.78.3:11211"
Failovernodes= "N1" # # #当n2出现故障时找n1节点 # # #
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>

4/usr/local/tomcat/bin/shutdown.sh # # #关闭tomcat如果没有开启tomcat则忽略这步 # # #

5/usr/local/tomcat/bin/startup.sh # # #开启tomcat # # #

6 tail-f/usr/local/tomcat/logs/catalina.out # # #监控日志信息查看是否出错 # # #





Server3

1 Yum install-y memcached


2 Put Memcached-session-manager-tc7-1.6.3.jar and so on/usr/local/tomcat/lib

3 Vim/usr/local/tomcat/conf/context.xml
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:172.25.78.2:11211,n2:172.25.78.3:11211"
Failovernodes= "N2" # # #当n1出现故障时找n2节点 # # #
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>

4/usr/local/tomcat/bin/shutdown.sh # # #关闭tomcat如果没有开启tomcat则忽略这步 # # #

5/usr/local/tomcat/bin/startup.sh # # #开启tomcat # # #

6 tail-f/usr/local/tomcat/logs/catalina.out # # #监控日志信息查看是否出错 # # #



Test
Visit 172.25.78.1/test.jsp

Tge contenttype= "text/html; CHARSET=GBK "%> Server info:172.25.78.2:8080

ID 3281d8620d9c032d509f68999319e338-n2
Session Listuser2 = 123
User1 = 123
User5 = 789
Name
Key
He time Is:mon Jul 03:46:41 CST 2017


[[Email protected] tomcat]# telnet localhost 11211
Trying:: 1 ...
Connected to localhost.
Escape character is ' ^] '.
Get 3281D8620D9C032D509F68999319E338-N2
VALUE 3281D8620D9C032D509F68999319E338-N2 2048 125
w]p~j]p01]p]p#3281d8620d9c032d509f68999319e338-n2user2123user1123user5789
END

# # #如果将server2的tomcat关闭 # # #

Still visit 172.25.78.1/test.jsp will find that the original session was not lost because it was saved in the cache
Tge contenttype= "text/html; CHARSET=GBK "%> Server info:172.25.78.3:8080

ID 3281d8620d9c032d509f68999319e338-n2
Session Listuser2 = 123
User1 = 123
User4 = 345
User5 = 789
Name
Key
He time Is:mon Jul 03:52:52 CST 2017

Get 3281D8620D9C032D509F68999319E338-N2
VALUE 3281D8620D9C032D509F68999319E338-N2 2048 137
w]p~j]qt01]qt]qt#3281d8620d9c032d509f68999319e338-n2user2123user1123user4345user5789
END





# # # #nginx算法sticky # #

Client requests---> CDN----> Nginx---> Server if the Nginx load balancing algorithm uses IP_ Hash so after the CDN after the IP has become the CDN IP then all the CDN customer requests to the same back-end server so that only one back-end server corresponding request is unreasonable, but using the stick algorithm is based on the cookie load Balancing cookie is the server Browser's cookie is different server can be processed according to the cookie

The original compiled nginx1.12 does not have a sticky module and the existing module nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz does not match the 1.12 version, so recompile the 1.10 version of Nginx

1 tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

2./configure--prefix=/opt/nginx/--user=nginx--group=nginx--with-http_stub_status_module--with-http_ssl_module- -with-threads--with-file-aio--add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d

3 Make && make install

4 vim/opt/nginx/cong/ngin.conf


Upstream Westos {
Sticky
Server 172.25.38.2:8080;
Server 172.25.38.3:8080;

}


Location ~ \.jsp$ {
Proxy_pass Http://westos;
}


5/opt/nginx/sbin/nginx-s Reload

Nginx+tomcat+memcache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.