阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Spring集成jedis支持Redis3.0集群

182次阅读
没有评论

共计 5092 个字符,预计需要花费 13 分钟才能阅读完成。

接着上一节,我们通过 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

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计5092字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中