一个很好的例子: nginx+tomcat+memcached负载均衡集群搭建详细笔记:上

原文地址: http://www.open-open.com/lib/view/open1435117095903.html

忙了三天,终于把nginx+tomcat+memcached负载均衡集群搭建成功,真的非常兴奋,在此和大家一起分享一下我的成果,希望对大家今后的学习有所帮助,哈哈!首先我强调的是我用的操作系统是CentOS6.4,而且开的是一个VMWARE,其实也没什么影响,但是在Ubuntu下搭建的话,就可能有些许不一样,在centos下我们需要手动去下载一些额外的资源文件。好了,说了这么多,咱们开始吧

    前言:本文档主要讲解,如何在CentOS6.4下搭建Nginx+Tomcat+Memcached负载均衡集群服务器。

                (1)Nginx负责负载均衡。

                (2)Tomcat负责实际服务。

                (3)Memcached负责同步Tomcat的Session,达到Session共享的目的。

     1、首先我们来安装一下JDK吧,这不是必须要放在第一步,只是我自己之前在学习hadoop时就已经安装了JDK。

          我使用的JDK版本:jdk-6u24-linux-i586.bin(下载地址:JDK下载

         1.1将下载好的JDK放进 local/usr/文件路径下(我所有下载的.tar.gz和.bin文件都是放在该路径下的,以便于以后查找),如果你用的是VMWARE,你可以使用WinSCP来将要传送的文件复制到该路径下即可。

         1.2然后使用linux命令解压

tar -zvxf jdk-6u24-linux-i586.bin


          1.3安装完成之后,我将其重命名为jdk,只是这样看着比较舒服

    mv jdk-6u24-linux-i586 jdk


         1.4接着我们编辑/etc/profile,输入linux命令

gedit /etc/profile


         1.5编辑profile文件,添加以下文本

export JAVA_HOME=/usr/local/jdk  
   export PATH=$PATH:$JAVA_HOME/bin


         1.6验证jdk是否安装成功

java -version



      2、安装nginx


         Nginx官网:http://nginx.org/ 

        在安装Nginx之前,需要先安装gcc、 openssl、 pcre和zlib软件库,这些貌似在Ubuntu下有些就不需要安装的,根据你自己的需要进行安装。

        首先我们来谈一下如果缺少这些问题会报哪些错误以及解决方案:

        1)缺少pcre library
             ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. 

        当执行./configure的时候会抛出说缺少PCRE library 这个是HTTP Rewrite 模块,也即是url静态化的包


所以首先我们要安装这个东西  PCRE下载PCRE ,我选择的是pcre-8.36.tar.gz版本

# tar zxvf pcre-8.36.tar.gz
# ./configure  
# make  
# make install 


        2)缺少gcc-c++,也就是c++编译包 

             libtool: compile: unrecognized option `-DHAVE_CONFIG_H' 
libtool: compile: Try `libtool --help' for more information. 
make[1]: *** [pcrecpp.lo] Error 1 
make[1]: Leaving directory `/usr/local/src//pcre-8.31' 
make: *** [all] Error 2root@wolfdog-virtual-machine:~/work/pcre-8.12$ libtool -help -DHAVE_CONFIG_H 
The program 'libtool' is currently not installed. You can install it by typing: 
sudo apt-get install libtool 

  在线安装:输入命令:

yum install libtool 


                   再输入:

yum install gcc-c++ 


        3)缺少openssl库

          ./configure: error: the HTTP cache module requires md5 functions 
from OpenSSL library. You can either disable the module by using 
--without-http-cache option, or install the OpenSSL library into the system, 
or build the OpenSSL library statically from the source with nginx by using 
--with-http_ssl_module --with-openssl=<path> options. 

          openssl官网:openssl下载

          在线安装: 

yum install openssl libssl-dev libperl-dev


        4)缺少zlib库

          ./configure: error: the HTTP gzip module requires the zlib library. 
You can either disable the module by using --without-http_gzip_module 
option, or install the zlib library into the system, or build the zlib library 
statically from the source with nginx by using --with-zlib=<path> option. 

       zlib官网:下载zlib

       在线安装:


yum install zlib
yum install zlib-devel


         2.1接着安装nginx,版本为:nginx-1.7.9.tar.gz

# ./configure  
    # make  
    # sudo make install


       1)解压

 tar -zxvf nginx-1.7.9.tar.gz
          cd nginx-1.7.9


2)安装
# ./configure  
# make  
# make install 

         2.2安装完之后我们就可以在nginx的conf文件夹下面创建一个proxy.conf文件,配置一些代理参数

$cd /usr/local/nginx/conf
$sudo touch proxy.conf

#!nginx (-) 
# proxy.conf 
proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;  #获取真实ip
#proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;



        2.3接着,我们要编辑nginx.conf文件

gedit nginx.conf


         写上基本配置:

#运行nginx所在的用户名和用户组  
#user  www www;   
    
#启动进程数  
worker_processes 8;  
#全局错误日志及PID文件  
error_log  /usr/local/nginx/logs/nginx_error.log  crit;  
    
pid        /usr/local/nginx/nginx.pid;  
    
#Specifies the value for maximum file descriptors that can be opened by this process.  
    
worker_rlimit_nofile 65535;  
#工作模式及连接数上限  
events  
{  
  use epoll;  
  worker_connections 65535;  
}  
#设定http服务器,利用它的反向代理功能提供负载均衡支持  
http  
{  
  #设定mime类型  
  include       mime.types;  
  default_type  application/octet-stream;  
  include /usr/local/nginx/conf/proxy.conf;  
  #charset  gb2312;  
  #设定请求缓冲      
  server_names_hash_bucket_size 128;  
  client_header_buffer_size 32k;  
  large_client_header_buffers 4 32k;  
  client_max_body_size 8m;  
          
  sendfile on;  
  tcp_nopush     on;  
    
  keepalive_timeout 60;  
    
  tcp_nodelay on;  
    
#  fastcgi_connect_timeout 300;  
#  fastcgi_send_timeout 300;  
#  fastcgi_read_timeout 300;  
#  fastcgi_buffer_size 64k;  
#  fastcgi_buffers 4 64k;  
#  fastcgi_busy_buffers_size 128k;  
#  fastcgi_temp_file_write_size 128k;  
    
#  gzip on;  
#  gzip_min_length  1k;  
#  gzip_buffers     4 16k;  
#  gzip_http_version 1.0;  
#  gzip_comp_level 2;  
#  gzip_types       text/plain application/x-javascript text/css application/xml;  
#  gzip_vary on;  
    
  #limit_zone  crawler  $binary_remote_addr  10m;  
 ###禁止通过ip访问站点  
  server{  
        server_name _;  
        return 404;  
        }  
    
    
  server  
  {  
    listen       80;  
    server_name  localhost;  
    index index.html index.htm index.jsp;#设定访问的默认首页地址  
    root  /home/www/web/ROOT;#设定网站的资源存放路径  
    
    #limit_conn   crawler  20;      
        
    location ~ .*.jsp$ #所有jsp的页面均交由tomcat处理  
    {  
      index index.jsp;  
      proxy_pass http://localhost:8080;#转向tomcat处理  
      }  
          
        
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat  
    {  
      expires      30d;  
    }  
    
    location ~ .*\.(js|css)?$  
    {  
      expires      1h;  
    }      
    
#定义访问日志的写入格式  
     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '  
              '$status $body_bytes_sent "$http_referer" '  
              '"$http_user_agent" $http_x_forwarded_for';  
    access_log  /usr/local/nginx/logs/localhost.log access;#设定访问日志的存放路径  
    
      }     
}




          2.4然后我们运行linux命令


#/usr/local/nginx/sbin/nginx -t


如果屏幕显示以下两行信息,说明配置文件正确:  

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/nginx/conf/nginx.conf was tested successfull
但是这个时候一般会出现一个问题说是

error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory 
解决方法:


# ln -s /usr/local/lib/libpcre.so.1 /lib64

32位系统则:


ln -s /usr/local/lib/libpcre.so.1 /lib

注:
/usr/local/lib/libpcre.so.1 为prce安装后的文件地址
低版本prce对应的libpcre.so.1 为libpcre.so.0


          2.5启动与关闭Nginx

                1)启动

#/usr/local/nginx/sbin/nginx


                  重启:

/usr/local/nginx/sbin/nginx -s reload


      2)关闭

#/usr/local/nginx/sbin/nginx -s stop
或:
ps -ef | grep nginx
找到主进程ID,然后kill即可,如:


# kill -9 [进程号]

好了,这样nginx就安装好了。

(3)检查是否启动成功:
netstat -ano " grep80 "有结果输入说明启动成功
打开浏览器访问此机器的 IP,如果浏览器出现Welcome to nginx! 则表示 Nginx 已经安装并运行成功。如果已经配置了负载均衡服务器,则会看Tomcat中的网站页面

           3、安装memcached

              Memcached官网:点击进入

               3.1由于memcached安装时,需要使用libevent类库,所以先安装libevent 下载地址:点击进入

                 我下载的是版本:libevent-1.4.14b-stable.tar.gz

               3.2解压:

libevent-1.4.14b-stable.tar.gz
               3.3进入


cd libevent-1.4.14b-stable 
              3.4编译安装(默认安装到/usr/local/lib/目录)


# ./configure   
# make   
# make install 
             3.5安装memcached, memcached下载网址: http://www.danga.com/memcached/download.bml


1. 解压缩  
 tar xzfv memcached-1.4.22.tar.gz  
2. 进入到 memcached-1.4.22目录  
 cd memcached-1.4.22
3. 编译,安装  
    ./configure --prefix=/local/memcached  
    make  
    make install
安装完成后,会在 /local/memcached 出现 bin和share目录



进行 bin目录,启动 memcache
方法如下: 
./memcached -d -u nobody -m 512 127.0.0.1 -p 11211
此时,会报一个异常
 error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

原因是找不到libevent-1.4.so.2类库,解决办法如下:


使用LD_DEBUG=help ./memcached -v来确定 加载的类库路径,方法如下:
LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less
则系统会显示:


linux:/local/memcached/bin # LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less  
     20421:     find library=libevent-1.4.so.2; searching  
     20421:      search cache=/etc/ld.so.cache  
     20421:      search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686  
/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib          (system search path)  
     20421:       trying file=/lib/tls/i686/sse2/libevent-1.4.so.2  
     20421:       trying file=/lib/tls/i686/libevent-1.4.so.2  
     20421:       trying file=/lib/tls/sse2/libevent-1.4.so.2  
     20421:       trying file=/lib/tls/libevent-1.4.so.2  
     20421:       trying file=/lib/i686/sse2/libevent-1.4.so.2  
     20421:       trying file=/lib/i686/libevent-1.4.so.2  
     20421:       trying file=/lib/sse2/libevent-1.4.so.2  
     20421:       trying file=/lib/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/tls/i686/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/tls/sse2/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/tls/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/i686/sse2/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/i686/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/sse2/libevent-1.4.so.2  
     20421:       trying file=/usr/lib/libevent-1.4.so.2  
     20421:      
./memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

我们看到,memcached会到很多地方去找,所以根据其它求,我们只需建一个软链接,指定到我们安装的类库上即可方法如下:  
ln -s /usr/local/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2  
现在可以正常启动memcached了  
./memcached -d -u nobody -m 512 127.0.0.1 -p 11211  
memcache启动参数说明:
安装好了就可以使用memcached了,下面我用一种最简单的官方提供的方法java_memcached_releas



可以到这里去下载https://github.com/gwhalin/Memcached-Java-Client/downloads

       1)工具类


package org.memcached.dhp.test;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/** 
 *  
 * <pre><b>功能描述:</b>Memcached的 工具类 
 * 
 * @author xie)<br> 
 * 
 * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
 * 
 * </pre> 
 */  
public final class MemcachedUtil {
	/** 
     * <b>构造函数:工具类,禁止实例化</b> 
     *  
     */
	private MemcachedUtil() {
		
	}
	// 创建全局的唯一实例 
	private static MemCachedClient mcc = new MemCachedClient();
	
	/** 
     * 自身实例 
     */  
    private static MemcachedUtil memcachedUtil = new MemcachedUtil();
    
    // 设置与缓存服务器的连接池
    static {
    	// 服务器列表和其权重  
        String[] servers = {"192.168.13.100:11211" };// Ip地址和端口号  
        // 权重  
        Integer[] weights = {3 };
        // 获取socket连接池的实例对象  
        SockIOPool pool = SockIOPool.getInstance();
        // 设置服务器信息
        pool.setServers(servers);
        pool.setWeights(weights);
        
        // 设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(1000 * 60 * 60 * 6);
        
        // 设置主线程的睡眠时间
        pool.setMaintSleep(30);
        // 设置TCP的参数,连接超时等
        pool.setNagle(false);  
        pool.setSocketTO(3000);  
        pool.setSocketConnectTO(0);
        
        // 初始化连接池
        pool.initialize();
        // 压缩设置,超过指定大小(单位为K)的数据都会被压缩  
        // mcc.setCompressEnable(true);  
        // mcc.setCompressThreshold(64 * 1024);
        
        mcc.setPrimitiveAsString(true);   // 设置序列化
    }
    /** 
     *  
     * <pre><b>功能描述:</b>获取唯一实例. 
     *  
     * @author :xiezhaodong 
     * <b>创建日期 :</b>2012-4-25 上午10:57:41 
     * 
     * @return 
     * 
     * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
     * 
     * </pre> 
     */
    public static MemcachedUtil getInstance() {  
    	  
        return memcachedUtil;  
    }
    /** 
     *  
     * <pre><b>功能描述:</b>新增一个缓存数据 
     *  
     * @author :xiezhaodong 
     * <b>创建日期 :</b>2012-4-25 上午10:55:15 
     * 
     * @param key 缓存的key 
     * @param value 缓存的值 
     * @return 操作结果 
      
     * 
     * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
     * 
     * </pre> 
     */
    public boolean add(String key, Object value) {
    	// 不会存入缓存
    	return mcc.add(key, value);
    	// return mcc.set(key, value);
    }
    /** 
     *  
     * <pre><b>功能描述:</b>新增一个缓存数据 
     *  
     * @author :xiezhaodong 
     * <b>创建日期 :</b>2012-4-25 上午10:56:15 
     * 
     * @param key 缓存的key 
     * @param value 缓存的值 
     * @param expiry 缓存过期的时间 
     * @return 操作结果 
     * 
     * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
     * 
     * </pre> 
     */
    public boolean add(String key, Object value, Date expiry) {
    	// 不会存入缓存
    	return mcc.add(key, value, expiry);
    	// return mcc.set(key, value, expiry);
    }
    
    /** 
     * <pre><b>功能描述:</b>替换已有的缓存数据 
     *  
     * @author :xiezhaodong 
     * <b>创建日期 :</b>2012-4-25 上午10:55:34 
     * 
     * @param key 设置对象的key 
     * @return Object 设置对象的值 
     * @return 是否替换成功 
     *  
     * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
     * 
     * </pre> 
     */
    public boolean replace(String key, Object value) {
    	return mcc.replace(key, value);
    }
    /** 
     *  
     * <pre><b>功能描述:</b>替换已有的缓存数据 
     *  
     * @author :xiezhaodong 
     * <b>创建日期 :</b>2012-4-25 上午10:43:17 
     * 
     * @param key 设置对象的key 
     * @return Object 设置对象的值 
     * @param expiry 过期时间 
     * @return 是否替换成功 
     * 
     * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
     * 
     * </pre> 
     */
    public boolean replace(String key, Object value, Date expiry) {  
    	  
        return mcc.replace(key, value, expiry);  
    }
    /** 
     *  
     * <pre><b>功能描述:</b>根据指定的关键字获取对象 
     *  
     * @author :xiezhaodong 
     * <b>创建日期 :</b>2012-4-25 上午10:42:49 
     * 
     * @param key 获取对象的key 
     * @return Object 对象值 
     * 
     * <b>修改历史:</b>(修改人,修改时间,修改原因/内容) 
     * 
     * </pre> 
     */
    public Object get(String key) {
    	return mcc.get(key);
    }
    
}


       2)对象,必须实现序列化接口


package org.memcached.dhp.test;

import java.io.Serializable;

public class Employee implements Serializable {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -271486574863799175L;

	/** 
     * 员工名字 
     */  
    private String EmpName;  
      
    /** 
     * 部门名 
     */  
    private String deptName;  
      
    /** 
     * 公司名 
     */  
    private String companyName;  
      
    /** 
     *  
     * <b>构造函数:</b> 
     *  
     */  
    public Employee() {  
  
    }
    
    /** 
     * Access method for the empName property 
     *  
     * @return the empName 
     */  
    public String getEmpName() {  
  
        return EmpName;  
    }  
      
    /** 
     * Sets the value of empName the property 
     *  
     * @param empName the empName to set 
     */  
    public void setEmpName(String empName) {  
  
        EmpName = empName;  
    }  
      
    /** 
     * Access method for the deptName property 
     *  
     * @return the deptName 
     */  
    public String getDeptName() {  
  
        return deptName;  
    }  
      
    /** 
     * Sets the value of deptName the property 
     *  
     * @param deptName the deptName to set 
     */  
    public void setDeptName(String deptName) {  
  
        this.deptName = deptName;  
    }  
      
    /** 
     * Access method for the companyName property 
     *  
     * @return the companyName 
     */  
    public String getCompanyName() {  
  
        return companyName;  
    }  
      
    /** 
     * Sets the value of companyName the property 
     *  
     * @param companyName the companyName to set 
     */  
    public void setCompanyName(String companyName) {  
  
        this.companyName = companyName;  
    }
}
       3) 测试类


package org.memcached.dhp.test;

public class MemcachedUtilTest {
	public static void main(String[] args) {
	    //add();  
        
        print();
	}
	private static void print() {
		MemcachedUtil cache = MemcachedUtil.getInstance();
		Employee emp=(Employee) cache.get("emp");  
        System.out.println("name:"+emp.getCompanyName());  
        System.out.println("dep:"+emp.getDeptName());  
        System.out.println("emp:"+emp.getEmpName()); 
	}
	
	private static void add() {
		MemcachedUtil cache = MemcachedUtil.getInstance();  
		  
        Employee emp = new Employee();  
        emp.setCompanyName("Kevin's Company");  
        emp.setDeptName("R&D Dept");  
        emp.setEmpName("Kevin");  
          
          
        cache.add("emp", emp);
	}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值