JedisCluster连接redis集群(有密码)

redis集群是通过redis-trib.rb方式构建,在连接之前需要导入Jedis包
1.配置文件

###############################redis数据库的相关配置##################################
##访问密码
redis.auth = yangfuren
##控制一个pool最多可以有多少个状态为Idle(空)的jedis实例默认值为8
redis.maxIdle = 200
##可用的最大连接实例数 默认为8
redis.maxActive = 1024
##等待可用连接的最大时间单位为毫秒  默认为-1表示永不超时,一旦超过等待时间则直接抛出
redis.maxWait = 10000
redis.timeOut = 10000
##设置为true则会在borrow一个jedis实例时,提前做validate操作
redis.testOnBorrow =true
##连接最小空闲时间(毫秒)
redis.minEvictableIdleTimeMillis=1800000
##释放连接的扫描间隔(毫秒)
redis.timeBetweenEvictionRunsMillis=30000
##每次释放连接的最大数目
redis.numTestsPerEvictionRun=1024
##最大连接数
redis.maxTotal=30
##在空闲时检查有效性, 默认false 
redis.testWhileIdle=true
##连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
redis.blockWhenExhausted=false
redis.sockettimeout=1000
redis.maxAttempts=1000

#集群
cluster.host1=ip
cluster.port1=6380
cluster.port2=6381
cluster.port3=6382
cluster.port4=6383
cluster.port5=6384
cluster.port6=6385

spring的application.xml中配置


    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        
        <property name="maxTotal" value="${redis.maxTotal}"/>
        
        <property name="maxIdle" value="${redis.maxIdle}"/>
        
        <property  name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
        
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
        
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
        
        <property name="softMinEvictableIdleTimeMillis" value="10000"/>
        
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
        
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
    bean>

    

    
     <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">  
    <constructor-arg index="0">  
        <set>  
            <bean class="redis.clients.jedis.HostAndPort">  
                <constructor-arg index="0" value="${cluster.host1}">constructor-arg>  
                <constructor-arg index="1" value="${cluster.port1}">constructor-arg>  
            bean>  
            <bean class="redis.clients.jedis.HostAndPort">  
                <constructor-arg index="0" value="${cluster.host1}">constructor-arg>  
                <constructor-arg index="1" value="${cluster.port2}">constructor-arg>  
            bean>  
            <bean class="redis.clients.jedis.HostAndPort">  
                <constructor-arg index="0" value="${cluster.host1}">constructor-arg>  
                <constructor-arg index="1" value="${cluster.port3}">constructor-arg>  
            bean>  
            <bean class="redis.clients.jedis.HostAndPort">  
                <constructor-arg index="0" value="${cluster.host1}">constructor-arg>  
                <constructor-arg index="1" value="${cluster.port4}">constructor-arg>  
            bean>  
            <bean class="redis.clients.jedis.HostAndPort">  
                <constructor-arg index="0" value="${cluster.host1}">constructor-arg>  
                <constructor-arg index="1" value="${cluster.port5}">constructor-arg>  
            bean>  
            <bean class="redis.clients.jedis.HostAndPort">  
                <constructor-arg index="0" value="${cluster.host1}">constructor-arg>  
                <constructor-arg index="1" value="${cluster.port6}">constructor-arg>  
            bean>  
        set>  
    constructor-arg>  
    <constructor-arg index="1" value="${redis.timeOut}">constructor-arg>  
    <constructor-arg index="2" value="${redis.sockettimeout}">constructor-arg>  
    <constructor-arg index="3" value="${redis.maxAttempts}">constructor-arg>  
    <constructor-arg index="4" value="${redis.auth}">constructor-arg>  
    <constructor-arg index="5" ref="jedisPoolConfig">constructor-arg>  
bean>  

调用时候通过
@Resource
private SimpleRedisCluster simpleRedisCluster;
注入后调用即可

你可能感兴趣的:(redis)