栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自定义spring工厂类生产jedisCluster

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义spring工厂类生产jedisCluster

JedisClusterFactory.java

package cn.shutdown.cachecloud;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;


@Component
public class JedisClusterFactory implements FactoryBean, InitializingBean {

    private String address;
    private JedisCluster jedisCluster;
    private Integer timeout;
    private Integer maxRedirections;
    private String password;
    private GenericObjectPoolConfig 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;
    }

    private Set parseHostAndPort() throws Exception {
        try {
            String[] addressArr = address.trim().split(",");
            Set haps = new HashSet();
            for (String addressStr : addressArr) {
                String[] ipAndPort = addressStr.trim().split(":");
                HostAndPort hap = new HostAndPort(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1].trim()));
                haps.add(hap);
            }
            return haps;
        } catch (IllegalArgumentException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new Exception("解析 jedis 配置文件失败", ex);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Set haps = this.parseHostAndPort();
        jedisCluster = new JedisCluster(haps, timeout, timeout, maxRedirections, password, genericObjectPoolConfig);
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

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

    public void setAddress(String address) {
        this.address = address;
    }

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

    public void setPassword(String password) {
        this.password = password;
    }
}

spring的applicationContext.xml配置redis的连接池和工厂bean

 


    
        
            
                classpath:redis.properties
            
        
    

    
    
        
        
        
    

    

    
        
        
        
        
        
    

redis.properties配置

#redis pool config
redis.maxTotal=200
redis.maxIdle=50
redis.minIdle=10

#redis host and port config
redis.address=10.4.7.212:6410,10.4.7.213:7410,10.4.7.213:6411,10.4.7.214:7411,10.4.7.214:6412,10.4.7.212:7412
redis.timeout=300000
redis.maxRedirections=6
redis.password=2651080c6814a4a9d62da69a12f962b6

测试类

public class JedisDemo3 {

   public static void main(String[] args) throws Exception {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
               "classpath:applicationContext2.xml");
       JedisCluster jc = (JedisCluster) context.getBean("jedisCluster");
       jc.set("foo", "bar");
       String value = jc.get("foo");
       System.out.println("value:" + value);
   }
}

项目中的jedisCluster的使用

@Autowired
private JedisCluster jedisCluster;

参考文档:https://www.cnblogs.com/niceplay/p/4992614.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号