Redis RedisCluster Spring整合

前言:

在上一篇文章中,用了jedisCluster来做redis集群的客户端,这一篇我们介绍一下spring-data-redis给我们提供的操作redis集群的redisTemplate,着急用的可以跳过1,直接看2

准备工作:

jdk版本:1.8

junit版本:4.12

jar包版本:

    <!-- jedis Java接口 -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
      <type>jar</type>
    </dependency>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.7.4.RELEASE</version>
    </dependency>

1:RedisTemplate由来简介

在网上没有找到redisTemplate操作redis集群的例子,所以只能自己动手,在这里简单说一下过程.首先既然redisTemplate依赖jedis,那我们可以认为他内部操作的就是jedis,同理,我们也可以认为他内部也能操作jedisCluster.接下来就在spring-data-redis的源码里面搜一下jedisCluster这个字符串,发现JedisClusterConnection和JedisConnectionFactory中出现了jedisCluster,有没有觉得JedisConnectionFactory很眼熟呢,对,就是配置文件中redisTemplate初始化时候需要用到的连接工厂.现在就可以直接看JedisConnectionFactory
首先,我们来看JedisConnectionFactory,发现里面有一个属性就是jedisCluster,那就看看jedisCluster是如何被初始化的,看下图:


我们可以先看这个方法,这个方法是从InitializingBean中实现的方法,是spring初始化bean的时候需要调用的.所以可以假装认为只要实例化了JedisConnectionFactory就可以实例化jedisCluster,但是不要忘了有一个条件,那就是clusterConfig不能为空,接下来我们找clusterConfig是如何被实例化的.发现JedisConnectionFactory有一个构造函数

JedisConnectionFactory(RedisClusterConfiguration clusterConfig, JedisPoolConfig poolConfig).这下就好办了JedisPoolConfig我们本来就认识,RedisClusterConfiguration是需要我们实例化的,接下来就看看RedisClusterConfiguration,一进来RedisClusterConfiguration我们就能看到个好东西,见下图:



我们可以看RedisClusterConfiguration的注释,虽然没有说明,但是光看格式,就大概能猜到这些东西应该是写到properties文件里面的,而构造函数的参数又正好是propertySource,很容易就能联想到ResourcePropertySource,接下来就简单了,直接开始开干了.

2:redisClusterConfig配置文件

<?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:p="http://www.springframework.org/schema/p"
       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">

<!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:redis.properties" />
    </bean>

    <!-- jedis 配置-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
        <!--最大空闲数-->
        <property name="maxIdle" value="${redis.maxIdle}" />
        <!--最大建立连接等待时间-->
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean >

    <!--配置文件加载-->
    <bean id="resourcePropertySource" class="org.springframework.core.io.support.ResourcePropertySource">
        <constructor-arg name="name" value="redis.properties"/>
        <constructor-arg name="resource" value="classpath:redis.properties"/>
    </bean>
    <!--redisCluster配置-->
    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <constructor-arg name="propertySource" ref="resourcePropertySource"/>
    </bean>
    <!-- redis服务器中心 -->
    <bean id="connectionFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
       <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
        <constructor-arg name="poolConfig" ref="poolConfig"/>
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}" ></property>
    </bean >
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
        <property name="connectionFactory" ref="connectionFactory" />
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer" >
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean >

    <bean id="redisHttpSessionConfiguration"
          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
       <!--超时时间,默认1800秒-->
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>

</beans>

3:redis.properties配置文件

#redis中心
#redis的服务器地址
redis.host=127.0.0.1
#redis的服务端口
redis.port=6379
#密码
redis.password=
#最大空闲数
redis.maxIdle=100
#最大连接数
redis.maxActive=300
#最大建立连接等待时间
redis.maxWait=1000
#客户端超时时间单位是毫秒
redis.timeout=100000
redis.maxTotal=1000
redis.minIdle=8
#明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true

#sentinel
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.node1.host=127.0.0.1
spring.redis.sentinel.node2.host=127.0.0.1
spring.redis.sentinel.node3.host=127.0.0.1
spring.redis.sentinel.node1.port=26379
spring.redis.sentinel.node2.port=26479
spring.redis.sentinel.node3.port=26579
#sentinel

#jediscluster
cluster1.host.port=127.0.0.1:7000
cluster2.host.port=127.0.0.1:7001
cluster3.host.port=127.0.0.1:7002
cluster4.host.port=127.0.0.1:7003
cluster5.host.port=127.0.0.1:7004
cluster6.host.port=127.0.0.1:7005
cluster7.host.port=127.0.0.1:7006
cluster8.host.port=127.0.0.1:7007
#jediscluster

#rediscluster
spring.redis.cluster.nodes=127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006,127.0.0.1:7007
spring.redis.cluster.max-redirects=3
#rediscluster

4:直接注入就可以使用,测试通过

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值