Spring集成jedis支持Redis3.0集群

引用依赖:

<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.7.3</version>
  </dependency>
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-pool2</artifactId>
   <version>2.4.2</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>4.1.7.RELEASE</version>
  </dependency>java实现JedisCluster的FactoryBean。


package cn.slimsmart.jediscluster.spring;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {

 private GenericObjectPoolConfig genericObjectPoolConfig;
 private JedisCluster jedisCluster;
 private int connectionTimeout = 2000;
 private int soTimeout = 3000;
 private int maxRedirections = 5;
 private Set<String> jedisClusterNodes;

 @Override
 public void afterPropertiesSet() throws Exception {
  if (jedisClusterNodes == null || jedisClusterNodes.size() == 0) {
   throw new NullPointerException("jedisClusterNodes is null.");
  }
  Set<HostAndPort> haps = new HashSet<HostAndPort>();
  for (String node : jedisClusterNodes) {
   String[] arr = node.split(":");
   if (arr.length != 2) {
    throw new ParseException("node address error !",node.length()-1);
   }
   haps.add(new HostAndPort(arr[0], Integer.valueOf(arr[1])));
  }
  jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, genericObjectPoolConfig);
 }

 @Override
 public JedisCluster getObject() throws Exception {
  return jedisCluster;
 }

 @Override
 public Class<?> getObjectType() {
  return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
 }

 @Override
 public boolean isSingleton() {
  return true;
 }

 public GenericObjectPoolConfig getGenericObjectPoolConfig() {
  return genericObjectPoolConfig;
 }

 public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
  this.genericObjectPoolConfig = genericObjectPoolConfig;
 }

 public JedisCluster getJedisCluster() {
  return jedisCluster;
 }

 public void setJedisCluster(JedisCluster jedisCluster) {
  this.jedisCluster = jedisCluster;
 }

 public int getConnectionTimeout() {
  return connectionTimeout;
 }

 public void setConnectionTimeout(int connectionTimeout) {
  this.connectionTimeout = connectionTimeout;
 }

 public int getSoTimeout() {
  return soTimeout;
 }

 public void setSoTimeout(int soTimeout) {
  this.soTimeout = soTimeout;
 }

 public int getMaxRedirections() {
  return maxRedirections;
 }

 public void setMaxRedirections(int maxRedirections) {
  this.maxRedirections = maxRedirections;
 }

 public Set<String> getJedisClusterNodes() {
  return jedisClusterNodes;
 }

 public void setJedisClusterNodes(Set<String> jedisClusterNodes) {
  this.jedisClusterNodes = jedisClusterNodes;
 }
}
spring配置使用JedisCluster


<?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:cache="http://www.springframework.org/schema/cache"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"
 default-destroy-method="close" default-lazy-init="false">
 <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
  <property name="maxWaitMillis" value="-1" />
  <property name="maxTotal" value="8" />
  <property name="minIdle" value="0" />
  <property name="maxIdle" value="8" />
 </bean>
 <bean id="jedisCluster" class="cn.slimsmart.jediscluster.spring.JedisClusterFactory">
  <property name="connectionTimeout" value="3000" />
  <property name="soTimeout" value="3000" />
  <property name="maxRedirections" value="5" />
  <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
  <property name="jedisClusterNodes">
   <set>
    <value>192.168.36.54:6380</value>
    <value>192.168.36.54:6381</value>
    <value>192.168.36.54:6382</value>
    <value>192.168.36.54:6383</value>
    <value>192.168.36.189:6380</value>
    <value>192.168.36.189:6381</value>
    <value>192.168.36.189:6382</value>
    <value>192.168.36.189:6383</value>
   </set>
  </property>
 </bean>

</beans>可以在应用中注入JedisCluster处理业务。

下面关于Redis的文章您也可能喜欢,不妨参考下:

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

相关推荐