博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx+Tomcat+Memcached部署
阅读量:6976 次
发布时间:2019-06-27

本文共 6592 字,大约阅读时间需要 21 分钟。

环境清单列表:(因为只有三台电脑,所有把Nginx和memcached放到一起)

  应用服务器1:192.168.51.10;

  应用服务器2:192.168.55.110;

  memcached服务器:192.168.51.75;

  Nginx地址:192.168.51.75

 

一、安装tomcate

  1、下载tomcate7.0,下载地址:链接: https://pan.baidu.com/s/1dxiE9w 密码: nba7,把压缩包放在/home/ump/tomcat目录下。

  先按照55.110上的tomcate

  2、解压压缩包到/usr/local目录下,并进入到安装目录,命令:

unzip apache-tomcat-7.0.77-windows-x64.zip -d /usr/local cd /usr/local/apache-tomcat-7.0.77

  3、修改conf/server.xml文件,因为192.168.55.110上面已经装有一个应用服务器,所以需要修改对应的端口,修改server端口,修改连接器的端口,修改后的内容如下:

 

  4、启动tomcate,访问:http://192.168.55.110:9090,如果出现tomcate管理界面,说明tamcate安装成功。

 

  5、用同样的方法,部署另一个tomcate到192.168.51.10,因为192.168.51.10上不存在端口冲突,所以server.xml不需要修改,解压后,启动,可以通过:http://192.168.51.10:8080/访问到tomcate管理控制台。

二、创建一个动态web工程,并部署到tomcate上

  1、eclipse创建一个动态web工程,并新建一个index.jsp,内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Cluster Web Test

sessionID:<%=session.getId() %>

长度:<%=session.getId().length() %>

    

this is Web 1

 

  2、把创建的动态工程导出为war包。下载地址:链接: https://pan.baidu.com/s/1c2Waok4 密码: 2jaf

  3、创建存放工程的目录,命令:

mkdir /home/ump/www

 

  4、把ClusterProject.war解压到ClusterProject文件夹,把ClusterProject文件夹放入道/home/ump/www目录下,修改server.xml,添加ClusterProject工程,内容如下:

 

主要是添加绿色背景的语句。

 

 

5、重启Tomcat,并访问:http://192.168.55.110:9090/ClusterProject/index.jsp,如果出现下图,说明应用安装成功。

 

  4、把ClusterProject.war安装同样的方法,部署到192.168.51.10上。为了与192.168.55.110上的应用区分,修改index.jsp中

 

this is Web 1

this is Web 2

  5、访问:http://192.168.51.10:8080/ClusterProject/index.jsp,如下图所示

 

至此,应用部署完毕。

 

三、安装Nginx,并配置负载均衡。(192.168.51.75)

  1、Nginx的安装过程参考其他文章,不在赘述。

  2、修改Nginx的配置文件,/etc/nginx/nginx.conf,修改内容如下:

# For more information on configuration, see:#   * Official English Documentation: http://nginx.org/en/docs/#   * Official Russian Documentation: http://nginx.org/ru/docs/user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log;#error_log  /var/log/nginx/error.log  notice;#error_log  /var/log/nginx/error.log  info;pid        /run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    index   index.html index.htm;    # Load modular configuration files from the /etc/nginx/conf.d directory.    # See http://nginx.org/en/docs/ngx_core_module.html#include    # for more information.    include /etc/nginx/conf.d/*.conf;    upstream myweb{        server 192.168.51.10:8080;        server 192.168.55.110:9090;    }    server {        listen       80 default_server;        server_name  localhost;        root         /usr/share/nginx/html;        #charset koi8-r;        #access_log  /var/log/nginx/host.access.log  main;        # Load configuration files for the default server block.        include /etc/nginx/default.d/*.conf;        location / {        proxy_pass http://myweb;        }        # redirect server error pages to the static page /40x.html        #        error_page  404              /404.html;        location = /40x.html {        }        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {        }    }}

 

只需要添加红色字体部分。

  3、访问Nginx:http://192.168.51.75/ClusterProject/index.jsp,第一次访问显示图一,刷新显示图二,说明第一次访问192.168.51.10,第二次访问192.168.55.110

 

 

至此负载均衡搭建完毕。

 

四、安装memcached,安装过程参考其他文档,不在赘述。

  memcached配置信息如下:

当前项目中,linux下memcached的启动/结束的方式
默认情况下memcached安装到/usr/local/bin下。
进入安装目录,启动memcached:/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -u root
获取运行状态:echo stats | nc localhost 11211(可以查看出pid) 或使用ps -ef|grep memcached 
停止memcached:kill -9 pid  (-9表示强制杀死,pid 为进程的进程标识符)
-d 选项是启动一个守护进程,
-m 是分配给Memcache使用的内存数量,单位是MB,这里是1024MB,默认是64MB
-u 是运行Memcache的用户,这里是root
-l 是监听的服务器IP地址,默认应该是本机
-p 是设置Memcache监听的端口,默认是11211,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024,这里设置了10240,按照你服务器的负载量来设定
-P 是设置保存Memcache的pid文件位置
-h 打印帮助信息
-v 输出警告和错误信息
-vv 打印客户端的请求和返回信息

 

五、基于memcached配置tomcate,共享session

  1、启动memcached,memcached -d -c 10240 -m 1024 -u root

  2、下载memcached相关jar包和序列化对应的jar包,这里session序列化采用kryo,所有的jar包下载地址:链接: https://pan.baidu.com/s/1htV2vnm 密码: hkiy

  3、把下载的所有jar,放入到tomcate安装目录的lib下面。

  4、修改tomcate,server.xml,添加session管理。

 

  5、同样,修改192.168.51.10上server.xml添加该session存储到memcached中。重启tomcate1和tomcate2。

  6、访问http://192.168.51.75/ClusterProject/index.jsp,并刷新界面,如下图所示

 

可以看到this is Web1变成this is web2,说明刷新访问了tomcate1和tomcate2,但是上面的sessionId没有变化,说明,实现了session共享。

至此,tomcate+Nginx+memcached,搭建的session共享集群完毕。

 

转载地址:http://fiupl.baihongyu.com/

你可能感兴趣的文章
编写自己的SpringBoot-starter
查看>>
MySql 语句优化
查看>>
MySQL的基础
查看>>
Spring Boot(十一)Redis集成从Docker安装到分布式Session共享
查看>>
读Zepto源码之操作DOM
查看>>
切版网上线,启用qieban.cn
查看>>
横向ListView(一) ——开篇,基础逻辑实现
查看>>
STM32单片机外部中断配置讲解
查看>>
阿里云智能对话分析服务
查看>>
中文详解phpmailer所有对象和属性
查看>>
python 函数
查看>>
什么是类型别名?什么是潜在类型?
查看>>
Condition
查看>>
10.15 iptables filter表案例
查看>>
Nginx防盗链,Nginx访问控制, Nginx解析php相关配置, Nginx代理
查看>>
芝麻HTTP:Scrapy-Splash的安装
查看>>
专访小邪:从十年技术之路看阿里技术体系的变革
查看>>
python 100例(10)
查看>>
测试服务命名和动态注册路由的方式@Xan
查看>>
如何处理用代码创建SD Sales order时遇到的错误消息KI 180
查看>>