redis3.0 java spring,Spring集成jedis支持Redis3.0集群

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

引用依赖:

redis.clients

jedis

2.7.3

org.apache.commons

commons-pool2

2.4.2

org.springframework

spring-context

4.1.7.RELEASE

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, InitializingBean {

private GenericObjectPoolConfig genericObjectPoolConfig;

private JedisCluster jedisCluster;

private int connectionTimeout = 2000;

private int soTimeout = 3000;

private int maxRedirections = 5;

private Set jedisClusterNodes;

@Override

public void afterPropertiesSet() throws Exception {

if (jedisClusterNodes == null || jedisClusterNodes.size() == 0) {

throw new NullPointerException("jedisClusterNodes is null.");

}

Set haps = new HashSet();

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 getJedisClusterNodes() {

return jedisClusterNodes;

}

public void setJedisClusterNodes(Set jedisClusterNodes) {

this.jedisClusterNodes = jedisClusterNodes;

}

}

spring配置使用JedisCluster

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">

192.168.36.54:6380

192.168.36.54:6381

192.168.36.54:6382

192.168.36.54:6383

192.168.36.189:6380

192.168.36.189:6381

192.168.36.189:6382

192.168.36.189:6383

可以在应用中注入JedisCluster处理业务。

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

Redis 的详细介绍:请点这里

Redis 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值