自定义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;

/**
 * @author jwj
 * @date 2021/11/9
 */
@Component
public class JedisClusterFactory implements FactoryBean<JedisCluster>, 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<? extends JedisCluster> getObjectType() {
        return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
    }

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

    private Set<HostAndPort> parseHostAndPort() throws Exception {
        try {
            String[] addressArr = address.trim().split(",");
            Set<HostAndPort> haps = new HashSet<HostAndPort>();
            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<HostAndPort> 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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

    <!-- redis连接配置 start-->
    <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="minIdle" value="${redis.minIdle}"/>
    </bean>

    <!-- redis连接配置 end-->

    <bean id="jedisCluster" class="cn.shutdown.cachecloud.JedisClusterFactory">
        <property name="address" value="${redis.address}"/>
        <property name="timeout" value="${redis.timeout}"/>
        <property name="password" value="${redis.password}"/>
        <property name="maxRedirections" value="${redis.maxRedirections}"/>
        <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig"/>
    </bean>
</beans>

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

测试类

/**
* 通过Factory创建JedisCluster
* @date 2021/11/8
*/
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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值