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中配置

<!-- redis连接池配置 -->
    <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"/>
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
    </bean>

    <!-- redis单机通过连接池
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool"
        destroy-method="close">
           <constructor-arg index="0" ref="jedisPoolConfig"/>  
          <constructor-arg index="1" value="${redis.addr}"/>  
          <constructor-arg index="2" value="${redis.port}" type="int"/>  
          <constructor-arg index="3" value="${redis.timeOut}" type="int"/>  
          <constructor-arg index="4" value="${redis.auth}"/>  
    </bean>
     -->

    <!-- redis集群 -->
     <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;
注入后调用即可

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值