? ?????在互聯(lián)網(wǎng)項(xiàng)目中,往往面臨著高用戶量、高并發(fā)的問題,造成服務(wù)器的壓力非常大,特別是電商項(xiàng)目,以淘寶天貓為例,近年雙十一的成交量屢創(chuàng)新高,可想而知淘寶天貓的服務(wù)器面臨的并發(fā)量有多大,單一服務(wù)器肯定承受不住。這些互聯(lián)網(wǎng)項(xiàng)目的服務(wù)器一般都是采用集群部署的方式,那么下面我們來了解一下集群部署的解決方案。
1.什么是集群
? ? ? ? 集群是將一組相對(duì)獨(dú)立的計(jì)算機(jī)軟件或硬件連接在一起,構(gòu)成一個(gè)組,共同完成計(jì)算工作,對(duì)外,集群就像是一個(gè)獨(dú)立的服務(wù)器,集群中的單個(gè)服務(wù)器作為一個(gè)節(jié)點(diǎn),共同去處理相同的任務(wù),大大減輕每臺(tái)服務(wù)器的處理壓力。
2.集群與分布式的區(qū)別
? ? ? ? 分布式技術(shù)也是互聯(lián)網(wǎng)項(xiàng)目使用比較多的技術(shù),都需要有很多的節(jié)點(diǎn)服務(wù)器協(xié)同工作,那么集群與分布式的區(qū)別是什么呢?分布式是指將一個(gè)系統(tǒng)進(jìn)行拆分,拆分成多個(gè)子系統(tǒng),各個(gè)子系統(tǒng)實(shí)現(xiàn)不同的功能,單獨(dú)部署在不同的服務(wù)器上。通俗來說,多個(gè)服務(wù)器有不同的分工,做不同的事情,屬于分布式,多個(gè)服務(wù)器做同一件事情,屬于集群。
3.集群方案的具體實(shí)踐
? ? ? ? 在我們的實(shí)際項(xiàng)目部署中,一般需要采用集群部署的有,數(shù)據(jù)庫集群,例如mysql集群,Tomcat集群,緩存Redis集群,分布式中使用到的zookeeper集群等等。下面以搭建Redis集群redis-cluster集群為例。
3.1準(zhǔn)備工作
(1)安裝gcc(Redis是c語言開發(fā)的,安裝Redis需要C語言的編譯環(huán)境,所以需要先安裝gcc)
安裝命令:yum install gcc-c++
(2)安裝ruby(我們需要使用ruby腳本來實(shí)現(xiàn)集群搭建)
安裝命令:
yum install ruby
yum install rubygems
(3)將Redis源碼包上傳到Linux系統(tǒng)(沒有的先去網(wǎng)上下載),解壓Redis源碼包
(4)進(jìn)入Redis源碼文件夾,使用make命令編譯Redis源碼
(5)創(chuàng)建目錄/usr/local/redis-cluster,安裝3個(gè)Redis實(shí)例,分別安裝在一下目錄(注:由于服務(wù)器不足,演示的是搭建偽集群)
/usr/local/redis-cluster/redis-1
/usr/local/redis-cluster/redis-2
/usr/local/redis-cluster/redis-3
第一個(gè)Redis實(shí)例使用命令make install PREFIX=/usr/local/redis-cluster/redis-1,其他使用同樣方法安裝
(6)把Redis解壓文件夾中的redis.conf文件分別復(fù)制到剛安裝的Redis各個(gè)實(shí)例下的bin目錄下
/usr/local/redis-cluster/redis-1/bin
/usr/local/redis-cluster/redis-2/bin
/usr/local/redis-cluster/redis-3/bin
(7)修改redis.conf中Redis的運(yùn)行端口,使得各個(gè)Redis實(shí)例的運(yùn)行端口不一樣(這里分別改為7001,7002,7003),同時(shí)將cluster-enabled yes前的注釋去掉
(8)啟動(dòng)每個(gè)Redis實(shí)例,分別對(duì)三個(gè)實(shí)例使用下面的命令啟動(dòng)
cd /usr/local/redis-cluster/redis-1/bin/
./redis-server redis.conf
(9)上傳redis-3.0.0.gem,安裝ruby用于搭建redis集群的腳本
命令gem install redis-3.0.0.gem
(10)使用ruby腳本搭建集群,進(jìn)入redis源碼目錄中的src目錄,執(zhí)行命令
./redis-trib.rb create --replicas 1 你的服務(wù)器ip:7001 你的服務(wù)器ip:7002 你的服務(wù)器ip:7003
以上成功完成后,就可以使用客戶端連接Redis集群了,可以使用Redis-cli連接集群,也可以使用SpringDataRedis連接Redis集群,下面SpringDataRedis連接Redis集群的配置。
在你的工程項(xiàng)目中添加spring配置文件applicationContext-redis-cluster.xml
<?xml?version="1.0"?encoding="UTF-8"?>?
<beans?xmlns="http://www.springframework.org/schema/beans"?
??xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:p="http://www.springframework.org/schema/p"?
??xmlns:context="http://www.springframework.org/schema/context"?
??xsi:schemaLocation="http://www.springframework.org/schema/beans ??
????????????http://www.springframework.org/schema/beans/spring-beans.xsd ??
????????????http://www.springframework.org/schema/context ??
????????????http://www.springframework.org/schema/context/spring-context.xsd">??
<!-- 加載配置屬性文件 -->??
<context:property-placeholder?ignore-unresolvable="true"?location="classpath:properties/redis-cluster-config.properties" />??
<bean?id="redis-clusterConfiguration"?class="org.springframework.data.redis.connection.redis-clusterConfiguration">??
<property?name="maxRedirects"?value="${redis.maxRedirects}"></property>??
<property?name="clusterNodes">??
<set>??
<bean?class="org.springframework.data.redis.connection.redis-clusterNode">??
<constructor-arg?name="host"?value="${redis.host1}"></constructor-arg>??
<constructor-arg?name="port"?value="${redis.port1}"></constructor-arg>??
</bean>??
<bean?class="org.springframework.data.redis.connection.redis-clusterNode">??
<constructor-arg?name="host"?value="${redis.host2}"></constructor-arg>??
<constructor-arg?name="port"?value="${redis.port2}"></constructor-arg>??
</bean>??
<bean?class="org.springframework.data.redis.connection.redis-clusterNode">??
<constructor-arg?name="host"?value="${redis.host3}"></constructor-arg>??
<constructor-arg?name="port"?value="${redis.port3}"></constructor-arg>??
</bean>??
???</set>??
?</property>??
</bean>??
<bean?id="jedisPoolConfig"???class="redis.clients.jedis.JedisPoolConfig">??
???<property?name="maxIdle"?value="${redis.maxIdle}"?/>???
???<property?name="maxTotal"?value="${redis.maxTotal}"?/>???
</bean>??
<bean?id="jeidsConnectionFactory"?class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"??>??
<constructor-arg?ref="redis-clusterConfiguration"?/>??
<constructor-arg?ref="jedisPoolConfig"?/>??
</bean>????
<bean?id="redisTemplate"?class="org.springframework.data.redis.core.RedisTemplate">??
<property?name="connectionFactory"?ref="jeidsConnectionFactory"?/>??
</bean>??
</beans>
添加屬性文件redis-cluster-config.properties
#cluster configuration
redis.host1=?你的redis服務(wù)器ip
redis.port1=7001
redis.host2=你的redis服務(wù)器ip
redis.port2=7002
redis.host3=你的redis服務(wù)器ip
redis.port3=7003
redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600