实验一:
负载均衡
- export JAVA_HOME=/usr/local/jdk
- export CLASSPATH=:$JAVA_HOME/lib
- export PATH=$PATH:$JAVA_HOME/bin
- user www www;
- worker_processes 4;
- error_log logs/error.log;
- events {
- use epoll;
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 65;
- gzip on;
- server {
- listen 80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- location ~ \.php$ {
- root html;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fastcgi.conf;
- }
- location /status {
- stub_status on;
- access_log off;
- }
- location ~\.jsp$
- {
- proxy_pass http://192.168.0.20:8080; //所有的jsp页面交给tomcat处理,动静分离,这样你访问的时候就不用亲自去输入8080端口了,因为nginx是访问的静态的,tomcat访问的是动态的页面。
- }
- }
- }
//所有的jsp页面交给tomcat处理,动静分离,这样你访问的时候就不用亲自去输入8080端口了,因为nginx是访问的静态的,tomcat访问的是动态的页面。
此时,进行测试:注意两个主机上的文件应该是一致的。都得有test.jsp
- upstream tomcat {
- server 192.168.0.9:8080;
- server 192.168.0.85:8080;
- }(此处应该写在http下面并且在proxy_pass的上面的!)
- location ~\.jsp$ {
- proxy_pass http://tomcat;
- }//(此处应该在上面,在location的位置。否则报错的)
访问网页进行测试: http://192.168.0.20/test.jsp
- upstream tomcat
- { sticky;
- server 192.168.0.20:8080;
- server 192.168.0.80:8080;
- }
- location ~\.jsp$
- {
- proxy_pass http://tomcat;
- }//(此处应该在上面,在location的位置。否则报错的)
- vi /usr/local/tomcat/conf/context.xml
- <Context>
- ......
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.0.20:11211,n2:192.168.0.80:11211"
- failoverNodes="n1"
- #在 node2 上此项设置为“n2”
- requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
- />
- </Context>
- vi /usr/local/tomcat/conf/context.xml
- <Context>
- ......
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.0.20:11211,n2:192.168.0.80:11211"
- failoverNodes="n2"
- #在 node2 上此项设置为“n2”
- requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
- />
- </Context>
分别在两台机子上启动tomcat
- <%@ 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://192.168.0.20/test.jsp,不同的主机访问时会调度到不同的 tomcat 实例上处理来自同一主机的请求会交给同一个 tomcat 实例处理,此时你 down 掉当前正在响应的 tomcat 实
本文出自 “资料小结” 博客,谢绝转载!