您的位置:首页 > 数据库 > Redis

JedisCluster连接redis集群(有密码)

2018-03-16 09:08 716 查看
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.a
4000
uth}"/>
</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;

注入后调用即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息