首先附上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);     
        }
     }
    
}

springmvc关于redisCluster的使用及配置的更多相关文章

  1. spring-mvc不拦截静态资源的配置

    spring-mvc不拦截静态资源的配置 标签: spring 2015-03-27 23:54 11587人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. &qu ...

  2. springmvc国际化 基于请求的国际化配置

    springmvc国际化 基于请求的国际化配置 基于请求的国际化配置是指,在当前请求内,国际化配置生效,否则自动以浏览器为主. 项目结构图: 说明:properties文件中为国际化资源文件.格式相关 ...

  3. springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置

    前面主要是后台代码,spring以及mybatis的整合 下面主要是springmvc用来处理请求转发,展现层的处理 之前所有做到的,完成了后台,业务层和持久层的开发完成了 接下来就是展现层了 有很多 ...

  4. 使用IntelliJ IDEA开发SpringMVC网站(二)框架配置

    原文:使用IntelliJ IDEA开发SpringMVC网站(二)框架配置 摘要 讲解如何配置SpringMVC框架xml,以及如何在Tomcat中运行 目录[-] 文章已针对IDEA 15做了一定 ...

  5. springMVC学习记录2-使用注解配置

    前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...

  6. spring 和springmvc 在 web.xml中的配置

    (1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...

  7. 使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置

    原文:使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置 摘要 讲解在IntelliJ IDEA中,如何进行Mysql数据库的配置 目录[-] 文章已针对IDEA 15做了一定的 ...

  8. spring-mvc.xml 和 application-context.xml的配置与深入理解

    在java框架这个话题,前几篇文章是基于搭建ssm项目框架,以及web.xml的配置讲解,本篇主要就ssm框架的其他配置文件进行深入讲解,他们分别是:1.application-context.xml ...

  9. Maven+SpringMVC+Dubbo 简单的入门demo配置

    转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...

随机推荐

  1. java使用纯命令行打包项目

    1: javac -d 编译之后的class文件输出目录   指定源文件位置即可.例如 对于多个包下面的源码编译,貌似javac不支持迭代编译,可能需要一次传入多个源码位置进行编译.一种便捷方法就是使 ...

  2. [luogu2172] 部落战争

    题面 ​ 我们可以将'.'抽象为一个可以通过的点, 将'x'抽象为一个不可通过的点. ​ 那么题意便可以转化为: 一条路径可以看做从任意一个没有到达过的可通过的点出发到任意一个其他的可以通过却没有被到 ...

  3. ap、map值计算

    ap:所有图片某一个类 map:所有图片所有类的ap的平均 以一个score为阈值,大于score的所有框是假定正确输出的所有预测,将这些框和gt匹配(iou大于某一个阈值认为匹配成功),得到当前sc ...

  4. <Android 开源库> PhotoPicker 从头到脚

    1. 简介 PhotoPicker, 是一款开源的图片选择器.效果上和微信相似. 2. 使用方法 2.1 添加依赖 dependencies { compile 'me.iwf.photopicker ...

  5. docker镜像的创建方法docker commit方式

    Docker 提供了两种构建镜像的方法: docker commit 命令(交互式修改创建) Dockerfile 构建文件 (文本命令定义) Docker commit方法: 1.运行一个现有容器 ...

  6. VC++定义全局变量及extern用法

    基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern有两 ...

  7. XmlSpy / XSD以及验证

    很早以前看过一句话:“XML就象空气”,在企业应用开发中XML是一个重要的数据交换标准.而XSD则可以用来校验XML的数据格式是否正确. 一个典型的XSD文件如下: <?xml version= ...

  8. WorldWind源码剖析系列:星球表面渲染类WorldSurfaceRenderer

    星球表面渲染类WorldSurfaceRenderer描述如何渲染星球类(如地球)表面影像纹理.该类的类图如下. 星球类World包含的主要的字段.属性和方法如下: public const int ...

  9. CentOS7服务器配置网络

    Centos7最小化安装 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-enp5s0f0编辑如下:TYPE=Ethernet ...

  10. JAVA框架 Mybaits 核心配置

    一:mybaits的核心配置文件:SqlMapConfig.xml 配置文件中需要关注的属性: 二.properites属性:一般引用配置文件(properites文件)比如:数据库的配置.我们可以编 ...