Nginx+Tomcat7负载均衡和集群环境搭建
1.Nginx
1.1简介
Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。
其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。
优点:
1、Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。 Nginx 的1.2.6稳定版已经于2012年12月11日发布,[1]1.3.10开发版已经于2012年12月25日发布,如果新建站点,建议使用最新稳定版作为生产版本,已有站点升级急迫性不高。Nginx的源代码使用 2-clause BSD-like license。
2、Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:
在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达50,000 个并发连接数的响应,感谢Nginx为我们选择了epoll and kqueue作为开发模型。
3、Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
4、Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器:Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。
1.2安装
官网下载地址:http://nginx.org/download/
选择nginx-1.7.9.tar.gz版本
将nginx-1.7.9.tar.gz上传到home目录
[root@datanode2-web home]#tar -zxvf nginx-1.7.9.tar.gz /解压压缩文件
[root@datanode2-web home]# cdnginx-1.7.9 /进入安装文件目录
[root@datanode2-webnginx-1.7.9]# ./configure - prefix=/usr/local/nginx
/指定安装到/usr/local/nginx目录下,可用./configure –help查看需要哪些编译参数
[root@datanode2-webnginx-1.7.9]#make /编译
[root@datanode2-webnginx-1.7.9]#make install /安装
[root@datanode2-web nginx-1.7.9]# ll /usr/local/nginx/ /查看是否安装成功 drwxr-xr-x 2 root root 4096 Sep 24 15:12 conf drwxr-xr-x 2 root root 4096 Sep 24 15:12 html drwxr-xr-x 2 root root 4096 Sep 24 15:12 logs drwxr-xr-x 2 root root 4096 Sep 24 15:12 sbin [root@datanode2-web nginx-1.7.9]#/usr/local/nginx/sbin/nginx –t /测试Nginx配置文件是否正确 [root@datanode2-web nginx-1.7.9]#/usr/local/nginx/sbin/nginx /启动Nginx
cd usr/local/nginx/sbin/ 目录执行以下命令 ./nginx –启动 ./nginx -s stop --停止 ./nginx -s reload –重启
http://10.10.92.145:88/ 145 nginx端口-修改nginx.conf文件改为88
有可能需要装pcre或者openssl,因为nginx需要这两个类库包-如果在make的过程中报错,则根据错误安装对应的安装包即可。
编写脚本启动命令: vi /etc/init.d/nginx (输入下面的代码)
#!/bin/bash
:wq 保存并退出 设置文件的访问权限 chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx… 同样的修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了 vi /etc/rc.local 加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
|
2.Memcached
官网下载地址:下载地址:http://www.danga.com/memcached/dist/
下载memcached-1.2.0.tar.gz安装包,上传到tmp目录
先安装libevent: 下载地址:http://www.monkey.org/~provos/libevent-1.2.tar.gz 上传到tmp目录
# tar zxvf libevent-1.2.tar.gz # cd libevent-1.2 # ./configure --prefix=/usr # make # make instal
测试libevent是否安装成功: # ls -al /usr/lib | grep libevent lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3 -rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3 -rw-r--r-- 1 root root 454156 11?? 12 17:38 libevent.a -rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
安装memcached,同时需要安装中指定libevent的安装位置: # cd /tmp # tar zxvf memcached-1.2.0.tar.gz # cd memcached-1.2.0 # ./configure --with-libevent=/usr # make # make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。 测试是否成功安装memcached: # ls -al /usr/local/bin/mem* -rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached -rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
启动报错: [root@datanode2-web bin]# memcached -d -c 10240 -m 1024 -u root memcached: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory 解决方案: [root@localhost bin]# LD_DEBUG=libs memcached -v
可以看到memcache已经启动成功 cd 进入memcache安装目录: cd /usr/local/bin/ 启动memcache: memcached -d -m 1024 -u root -p 11211 -c 1000 -P /tmp/memcached.pid 参数说明:
结束memcache服务: ps -ef|grep memcached 使用kill -9 ID(直接删掉进程)
相关资料: 2.把Memcached服务加载到Linux的启动项中.万一机器断电系统重启.那么Memcached就会自动启动了. 假如启动Memcache的服务器端的命令为: 想开机自动启动的话,只需在/etc/rc.d/rc.local中加入一行,下面命令 linux查看memcached状态 一、使用memcache top脚本 memcache-top 是一个用 Perl 编写的命令行小工具,用来实时的获取 memcached 缓存服务器的详细信息,例如缓存命中率等。到官网下载脚本,放到用户目录,直接运行即可。
/home/nihaoya/memcache-top
http://blog.sina.com.cn/s/blog_6a1fb8420102uz90.html
二、在windows上使用telnet IP 端口号 连接 可以输入flush_all 命令清除缓存 http://blog.csdn.net/clh604/article/details/16342755 常用命令文章说明 http://blog.sina.com.cn/s/blog_721cd3390101wqtx.html
|
3.Tomcat7
官网下载地址:http://archive.apache.org/dist/tomcat/tomcat-7/
下载apache-tomcat-7.0.52.tar.gz这个版本
将文件上传到/usr/local/tomcat7-home中
cd usr/local
mkdir tomcat7-home
tar zxvf apache-tomcat-7.0.52.tar.gz
mv apache-tomcat-7.0.52 tomct7-100
cd tomcat7-100
修改端口为100
重新解压apache-tomcat-7.0.52.tar.gz,重命名为tomcat7-101,修改端口为101
重新解压apache-tomcat-7.0.52.tar.gz,重命名为tomcat7-102,修改端口为102
同时修改三个应用的端口,如下红色的部分就是修改的部分。
tomcat7-100—server.xml配置文件 |
<?xml version='1.0' encoding='utf-8'?> <Server port="8105" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina">
<Connector port="100" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8543" /> <Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host> </Engine> </Service> </Server> |
tomcat7-101—server.xml配置文件 |
<?xml version='1.0' encoding='utf-8'?> <Server port="8205" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina">
<Connector port="101" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8643" /> <Connector port="8209" protocol="AJP/1.3" redirectPort="8643" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host> </Engine> </Service> </Server> |
tomcat7-102—server.xml配置文件 |
<?xml version='1.0' encoding='utf-8'?> <Server port="8305" shutdown="SHUTDOWN"> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina">
<Connector port="102" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8743" /> <Connector port="8309" protocol="AJP/1.3" redirectPort="8743" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host> </Engine> </Service> </Server> |
改完之后,三个tomcat就能同时启动了。
4.Nginx负载均衡配置
5.MemcachedSession共享
6.Tomcat7集群配置
Nginx负载配置: http://blog.csdn.net/binyao02123202/article/details/20387703
http://tengine.taobao.org/book/chapter_02.html http://www.linuxidc.com/Linux/2015-02/114184.htm
|
Session共享测试:--已完成-下一步是集群搭建 Tomcat+Memcached集群Session共享
|
Windows系统操作和linux系统也是一样的使用telnent命令进行操作 | ||||||||||||||||||||||
进入cmd输入:telnet 10.10.92.145 11211
连接成功之后使用以下命名进行操作: 命令结果: STORED :表示成功 NOT_STORED : 表示失败
|