Springmvc集成redis集群

1.maven依赖:

<dependency>

      <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

     <version>2.8.1</version>

</dependency>

2.增加spring 配置

  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  配置文件

这里配置了6个节点

  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类:

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

转载于:https://my.oschina.net/u/1421266/blog/847882

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:在Spring MVC中整合Redis的使用可以通过配置Jedis连接池来实现。首先需要在Spring配置文件中配置JedisPool bean,指定Redis服务的IP地址和端口号。然后可以创建一个JedisClientPool bean来使用连接池。\[1\]引用\[2\]:使用连接池的方式可以预先初始化好Jedis连接,每次只需要从连接池中借用连接即可,避免了每次都需要建立TCP连接的开销。这种方式还可以有效地保护和控制资源的使用。\[2\]引用\[3\]:另外,可以在Spring MVC中直接使用Jedis对象进行Redis操作,但这种方式每次都需要构建TCP连接,使用完后再关闭连接,效率较低。\[3\]因此,推荐使用连接池的方式来整合Redis在Spring MVC中的使用。 #### 引用[.reference_title] - *1* [SpringMVC整合Redis实战](https://blog.csdn.net/QQ1941638512/article/details/108344598)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringMVC整合Redis(包含java工具类)](https://blog.csdn.net/qq_40739917/article/details/107003722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值