首先附上maven仓库jar包的下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org

首先在linux系统安装redis3.0以上的版本,并且保证redis集群已经启动:

本次项目所需jar包:

完整图视:

1 新建属性文件:在src/conf/redis.properties:

address0=127.0.0.1:7000
address1=127.0.0.1:7001
address2=127.0.0.1:7002
address3=127.0.0.1:7003
address4=127.0.0.1:7004
address5=127.0.0.1:7005

redis.timeout=300000
redis.maxActive=1024
redis.minIdle=8
redis.maxIdle=100
redis.maxWaitMillis=1000
redis.maxRedirections=6
redis.testOnBorrow=true

2 新建文件夹 :src/xml/redis-context.xml

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   
   <!-- 加载配置文件 -->  
   <context:property-placeholder location="classpath:/conf/redis.properties" ignore-unresolvable="true"/> 
    <context:component-scan base-package="conf"/>  
    <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">  
        <property name="maxWaitMillis" value="-1" />  
        <property name="maxTotal" value="1000" />  
        <property name="minIdle" value="8" />  
        <property name="maxIdle" value="100" />  
        <property name="testOnBorrow" value="true" />
    </bean>   
    <bean id="jedisCluster" class="testDao.JedisClusterFactory">  
        <property name="addressConfig" value="classpath:/conf/redis.properties"/>  
        <property name="addressKeyPrefix" value="address" />   <!-- 属性文件里 key的前缀 -->  
        <property name="timeout" value="300000" />  
        <property name="maxRedirections" value="6" />  
        <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />  
    </bean>     
</beans>

3 实现bean工厂:src/testDao/JedisClusterFactory

package testDao;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean{
    
     private Resource addressConfig;  
     private String addressKeyPrefix ;  
     private JedisCluster jedisCluster;  
     private Integer timeout;  
     private Integer maxRedirections;  
     private GenericObjectPoolConfig genericObjectPoolConfig;          
     private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");
    
    
     public JedisClusterFactory() {
        
     }
    @Override
    public void afterPropertiesSet() throws Exception {
           Set<HostAndPort> haps = this.parseHostAndPort();  
              
            jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);  
        
    }

private Set<HostAndPort> parseHostAndPort() throws Exception{
        try {  
            Properties prop = new Properties();  
            prop.load(this.addressConfig.getInputStream());  
 
            Set<HostAndPort> haps = new HashSet<HostAndPort>();  
            for (Object key : prop.keySet()) {  
 
                if (!((String) key).startsWith(addressKeyPrefix)) {  
                    continue;  
                }  
 
                String val = (String) prop.get(key);  
 
                boolean isIpPort = p.matcher(val).matches();  
 
                if (!isIpPort) {  
                    throw new IllegalArgumentException("ip 或 port 不合法");  
                }  
                String[] ipAndPort = val.split(":");  
 
                HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));  
                haps.add(hap);  
            }  
 
            return haps;  
        } catch (IllegalArgumentException ex) {  
            throw ex;  
        } catch (Exception ex) {  
            throw new Exception("解析 jedis 配置文件失败", ex);  
        }  
    }

@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;
    }

public void setAddressConfig(Resource addressConfig) {  
        this.addressConfig = addressConfig;  
    }  
 
    public void setTimeout(int timeout) {  
        this.timeout = timeout;  
    }  
 
    public void setMaxRedirections(int maxRedirections) {  
        this.maxRedirections = maxRedirections;  
    }  
 
    public void setAddressKeyPrefix(String addressKeyPrefix) {  
        this.addressKeyPrefix = addressKeyPrefix;  
    }  
 
    public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {  
        this.genericObjectPoolConfig = genericObjectPoolConfig;  
    }     
}

4 新建Test类 测试redis-cluster

public class Test {

@Autowired
    static
    JedisCluster jedisCluster;
    private static ApplicationContext context;  
    static{  
        context = new ClassPathXmlApplicationContext("classpath:/xml/redis-context.xml");
    }
     public static void main(String[] args) {        
         jedisCluster = (JedisCluster) context.getBean("jedisCluster",JedisCluster.class);
        
         System.out.println(jedisCluster.get("name1"));
         System.out.println(jedisCluster.get("name2"));
         System.out.println(jedisCluster.get("first"));  
         int num = 100;
         String key = "wusc";
         String value = "";
         for (int i=1; i <= num; i++){
             // 存数据
//             jedisCluster.set(key+i,"WuShuicheng"+i);
             // 取数据
             value= jedisCluster.get(key+i);
             System.out.println(value);
//             // 删除数据
//             jedisCluster.del(key+i);     
        }
     }
    
}

最新文章

  1. 在Eclipse中使用建立使用Gradle做依赖管理的Spring Boot工程
  2. linux-ntpdate同步更新时间
  3. Elasticsearch——使用_cat查看Elasticsearch状态
  4. “破解大牛是怎么炼成的”之壳与ESP定律
  5. [转]Mybatis极其(最)简(好)单(用)的一个分页插件
  6. 将filenames里的每个字符串输出到out文件对象中注意行首的缩进
  7. Git 的origin和master分析 push/diff/head(转)
  8. IMAQ Flatten Image to String VI的参数设置对比
  9. for(int a:i)在java 编程中的使用
  10. OC-UICollectionView实现瀑布流
  11. TLB和MMU的区别
  12. Cesium 绘制点、线、面和测距
  13. Lily_music 网页音乐播放器 -可搜索(附歌词联动播放效果解说)
  14. socket跟TCP/IP 的关系,单台服务器上的并发TCP连接数可以有多少
  15. 【2019年03月29日】股票的滚动市盈率PE最低排名
  16. 2017-2018-1 20155317 《信息安全系统设计基础》课堂实践——实现mypwd
  17. BZOJ 1853: [Scoi2010]幸运数字(容斥原理)
  18. django入门-测试-part5
  19. javaweb(三十八)——mysql事务和锁InnoDB(扩展)
  20. 为pc编译配置安装当前最新的内核

热门文章

  1. 洛谷 P4707 【重返现世】
  2. 基于汇编的 C/C++ 协程 - 切换上下文
  3. 【[AHOI2013]差异】
  4. 20165302 程上杰 Exp1 PC平台逆向破解
  5. Python2.7-copy
  6. css 字体、文本、padding的样式
  7. 写脚本时出现: Permission denied
  8. DB2创建视图view
  9. Hadoop体系结构杂谈
  10. 20155325 Exp2 后门原理与实践