Springmvc集成redis集羣

1.maven依賴:java

<dependency>node

      <groupId>redis.clients</groupId>redis

    <artifactId>jedis</artifactId>spring

     <version>2.8.1</version>apache

</dependency>maven

2.增長spring 配置tcp

  1. <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >  
  2.         <property name="maxWaitMillis" value="-1" />  
  3.         <property name="maxTotal" value="1000" />  
  4.         <property name="minIdle" value="8" />  
  5.         <property name="maxIdle" value="100" />  
  6. </bean>  
  7.   
  8. <bean id="jedisCluster" class="xxx.JedisClusterFactory">  
  9.     <property name="addressConfig">  
  10.         <value>classpath:connect-redis.properties</value>  
  11.     </property>  
  12.     <property name="addressKeyPrefix" value="address" />   <!--  屬性文件裏  key的前綴 -->  
  13.       
  14.     <property name="timeout" value="300000" />  
  15.     <property name="maxRedirections" value="6" />  
  16.     <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />  
  17. </bean>  

 

3.增長connect-redis.properties  配置文件ide

這裏配置了6個節點this

  1. address1=172.16.23.27:6379  
  2. address2=172.16.23.27:6380  
  3. address3=172.16.23.27:6381  
  4. address4=172.16.23.27:6382  
  5. address5=172.16.23.27:6383  
  6. address6=172.16.23.27:6384  

 

4.增長java類:spa

Java代碼

  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. public class 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.     public boolean isSingleton() {  
  38.         return true;  
  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.                     throw new 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.             throw new Exception("解析 jedis 配置文件失敗", ex);  
  73.         }  
  74.     }  
  75.       
  76.     @Override  
  77.     public void afterPropertiesSet() throws Exception {  
  78.         Set<HostAndPort> haps = this.parseHostAndPort();  
  79.           
  80.         jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);  
  81.           
  82.     }  
  83.     public void setAddressConfig(Resource addressConfig) {  
  84.         this.addressConfig = addressConfig;  
  85.     }  
  86.   
  87.     public void setTimeout(int timeout) {  
  88.         this.timeout = timeout;  
  89.     }  
  90.   
  91.     public void setMaxRedirections(int maxRedirections) {  
  92.         this.maxRedirections = maxRedirections;  
  93.     }  
  94.   
  95.     public void setAddressKeyPrefix(String addressKeyPrefix) {  
  96.         this.addressKeyPrefix = addressKeyPrefix;  
  97.     }  
  98.   
  99.     public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {  
  100.         this.genericObjectPoolConfig = genericObjectPoolConfig;  
  101.     }  
  102.   
  103. }  

 

5.到此配置完成,使用時,直接注入便可, 以下所示:

@Autowired

JedisCluster jedisCluster;

注意:「no reachable node in cluster] with root cause」報錯

(1)若是redis集羣不是部署在本機,是部署在其它機器或者虛擬機上,必定注意redis.conf配置文件中的bind ip不要使用127.0.0.1,而是改爲機器的ip地址,否則將沒法訪問redis集羣,會由於找不到redis集羣報這樣的錯誤;

同時防火牆開放端口

$ iptables -I INPUT -p tcp --dport 7000 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 7001 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 7002 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 7003 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 7004 -j ACCEPT
$ iptables -I INPUT -p tcp --dport 7005 -j ACCEPT

(2)查看是否關閉了防火牆,若是沒有關閉防火牆,利用以下命令關閉防火牆:

1) 重啓後生效 
開啓: chkconfig iptables on 
關閉: chkconfig iptables off 

2) 即時生效,重啓後失效 
開啓: service iptables start 
關閉: service iptables stop
相關文章
相關標籤/搜索