1. 程式人生 > >Redis叢集配置和部署 spring整合redisCluster

Redis叢集配置和部署 spring整合redisCluster

Redis叢集配置和部署 1. 下載redis3.0 3. 建立叢集需要的目錄 mkdir /usr/local/cluster cd  /usr/local/cluster mkdir  7000 7001 7002 7003 7004 7005 4. 修改配置檔案redis.conf cp  /usr/local/redis3.0.5/redis.conf  /usr/local/cluster vi  redis.conf 修改配置檔案中如下選項 port  7000 daemonize  yes cluster-enabled  yes cluster-config-file  nodes.conf cluster-node-timeout  5000 appendonly  yes 修改完配置檔案後將
redis.conf檔案分別拷貝至70007001……7005目錄下 cp  /usr/local/cluster/redis.conf  /usr/local/cluster/7000 cp  /usr/local/cluster/redis.conf  /usr/local/cluster/7001 cp  /usr/local/cluster/redis.conf  /usr/local/cluster/7002 cp  /usr/local/cluster/redis.conf  /usr/local/cluster/7003 cp  /usr/local/cluster/redis.conf  /usr/local/cluster/7004 cp  /usr/local/cluster/redis.conf  /usr/local/cluster/7005 拷貝結束之後分別修改對應的配置檔案中的埠
port為對應的埠號 5. 分別啟動六個redis例項 /usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7000/redis.conf /usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7001/redis.conf /usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7002/redis.conf /usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7003/redis.conf /usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7004/redis.conf
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7005/redis.conf 檢視啟動情況 ps  -ef|grep redis 6. 升級ruby安裝gem 7. gem安裝redis ruby介面 8. 執行redis建立叢集命令建立叢集 /usr/local/redis-3.0.5/src/redis-trib.rb create --replicas 1 192.168.1.191:7000 192.168.1.191:7001 192.168.1.191:7002 192.168.1.191:7003 192.168.1.191:7004 192.168.1.191:7005 9. 進入叢集環境使用redis-cli /usr/local/redis-3.0.5/src/redis-cli -c -h 192.168.1.191 -p 7000 10. RedisCluster客戶端(Jedis 使用spring整合JedisCluster連線Redis3.0叢集 Ø Maven依賴 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency> Ø 增加spring配置 <bean name="genericObjectPoolConfig"class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >          <property name="maxWaitMillis" value="-1" />           <property name="maxTotal" value="1000" />          <property name="minIdle" value="8" />          <property name="maxIdle" value="100" />   </bean>   <bean id="jedisCluster"class="xxx.JedisClusterFactory">       <property name="addressConfig">           <value>classpath:connect-redis.properties</value>       </property>       <property name="addressKeyPrefix" value="address" />   <!--  屬性檔案裡  key的字首 -->       <property name="timeout" value="300000" />       <property name="maxRedirections" value="6" />       <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />   </bean> Ø 增加connect-redis.properties配置檔案 address1=192.168.1.191:7000 address2=192.168.1.191:7001 address3=192.168.1.191:7002 address4=192.168.1.191:7003 address5=192.168.1.191:7004 address6=192.168.1.191:7005 Ø 增加javaJedisClusterFactory 1. import java.util.HashSet;   2. import java.util.Properties;   3. import java.util.Set;   4. import java.util.regex.Pattern;   5.    6. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;   7. import org.springframework.beans.factory.FactoryBean;   8. import org.springframework.beans.factory.InitializingBean;   9. import org.springframework.core.io.Resource;   10.    11. import redis.clients.jedis.HostAndPort;   12. import redis.clients.jedis.JedisCluster;   13.    14. publicclass JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {   15.    16.     private Resource addressConfig;   17.     private String addressKeyPrefix ;   18.    19.     private JedisCluster jedisCluster;   20.     private Integer timeout;   21.     private Integer maxRedirections;   22.     private GenericObjectPoolConfig genericObjectPoolConfig;   23.        24.     private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");   25.    26.     @Override 27.     public JedisCluster getObject() throws Exception {   28.         return jedisCluster;   29.     }   30.    31.     @Override 32.     public Class<? extends JedisCluster> getObjectType() {   33.         return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);   34.     }   35.    36.     @Override 37.     publicboolean isSingleton() {   38.         returntrue;   39.     }   40.    41.    42.    43.     private Set<HostAndPort> parseHostAndPort() throws Exception {   44.         try {   45.             Properties prop = new Properties();   46.             prop.load(this.addressConfig.getInputStream());   47.    48.             Set<HostAndPort> haps = new HashSet<HostAndPort>();   49.             for (Object key : prop.keySet()) {   50.    51.                 if (!((String) key).startsWith(addressKeyPrefix)) {   52.                     continue;   53.                 }   54.    55.                 String val = (String) prop.get(key);   56.    57.                 boolean isIpPort = p.matcher(val).matches();   58.    59.                 if (!isIpPort) {   60.                     thrownew IllegalArgumentException("ip  port 不合法");   61.                 }   62.                 String[] ipAndPort = val.split(":");   63.    64.                 HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));   65.                 haps.add(hap);   66.             }   67.    68.             return haps;   69.         } catch (IllegalArgumentException ex) {   70.             throw ex;   71.         } catch (Exception ex) {   72.             thrownew Exception("解析 jedis 配置檔案失敗", ex);   73.         }   74.     }   75.        76.     @Override 77.     publicvoid afterPropertiesSet() throws Exception {   78.         Set<HostAndPort> haps = this.parseHostAndPort();   79.            80.         jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);   81.            82.     }   83.     publicvoid setAddressConfig(Resource addressConfig) {   84.         this.addressConfig = addressConfig;   85.     }   86.    87.     publicvoid setTimeout(int timeout) {   88.         this.timeout = timeout;   89.     }   90.    91.     publicvoid setMaxRedirections(int maxRedirections) {   92.         this.maxRedirections = maxRedirections;   93.     }   94.    95.     publicvoid setAddressKeyPrefix(String addressKeyPrefix) {   96.         this.addressKeyPrefix = addressKeyPrefix;   97.     }   98.    99.     publicvoid setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {   100.         this.genericObjectPoolConfig = genericObjectPoolConfig;   101.     }   102.    103. }   Ø 使用時直接注入 @Autowired JedisCluster  jedisCluster