Spring集成jedis支持Redis3.0集群

时间:2021-08-22 17:58:56

接着上一节,我们通过spring FactoryBean实现redis 3.0集群JedisCluster与spring集成。  http://www.linuxidc.com/Linux/2016-09/135123.htm

引用依赖:

<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的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis系列-安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3安装Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis安装部署学习笔记 http://www.linuxidc.com/Linux/2014-07/104306.htm

Redis配置文件redis.conf 详解 http://www.linuxidc.com/Linux/2013-11/92524.htm

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

转载:http://www.linuxidc.com/Linux/2016-09/135124.htm