springmvc 集成JedisCluster配置

springmvc 集成JedisCluster 配置

   点关注不迷路,欢迎再访!		

精简博客内容,尽量已行业术语来分享。
努力做到对每一位认可自己的读者负责。
帮助别人的同时更是丰富自己的良机。

cluster相对于哨兵模式是去中心化的,它的每个节点都存储了其它集群的信息,因此每个节点都可以做为集群的中心,容错能力强,具有更高的可用性和在线扩容能力。

引入依赖

<--2.9.0 以下版本不支持cluster密码认证-->
<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
</dependency>

配置redis.properties

###############################redis数据库的相关配置##################################
##访问密码
redis.auth = xxxxxx
##控制一个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

#集群节点
cluster.host1=ip
cluster.port1=7001
cluster.port2=7002
cluster.port3=7003
cluster.port4=7004
cluster.port5=7005
cluster.port6=7006

配置redis.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.2.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- redis连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
</bean>

    <bean id="jedis" class="redis.clients.jedis.JedisCluster">
        <constructor-arg>
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${cluster.port1}"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${cluster.port2}"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${cluster.port3}"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${cluster.port4}"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${cluster.port5}"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${cluster.port6}"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg name="connectionTimeout" value="3000" />
        <constructor-arg name="soTimeout" value="3000" />
        <constructor-arg name="maxAttempts" value="1000" />
        <constructor-arg name="password" value="${redis.auth}" />
        <constructor-arg name="poolConfig"> <ref bean="jedisPoolConfig" />
        </constructor-arg>
    </bean>
</beans>

测试验证

@Autowired
private JedisCluster jedis;
@Test
public void test(){
     jedis.set("abc123", "abc123");
     System.out.println("*********************************"+jedis.get("abc123"));
}

RedisTemplate 模板

下章学习RedisTemplate 模板

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值