linux|nginx+tomcat+memcache的负载均衡

  • 【linux|nginx+tomcat+memcache的负载均衡】实验环境:
    server1:172.25.55.1
    server2:172.25.55.2
  • 解压安装包(jdk、tomcat)
    [root@server1 sbin]# cd [root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local [root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local

  • 添加环境变量
    [root@server1 ~]# cd /usr/local [root@server1 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@server1 local]# source /etc/profile

  • 创建软链接
    [root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat [root@server1 local]# ln -s jdk1.7.0_79/ java

  • 编辑一个java程序试运行:
    [root@server1 ~]# vim test.java public class test { public static void main(String[] args) { System.out.println("Hello World!"); } } [root@server1 ~]# javac test.java(编译) [root@server1 ~]# java test(执行) Hello World!

  • 打开tomcat:
    [root@server1 ~]# cd /usr/local/tomcat/ [root@server1 tomcat]# cd bin/ [root@server1 bin]# ./startup.sh 打开 [root@server1 bin]# netstat -tnlp(查看端口号) tcp00 :::8080:::*LISTEN4344/java

  • 编辑nginx配置文件:
    [root@server1 conf]# pwd /usr/local/openresty/nginx/conf [root@server1 conf]# vim nginx.conf 71location ~ \.jsp$ { 72proxy_passhttp://127.0.0.1:8080; 73} [root@server1 conf]# cd ../sbin [root@server1 sbin]# ./nginx -s reload

  • 编辑网页
    [root@server1 html]# pwd /usr/local/openresty/nginx/html [root@server1 html]# ls 50x.htmlexample.phpindex.htmlindex.php [root@server1 html]# vim index.jsp the time is:

  • 访问页面http://172.25.28.1/index.jsp
    linux|nginx+tomcat+memcache的负载均衡
    文章图片
  • server2安装步骤与server1相同
    [root@server2 ~]# scp -r root@server1:/usr/local/java /usr/local/ [root@server2 ~]# scp -r root@server1:/usr/local/tomcat /usr/local/[root@server2 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@server2 local]# source /etc/profile

  • server1编辑配置文件,使其可以负载均衡(此实验用的Nginx是上篇博客openresty自带的Nginx,所以编辑文件在openresty目录下)
    [root@server1 conf]# vim /usr/local/openresty/nginx/conf/nginx.conf 17 http { 18upstream tomcat { 19server 172.25.55.1:8080; 20server 172.25.55.2:8080; 21}53#location /memc { 54#internal; 55#memc_connect_timeout 100ms; 56#memc_send_timeout 100ms; 57#memc_read_timeout 100ms; 58#set $memc_key $query_string; 59#set $memc_exptime 300; 60#memc_pass memcache; 61#}71location ~ \.jsp$ { 72proxy_passhttp://tomcat; 73}77location ~ \.php$ { 78#set $key $uri$args; 79#srcache_fetch GET /memc $key; 80#srcache_store PUT /memc $key; 81roothtml; 82fastcgi_pass127.0.0.1:9000; 83fastcgi_indexindex.php; 84#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; 85includefastcgi.conf; [root@server1 conf]# cd /usr/local/openresty/nginx/sbin/ [root@server1 sbin]# ./nginx -t(语法检测) [root@server1 sbin]# ./nginx -s reload(重启动服务)

    访问http://172.25.28.1:8080/
    linux|nginx+tomcat+memcache的负载均衡
    文章图片

  • 编辑负载均衡的网页
server1:
[root@server1 sbin]# cd /usr/local/tomcat/webapps/ROOT/ [root@server1 ROOT]# vim test.lsp the time is:

server2:
[root@server2 tomcat]# cd /usr/local/tomcat/webapps/ROOT [root@server2 ROOT]# vim test.jsp server2 the time is: [root@server2 bin]# ./startup.sh [root@server2 bin]# netstat -tnlp

  • 测试:访问网页,网页论询:
    linux|nginx+tomcat+memcache的负载均衡
    文章图片

    linux|nginx+tomcat+memcache的负载均衡
    文章图片

    推荐阅读