Skip to content

Spring Data Redis 操作Redis Cluster

Ricky Fung edited this page Sep 8, 2017 · 1 revision

依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.8.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

Spring 配置

<?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:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--spring-data-redis-->
    <bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="10.96.5.183" />
        <constructor-arg value="9002"/>
    </bean>
    <bean id="clusterRedisNodes2" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="10.96.5.183" />
        <constructor-arg value="9002"/>
    </bean>

    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="clusterNodes">
            <set>
                <ref bean="clusterRedisNodes1"/>
                <ref bean="clusterRedisNodes2"/>
            </set>
        </property>
        <property name="maxRedirects" value="5" />
    </bean>

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="20" />
        <property name="maxIdle" value="1" />
        <property name="maxWaitMillis" value="2000" />
    </bean>

    <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true">
        <constructor-arg index="0" ref="redisClusterConfiguration" />
        <constructor-arg index="1" ref="jedisPoolConfig" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"  p:connection-factory-ref="jedisConnFactory" />

</beans>

Java基础篇

Java进阶篇

微服务

注册中心

配置中心

分库分表

领域建模

Others

Clone this wiki locally